libvips/libvips/arithmetic/hough.c

219 lines
5.3 KiB
C
Raw Normal View History

2014-03-18 20:39:47 +01:00
/* hough transform
*
* 7/3/14
2014-03-22 18:07:35 +01:00
* - from hist_find.c
2014-03-18 20:39:47 +01:00
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <string.h>
#include <vips/vips.h>
#include "statistic.h"
2014-03-24 15:56:04 +01:00
#include "hough.h"
2014-03-18 20:39:47 +01:00
2014-03-24 15:56:04 +01:00
G_DEFINE_TYPE_ABSTRACT( VipsHough, vips_hough, VIPS_TYPE_STATISTIC );
2014-03-18 20:39:47 +01:00
static int
vips_hough_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsStatistic *statistic = VIPS_STATISTIC( object );
VipsHough *hough = (VipsHough *) object;
2014-03-22 18:07:35 +01:00
VipsImage *out;
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
/* Mono only, we use the bands dimension of the output image for
* a parameter.
*/
if( statistic->in )
if( vips_check_mono( class->nickname, statistic->in ) )
return( -1 );
2014-03-18 20:39:47 +01:00
if( VIPS_OBJECT_CLASS( vips_hough_parent_class )->build( object ) )
return( -1 );
2014-03-22 18:07:35 +01:00
/* hough->threads should be an array of completed accumulators, and we
* should have noted one for each thread we started.
2014-03-18 20:39:47 +01:00
*/
2014-03-22 18:07:35 +01:00
g_assert( hough->threads );
g_assert( hough->n_threads > 0 );
g_assert( hough->n_threads == hough->ith );
if( vips_sum( hough->threads, &out, hough->n_threads, NULL ) )
2014-03-18 20:39:47 +01:00
return( -1 );
2014-03-22 18:07:35 +01:00
g_object_set( object,
"out", out,
NULL );
2014-03-18 20:39:47 +01:00
return( 0 );
}
2014-03-22 18:07:35 +01:00
/* Build a new accumulator.
2014-03-18 20:39:47 +01:00
*/
static void *
vips_hough_start( VipsStatistic *statistic )
{
VipsHough *hough = (VipsHough *) statistic;
2014-03-24 15:56:04 +01:00
VipsHoughClass *class = VIPS_HOUGH_GET_CLASS( hough );
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
VipsImage *accumulator;
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
/* Make a note of the number of threads we start.
2014-03-18 20:39:47 +01:00
*/
2014-03-22 18:07:35 +01:00
hough->n_threads += 1;
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
/* We assume that we don't start any more threads after the first stop
* is called.
2014-03-18 20:39:47 +01:00
*/
2014-03-22 18:07:35 +01:00
g_assert( !hough->threads );
2014-03-18 20:39:47 +01:00
2014-03-24 15:56:04 +01:00
if( !(accumulator = class->new_accumulator( hought )) )
return( NULL );
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
if( vips_image_write_prepare( accumulator ) ) {
g_object_unref( accumulator );
return( NULL );
}
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
/* vips does not guarantee image mem is zeroed.
2014-03-18 20:39:47 +01:00
*/
2014-03-22 18:07:35 +01:00
memset( VIPS_IMAGE_ADDR( accumulator, 0, 0 ), 0,
VIPS_IMAGE_SIZEOF_IMAGE( accumulator ) );
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
return( (void *) accumulator );
2014-03-18 20:39:47 +01:00
}
2014-03-22 18:07:35 +01:00
/* Add our finished accumulator to the main area.
2014-03-18 20:39:47 +01:00
*/
static int
2014-03-22 18:07:35 +01:00
vips_hough_stop( VipsStatistic *statistic, void *seq )
2014-03-18 20:39:47 +01:00
{
2014-03-22 18:07:35 +01:00
VipsImage *accumulator = (VipsImage *) seq;
VipsHough *hough = (VipsHough *) statistic;
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
/* If this is the first stop, build the main accumulator array. We
* assume no more threads will start, see the assert above.
2014-03-18 20:39:47 +01:00
*/
2014-03-22 18:07:35 +01:00
if( !hough->threads )
/* This will unref the accumulators automatically on dispose.
*/
hough->threads = (VipsImage **)
vips_object_local_array( VIPS_OBJECT( hough ),
hough->n_threads );
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
g_assert( !hough->threads[hough->ith] );
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
hough->threads[hough->ith] = accumulator;
hough->ith += 1;
2014-03-18 20:39:47 +01:00
return( 0 );
}
static int
2014-03-22 18:07:35 +01:00
vips_hough_scan( VipsStatistic *statistic,
void *seq, int x, int y, void *in, int n )
2014-03-18 20:39:47 +01:00
{
VipsHough *hough = (VipsHough *) statistic;
2014-03-24 15:56:04 +01:00
VipsHoughClass *class = VIPS_HOUGH_GET_CLASS( hough );
2014-03-22 18:07:35 +01:00
VipsImage *accumulator = (VipsImage *) seq;
VipsPel *p = (VipsPel *) in;
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
int i;
for( i = 0; i < n; i++ )
if( p[i] )
2014-03-24 15:56:04 +01:00
class->vote( hough, accumulator, x + i, y );
2014-03-18 20:39:47 +01:00
2014-03-22 18:07:35 +01:00
return( 0 );
2014-03-18 20:39:47 +01:00
}
#define UC VIPS_FORMAT_UCHAR
2014-03-22 18:07:35 +01:00
/* Input image is cast to this format.
2014-03-18 20:39:47 +01:00
*/
2014-03-22 18:07:35 +01:00
static const VipsBandFormat vips_hough_format_table[10] = {
2014-03-18 20:39:47 +01:00
/* UC C US S UI I F X D DX */
2014-03-22 18:07:35 +01:00
UC, UC, UC, UC, UC, UC, UC, UC, UC, UC
2014-03-18 20:39:47 +01:00
};
static void
vips_hough_class_init( VipsHoughClass *class )
{
GObjectClass *gobject_class = (GObjectClass *) class;
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsStatisticClass *sclass = VIPS_STATISTIC_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "hough";
2014-03-22 18:07:35 +01:00
object_class->description = _( "find hough transform" );
2014-03-18 20:39:47 +01:00
object_class->build = vips_hough_build;
sclass->start = vips_hough_start;
sclass->scan = vips_hough_scan;
sclass->stop = vips_hough_stop;
2014-03-22 18:07:35 +01:00
sclass->format_table = vips_hough_format_table;
2014-03-18 20:39:47 +01:00
VIPS_ARG_IMAGE( class, "out", 100,
_( "Output" ),
2014-03-22 18:07:35 +01:00
_( "Output image" ),
2014-03-18 20:39:47 +01:00
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsHough, out ) );
2014-03-22 18:07:35 +01:00
VIPS_ARG_INT( class, "width", 110,
_( "Width" ),
_( "horizontal size of parameter space" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsHough, width ),
1, 100000, 256 );
VIPS_ARG_INT( class, "height", 110,
_( "Height" ),
_( "Vertical size of parameter space" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsHough, height ),
1, 100000, 256 );
2014-03-18 20:39:47 +01:00
}
static void
vips_hough_init( VipsHough *hough )
{
2014-03-22 18:07:35 +01:00
hough->width = 256;
hough->height = 256;
2014-03-18 20:39:47 +01:00
}