From 067b2a6ac15bf934e7080247077923aacff8400e Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 24 Mar 2014 14:56:04 +0000 Subject: [PATCH] started splitting hough.c up --- libvips/arithmetic/hough.c | 96 +------------- libvips/arithmetic/hough.h | 104 +++++++++++++++ libvips/arithmetic/houghline.c | 235 +++++++++++++++++++++++++++++++++ 3 files changed, 346 insertions(+), 89 deletions(-) create mode 100644 libvips/arithmetic/hough.h create mode 100644 libvips/arithmetic/houghline.c diff --git a/libvips/arithmetic/hough.c b/libvips/arithmetic/hough.c index 10820316..b68b1d02 100644 --- a/libvips/arithmetic/hough.c +++ b/libvips/arithmetic/hough.c @@ -41,31 +41,9 @@ #include #include "statistic.h" +#include "hough.h" -typedef struct _VipsHough { - VipsStatistic parent_instance; - - /* Size of parameter space. - */ - int width; - int height; - - /* Each thread adds its accumulator image to this array on stop. - * ith is the index the ith thread places its image at. - */ - VipsImage **threads; - int n_threads; - int ith; - - /* Sum the thread accumulators to here. - */ - VipsImage *out; - -} VipsHough; - -typedef VipsStatisticClass VipsHoughClass; - -G_DEFINE_TYPE( VipsHough, vips_hough, VIPS_TYPE_STATISTIC ); +G_DEFINE_TYPE_ABSTRACT( VipsHough, vips_hough, VIPS_TYPE_STATISTIC ); static int vips_hough_build( VipsObject *object ) @@ -109,6 +87,7 @@ static void * vips_hough_start( VipsStatistic *statistic ) { VipsHough *hough = (VipsHough *) statistic; + VipsHoughClass *class = VIPS_HOUGH_GET_CLASS( hough ); VipsImage *accumulator; @@ -121,16 +100,8 @@ vips_hough_start( VipsStatistic *statistic ) */ g_assert( !hough->threads ); - accumulator = vips_image_new_buffer(); - - vips_image_pipelinev( accumulator, - VIPS_DEMAND_STYLE_ANY, statistic->in, NULL ); - - vips_image_init_fields( accumulator, - hough->width, hough->height, 1, - VIPS_FORMAT_UINT, VIPS_CODING_NONE, - VIPS_INTERPRETATION_MATRIX, - 1.0, 1.0 ); + if( !(accumulator = class->new_accumulator( hought )) ) + return( NULL ); if( vips_image_write_prepare( accumulator ) ) { g_object_unref( accumulator ); @@ -171,37 +142,12 @@ vips_hough_stop( VipsStatistic *statistic, void *seq ) return( 0 ); } -/* Cast votes for all lines passing through x, y. - */ -static void -hough_vote( VipsHough *hough, VipsImage *accumulator, int x, int y ) -{ - VipsStatistic *statistic = (VipsStatistic *) hough; - double xd = (double) x / statistic->ready->Xsize; - double yd = (double) y / statistic->ready->Ysize; - - int thetai; - - for( thetai = 0; thetai < hough->width; thetai++ ) { - double theta = 2 * M_PI * thetai / hough->width; - double r = xd * cos( theta ) + yd * sin( theta ); - int ri = hough->height * r; - - if( ri >= 0 && - ri < hough->height ) - *VIPS_IMAGE_ADDR( accumulator, thetai, ri ) += 1; - } -} - -/* See our superclass in statistic.c, but this is called for each section of - * each scanline. @x, @y is the position of the left end, @in is the pixel - * data, @n is the number of pixels in this scanline. VipsPel is uint8. - */ static int vips_hough_scan( VipsStatistic *statistic, void *seq, int x, int y, void *in, int n ) { VipsHough *hough = (VipsHough *) statistic; + VipsHoughClass *class = VIPS_HOUGH_GET_CLASS( hough ); VipsImage *accumulator = (VipsImage *) seq; VipsPel *p = (VipsPel *) in; @@ -209,7 +155,7 @@ vips_hough_scan( VipsStatistic *statistic, for( i = 0; i < n; i++ ) if( p[i] ) - hough_vote( hough, accumulator, x + i, y ); + class->vote( hough, accumulator, x + i, y ); return( 0 ); } @@ -270,31 +216,3 @@ vips_hough_init( VipsHough *hough ) hough->width = 256; hough->height = 256; } - -/** - * vips_hough: - * @in: input image - * @out: output image - * @...: %NULL-terminated list of optional named arguments - * - * Optional arguments: - * - * @width: horizontal size of parameter space - * @height: vertical size of parameter space - * - * See also: - * - * Returns: 0 on success, -1 on error - */ -int -vips_hough( VipsImage *in, VipsImage **out, ... ) -{ - va_list ap; - int result; - - va_start( ap, out ); - result = vips_call_split( "hough", ap, in, out ); - va_end( ap ); - - return( result ); -} diff --git a/libvips/arithmetic/hough.h b/libvips/arithmetic/hough.h new file mode 100644 index 00000000..2718a751 --- /dev/null +++ b/libvips/arithmetic/hough.h @@ -0,0 +1,104 @@ +/* hough transform + * + * 7/3/14 + * - from hist_find.c + */ + +/* + + 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 + + */ + +#ifndef VIPS_HOUGH_H +#define VIPS_HOUGH_H + +#ifdef __cplusplus +extern "C" { +#endif /*__cplusplus*/ + +#define VIPS_TYPE_HOUGH (vips_hough_get_type()) +#define VIPS_HOUGH( obj ) \ + (G_TYPE_CHECK_INSTANCE_CAST( (obj), \ + VIPS_TYPE_HOUGH, VipsHough )) +#define VIPS_HOUGH_CLASS( klass ) \ + (G_TYPE_CHECK_CLASS_CAST( (klass), \ + VIPS_TYPE_HOUGH, VipsHoughClass)) +#define VIPS_IS_HOUGH( obj ) \ + (G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_HOUGH )) +#define VIPS_IS_HOUGH_CLASS( klass ) \ + (G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_HOUGH )) +#define VIPS_HOUGH_GET_CLASS( obj ) \ + (G_TYPE_INSTANCE_GET_CLASS( (obj), \ + VIPS_TYPE_HOUGH, VipsHoughClass )) + +typedef struct _VipsHough VipsHough; +typedef struct _VipsHoughClass VipsHoughClass; + +typedef VipsImage *(*VipsHoughNewAccumulator)( VipsHough *hough ); +typedef void (*VipsHoughVote)( VipsHough *hough, + VipsImage *accumulator, int x, int y ); + +struct _VipsHough { + VipsStatistic parent_instance; + + /* Size of parameter space. All have at least two dimensions, some + * subclasses add a third. + */ + int width; + int height; + + /* Each thread adds its accumulator image to this array on stop. + * ith is the index the ith thread places its image at. + */ + VipsImage **threads; + int n_threads; + int ith; + + /* Sum the thread accumulators to here. + */ + VipsImage *out; + +}; + +struct _VipsHoughClass { + VipsStatisticClass parent_class; + + /* Make a new accumulator image. + */ + VipsHoughNewAccumulator new_accumulator; + + /* Vote function for this parameter space. + */ + VipsHoughVote vote; + +}; + +GType vips_hough_get_type( void ); + +#ifdef __cplusplus +} +#endif /*__cplusplus*/ + +#endif /*VIPS_HOUGH_H*/ diff --git a/libvips/arithmetic/houghline.c b/libvips/arithmetic/houghline.c new file mode 100644 index 00000000..f9a21686 --- /dev/null +++ b/libvips/arithmetic/houghline.c @@ -0,0 +1,235 @@ +/* hough transform + * + * 7/3/14 + * - from hist_find.c + */ + +/* + + 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 +#endif /*HAVE_CONFIG_H*/ +#include + +#include + +#include + +#include "statistic.h" +#include "hough.h" + +typedef VipsHoughLine VipsHough; +typedef VipsHoughLineClass VipsHoughClass; + +G_DEFINE_TYPE( VipsHough, vips_hough, VIPS_TYPE_HOUGH ); + +/* Build a new accumulator. + */ +static VipsImage * +vips_houghline_new_accumulator( VipsHough *hough ) +{ + VipsStatistic *statistic = (VipsSt + VipsImage *accumulator; + + accumulator = vips_image_new_buffer(); + + vips_image_pipelinev( accumulator, + VIPS_DEMAND_STYLE_ANY, statistic->in, NULL ); + + vips_image_init_fields( accumulator, + hough->width, hough->height, 1, + VIPS_FORMAT_UINT, VIPS_CODING_NONE, + VIPS_INTERPRETATION_MATRIX, + 1.0, 1.0 ); + + if( vips_image_write_prepare( accumulator ) ) { + g_object_unref( accumulator ); + return( NULL ); + } + + /* vips does not guarantee image mem is zeroed. + */ + memset( VIPS_IMAGE_ADDR( accumulator, 0, 0 ), 0, + VIPS_IMAGE_SIZEOF_IMAGE( accumulator ) ); + + return( (void *) accumulator ); +} + +/* Add our finished accumulator to the main area. + */ +static int +vips_hough_stop( VipsStatistic *statistic, void *seq ) +{ + VipsImage *accumulator = (VipsImage *) seq; + VipsHough *hough = (VipsHough *) statistic; + + /* If this is the first stop, build the main accumulator array. We + * assume no more threads will start, see the assert above. + */ + if( !hough->threads ) + /* This will unref the accumulators automatically on dispose. + */ + hough->threads = (VipsImage **) + vips_object_local_array( VIPS_OBJECT( hough ), + hough->n_threads ); + + g_assert( !hough->threads[hough->ith] ); + + hough->threads[hough->ith] = accumulator; + hough->ith += 1; + + return( 0 ); +} + +/* Cast votes for all lines passing through x, y. + */ +static void +hough_vote( VipsHough *hough, VipsImage *accumulator, int x, int y ) +{ + VipsStatistic *statistic = (VipsStatistic *) hough; + double xd = (double) x / statistic->ready->Xsize; + double yd = (double) y / statistic->ready->Ysize; + + int thetai; + + for( thetai = 0; thetai < hough->width; thetai++ ) { + double theta = 2 * M_PI * thetai / hough->width; + double r = xd * cos( theta ) + yd * sin( theta ); + int ri = hough->height * r; + + if( ri >= 0 && + ri < hough->height ) + *VIPS_IMAGE_ADDR( accumulator, thetai, ri ) += 1; + } +} + +/* See our superclass in statistic.c, but this is called for each section of + * each scanline. @x, @y is the position of the left end, @in is the pixel + * data, @n is the number of pixels in this scanline. VipsPel is uint8. + */ +static int +vips_hough_scan( VipsStatistic *statistic, + void *seq, int x, int y, void *in, int n ) +{ + VipsHough *hough = (VipsHough *) statistic; + VipsImage *accumulator = (VipsImage *) seq; + VipsPel *p = (VipsPel *) in; + + int i; + + for( i = 0; i < n; i++ ) + if( p[i] ) + hough_vote( hough, accumulator, x + i, y ); + + return( 0 ); +} + +#define UC VIPS_FORMAT_UCHAR + +/* Input image is cast to this format. + */ +static const VipsBandFormat vips_hough_format_table[10] = { +/* UC C US S UI I F X D DX */ + UC, UC, UC, UC, UC, UC, UC, UC, UC, UC +}; + +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"; + object_class->description = _( "find hough transform" ); + object_class->build = vips_hough_build; + + sclass->start = vips_hough_start; + sclass->scan = vips_hough_scan; + sclass->stop = vips_hough_stop; + sclass->format_table = vips_hough_format_table; + + VIPS_ARG_IMAGE( class, "out", 100, + _( "Output" ), + _( "Output image" ), + VIPS_ARGUMENT_REQUIRED_OUTPUT, + G_STRUCT_OFFSET( VipsHough, out ) ); + + 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 ); + +} + +static void +vips_hough_init( VipsHough *hough ) +{ + hough->width = 256; + hough->height = 256; +} + +/** + * vips_hough: + * @in: input image + * @out: output image + * @...: %NULL-terminated list of optional named arguments + * + * Optional arguments: + * + * @width: horizontal size of parameter space + * @height: vertical size of parameter space + * + * See also: + * + * Returns: 0 on success, -1 on error + */ +int +vips_hough( VipsImage *in, VipsImage **out, ... ) +{ + va_list ap; + int result; + + va_start( ap, out ); + result = vips_call_split( "hough", ap, in, out ); + va_end( ap ); + + return( result ); +}