1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/* hough transform for circles
*
* 7/3/14
* - from hough_line.c
* 2/1/18
* - 20% speedup
*/
/*
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
*/
/* Derived in part from David Young's Matlab circle detector:
*
* http://www.mathworks.com/matlabcentral/fileexchange/26978-hough-transform-for-circles
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <string.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include "statistic.h"
#include "hough.h"
typedef struct _VipsHoughCircle {
VipsHough parent_instance;
int scale;
int min_radius;
int max_radius;
int width;
int height;
int bands;
} VipsHoughCircle;
typedef VipsHoughClass VipsHoughCircleClass;
G_DEFINE_TYPE( VipsHoughCircle, vips_hough_circle, VIPS_TYPE_HOUGH );
/* Smaller circles have fewer pixels and therefore fewer votes. Scale bands by
* the ratio of circumference, so all radii get equal weight.
*/
static void
vips_hough_circle_normalise( VipsHoughCircle *hough_circle )
{
VipsHough *hough = (VipsHough *) hough_circle;
int max_radius = hough_circle->max_radius;
int min_radius = hough_circle->min_radius;
int scale = hough_circle->scale;
int bands = hough_circle->bands;
int width = hough_circle->width;
int height = hough_circle->height;
double max_circumference = 2 * VIPS_PI * max_radius;
int b;
for( b = 0; b < bands; b++ ) {
int radius = b * scale + min_radius;
double circumference = 2 * VIPS_PI * radius;
double ratio = max_circumference / circumference;
size_t n_pels = (size_t) width * height * bands;
size_t i;
guint *q;
q = b + (guint *) VIPS_IMAGE_ADDR( hough->out, 0, 0 );
for( i = 0; i < n_pels; i += bands )
q[i] *= ratio;
}
}
static int
vips_hough_circle_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsStatistic *statistic = (VipsStatistic *) object;
VipsHoughCircle *hough_circle = (VipsHoughCircle *) object;
int range = hough_circle->max_radius - hough_circle->min_radius;
if( range <= 0 ) {
vips_error( class->nickname,
"%s", _( "parameters out of range" ) );
return( -1 );
}
hough_circle->width = statistic->in->Xsize / hough_circle->scale;
hough_circle->height = statistic->in->Ysize / hough_circle->scale;
hough_circle->bands = 1 + range / hough_circle->scale;
if( VIPS_OBJECT_CLASS( vips_hough_circle_parent_class )->
build( object ) )
return( -1 );
vips_hough_circle_normalise( hough_circle );
return( 0 );
}
static int
vips_hough_circle_init_accumulator( VipsHough *hough, VipsImage *accumulator )
{
VipsHoughCircle *hough_circle = (VipsHoughCircle *) hough;
vips_image_init_fields( accumulator,
hough_circle->width, hough_circle->height, hough_circle->bands,
VIPS_FORMAT_UINT, VIPS_CODING_NONE,
VIPS_INTERPRETATION_MATRIX,
1.0, 1.0 );
return( 0 );
}
/* Vote endpoints, with clip.
*/
static void
vips_hough_circle_vote_endpoints_clip( VipsImage *image,
int y, int x1, int x2, int quadrant, void *client )
{
int r = *((int *) client);
int b = image->Bands;
if( y >= 0 &&
y < image->Ysize ) {
guint *line = (guint *) VIPS_IMAGE_ADDR( image, 0, y ) + r;
if( x1 >=0 &&
x1 < image->Xsize )
line[x1 * b] += 1;
if( x2 >=0 &&
x2 < image->Xsize )
line[x2 * b] += 1;
}
}
/* Vote endpoints, no clip.
*/
static void
vips_hough_circle_vote_endpoints_noclip( VipsImage *image,
int y, int x1, int x2, int quadrant, void *client )
{
int r = *((int *) client);
guint *line = (guint *) VIPS_IMAGE_ADDR( image, 0, y ) + r;
int b = image->Bands;
line[x1 * b] += 1;
line[x2 * b] += 1;
}
/* Cast votes for all possible circles passing through x, y.
*/
static void
vips_hough_circle_vote( VipsHough *hough, VipsImage *accumulator, int x, int y )
{
VipsHoughCircle *hough_circle = (VipsHoughCircle *) hough;
int min_radius = hough_circle->min_radius;
int cx = x / hough_circle->scale;
int cy = y / hough_circle->scale;
int rb;
g_assert( hough_circle->max_radius - min_radius >= 0 );
for( rb = 0; rb < hough_circle->bands; rb++ ) {
/* r needs to be in scaled down image space.
*/
int r = rb + min_radius / hough_circle->scale;
VipsDrawScanline draw_scanline;
if( cx - r >= 0 &&
cx + r < accumulator->Xsize &&
cy - r >= 0 &&
cy + r < accumulator->Ysize )
draw_scanline = vips_hough_circle_vote_endpoints_noclip;
else
draw_scanline = vips_hough_circle_vote_endpoints_clip;
vips__draw_circle_direct( accumulator,
cx, cy, r, draw_scanline, &rb );
}
}
static void
vips_hough_circle_class_init( VipsHoughClass *class )
{
GObjectClass *gobject_class = (GObjectClass *) class;
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsHoughClass *hclass = (VipsHoughClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "hough_circle";
object_class->description = _( "find hough circle transform" );
object_class->build = vips_hough_circle_build;
hclass->init_accumulator = vips_hough_circle_init_accumulator;
hclass->vote = vips_hough_circle_vote;
VIPS_ARG_INT( class, "scale", 119,
_( "Scale" ),
_( "Scale down dimensions by this factor" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsHoughCircle, scale ),
1, 100000, 3 );
VIPS_ARG_INT( class, "min_radius", 120,
_( "Min radius" ),
_( "Smallest radius to search for" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsHoughCircle, min_radius ),
1, 100000, 10 );
VIPS_ARG_INT( class, "max_radius", 121,
_( "Max radius" ),
_( "Largest radius to search for" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsHoughCircle, max_radius ),
1, 100000, 20 );
}
static void
vips_hough_circle_init( VipsHoughCircle *hough_circle )
{
hough_circle->scale = 1;
hough_circle->min_radius = 10;
hough_circle->max_radius = 20;
}
/**
* vips_hough_circle: (method)
* @in: input image
* @out: (out): output image
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @scale: scale down dimensions by this much
* * @min_radius: smallest radius to search for
* * @max_radius: largest radius to search for
*
* Find the circular Hough transform of an image. @in must be one band, with
* non-zero pixels for image edges. @out is three-band, with the third channel
* representing the detected circle radius. The operation scales the number of
* votes by circle circumference so circles of differing size are given equal
* weight.
*
* The output pixel at (x, y, band) is the strength of the circle centred on
* (x, y) and with radius (band).
*
* Use @max_radius and @min_radius to set the range of radii to search for.
*
* Use @scale to set how @in coordinates are scaled to @out coordinates. A
* @scale of 3, for example, will make @out 1/3rd of the width and height of
* @in, and reduce the number of radii tested (and hence the number of bands
* int @out) by a factor of three as well.
*
* See also: vips_hough_line().
*
* Returns: 0 on success, -1 on error
*/
int
vips_hough_circle( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "hough_circle", ap, in, out );
va_end( ap );
return( result );
}
|