seems to work
This commit is contained in:
parent
ae54f35ed1
commit
cb37089c9b
@ -162,7 +162,7 @@ vips_hough_circle_vote_point( VipsImage *image, int x, int y, void *client )
|
|||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
vips_hough_circle_vote_endpoints_clip( VipsImage *image,
|
vips_hough_circle_vote_endpoints_clip( VipsImage *image,
|
||||||
int y, int x1, int x2, void *client )
|
int y, int x1, int x2, int quadrant, void *client )
|
||||||
{
|
{
|
||||||
if( y >= 0 &&
|
if( y >= 0 &&
|
||||||
y < image->Ysize ) {
|
y < image->Ysize ) {
|
||||||
@ -179,7 +179,7 @@ vips_hough_circle_vote_endpoints_clip( VipsImage *image,
|
|||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
vips_hough_circle_vote_endpoints_noclip( VipsImage *image,
|
vips_hough_circle_vote_endpoints_noclip( VipsImage *image,
|
||||||
int y, int x1, int x2, void *client )
|
int y, int x1, int x2, int quadrant, void *client )
|
||||||
{
|
{
|
||||||
vips_hough_circle_vote_point( image, x1, y, client );
|
vips_hough_circle_vote_point( image, x1, y, client );
|
||||||
vips_hough_circle_vote_point( image, x2, y, client );
|
vips_hough_circle_vote_point( image, x2, y, client );
|
||||||
|
@ -87,10 +87,10 @@ vips__draw_circle_direct( VipsImage *image, int cx, int cy, int r,
|
|||||||
d = 3 - 2 * r;
|
d = 3 - 2 * r;
|
||||||
|
|
||||||
for( x = 0; x < y; x++ ) {
|
for( x = 0; x < y; x++ ) {
|
||||||
draw_scanline( image, cy + y, cx - x, cx + x, client );
|
draw_scanline( image, cy + y, cx - x, cx + x, 0, client );
|
||||||
draw_scanline( image, cy - y, cx - x, cx + x, client );
|
draw_scanline( image, cy - y, cx - x, cx + x, 1, client );
|
||||||
draw_scanline( image, cy + x, cx - y, cx + y, client );
|
draw_scanline( image, cy + x, cx - y, cx + y, 2, client );
|
||||||
draw_scanline( image, cy - x, cx - y, cx + y, client );
|
draw_scanline( image, cy - x, cx - y, cx + y, 3, client );
|
||||||
|
|
||||||
if( d < 0 )
|
if( d < 0 )
|
||||||
d += 4 * x + 6;
|
d += 4 * x + 6;
|
||||||
@ -101,10 +101,10 @@ vips__draw_circle_direct( VipsImage *image, int cx, int cy, int r,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if( x == y ) {
|
if( x == y ) {
|
||||||
draw_scanline( image, cy + y, cx - x, cx + x, client );
|
draw_scanline( image, cy + y, cx - x, cx + x, 0, client );
|
||||||
draw_scanline( image, cy - y, cx - x, cx + x, client );
|
draw_scanline( image, cy - y, cx - x, cx + x, 1, client );
|
||||||
draw_scanline( image, cy + x, cx - y, cx + y, client );
|
draw_scanline( image, cy + x, cx - y, cx + y, 2, client );
|
||||||
draw_scanline( image, cy - x, cx - y, cx + y, client );
|
draw_scanline( image, cy - x, cx - y, cx + y, 3, client );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +127,7 @@ vips_draw_circle_draw_point( VipsImage *image, int x, int y, void *client )
|
|||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
vips_draw_circle_draw_endpoints_clip( VipsImage *image,
|
vips_draw_circle_draw_endpoints_clip( VipsImage *image,
|
||||||
int y, int x1, int x2, void *client )
|
int y, int x1, int x2, int quadrant, void *client )
|
||||||
{
|
{
|
||||||
if( y >= 0 &&
|
if( y >= 0 &&
|
||||||
y < image->Ysize ) {
|
y < image->Ysize ) {
|
||||||
@ -144,7 +144,7 @@ vips_draw_circle_draw_endpoints_clip( VipsImage *image,
|
|||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
vips_draw_circle_draw_endpoints_noclip( VipsImage *image,
|
vips_draw_circle_draw_endpoints_noclip( VipsImage *image,
|
||||||
int y, int x1, int x2, void *client )
|
int y, int x1, int x2, int quadrant, void *client )
|
||||||
{
|
{
|
||||||
vips_draw_circle_draw_point( image, x1, y, client );
|
vips_draw_circle_draw_point( image, x1, y, client );
|
||||||
vips_draw_circle_draw_point( image, x2, y, client );
|
vips_draw_circle_draw_point( image, x2, y, client );
|
||||||
@ -154,7 +154,7 @@ vips_draw_circle_draw_endpoints_noclip( VipsImage *image,
|
|||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
vips_draw_circle_draw_scanline( VipsImage *image,
|
vips_draw_circle_draw_scanline( VipsImage *image,
|
||||||
int y, int x1, int x2, void *client )
|
int y, int x1, int x2, int quadrant, void *client )
|
||||||
{
|
{
|
||||||
VipsPel *ink = (VipsPel *) client;
|
VipsPel *ink = (VipsPel *) client;
|
||||||
int psize = VIPS_IMAGE_SIZEOF_PEL( image );
|
int psize = VIPS_IMAGE_SIZEOF_PEL( image );
|
||||||
|
@ -188,7 +188,7 @@ int vips__draw_mask_direct( VipsImage *image, VipsImage *mask,
|
|||||||
typedef void (*VipsDrawPoint)( VipsImage *image,
|
typedef void (*VipsDrawPoint)( VipsImage *image,
|
||||||
int x, int y, void *client );
|
int x, int y, void *client );
|
||||||
typedef void (*VipsDrawScanline)( VipsImage *image,
|
typedef void (*VipsDrawScanline)( VipsImage *image,
|
||||||
int y, int x1, int x2, void *client );
|
int y, int x1, int x2, int quadrant, void *client );
|
||||||
|
|
||||||
void vips__draw_line_direct( VipsImage *image, int x1, int y1, int x2, int y2,
|
void vips__draw_line_direct( VipsImage *image, int x1, int y1, int x2, int y2,
|
||||||
VipsDrawPoint draw_point, void *client );
|
VipsDrawPoint draw_point, void *client );
|
||||||
|
@ -50,11 +50,12 @@
|
|||||||
typedef struct _Seed {
|
typedef struct _Seed {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
int radius;
|
int r;
|
||||||
|
|
||||||
/* Set when we know this seed cannot contribute any more values.
|
/* Bits saying which octant can still grow. When they are all zero, the
|
||||||
|
* seed is dead.
|
||||||
*/
|
*/
|
||||||
gboolean dead;
|
int octant_mask;
|
||||||
} Seed;
|
} Seed;
|
||||||
|
|
||||||
typedef struct _VipsNearest {
|
typedef struct _VipsNearest {
|
||||||
@ -88,14 +89,118 @@ vips_nearest_finalize( GObject *gobject )
|
|||||||
G_OBJECT_CLASS( vips_nearest_parent_class )->finalize( gobject );
|
G_OBJECT_CLASS( vips_nearest_parent_class )->finalize( gobject );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct _Circle;
|
||||||
|
typedef void (*VipsNearestPixel)( struct _Circle *circle,
|
||||||
|
int x, int y, int r, int octant );
|
||||||
|
|
||||||
|
typedef struct _Circle {
|
||||||
|
VipsNearest *nearest;
|
||||||
|
Seed *seed;
|
||||||
|
int octant_mask;
|
||||||
|
VipsNearestPixel nearest_pixel;
|
||||||
|
} Circle;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
vips_nearest_seed( VipsNearest *nearest, Seed *seed )
|
vips_nearest_pixel( Circle *circle, int x, int y, int r, int octant )
|
||||||
{
|
{
|
||||||
|
guint *p;
|
||||||
|
|
||||||
|
if( (circle->seed->octant_mask & (1 << octant)) == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
vips__draw_circle_direct( VipsImage *image, int cx, int cy, int r,
|
p = (guint *) VIPS_IMAGE_ADDR( circle->nearest->distance, x, y );
|
||||||
VipsDrawScanline draw_scanline, void *client )
|
|
||||||
|
|
||||||
|
if( p[0] == 0 ||
|
||||||
|
p[0] > r ) {
|
||||||
|
VipsMorphology *morphology = VIPS_MORPHOLOGY( circle->nearest );
|
||||||
|
VipsImage *in = morphology->in;
|
||||||
|
int ps = VIPS_IMAGE_SIZEOF_PEL( in );
|
||||||
|
VipsPel *pi = VIPS_IMAGE_ADDR( in,
|
||||||
|
circle->seed->x, circle->seed->y );
|
||||||
|
VipsPel *qi = VIPS_IMAGE_ADDR( circle->nearest->out,
|
||||||
|
x, y );
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
p[0] = r;
|
||||||
|
circle->octant_mask |= 1 << octant;
|
||||||
|
|
||||||
|
for( i = 0; i < ps; i++ )
|
||||||
|
qi[i] = pi[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
vips_nearest_pixel_clip( Circle *circle, int x, int y, int r, int octant )
|
||||||
|
{
|
||||||
|
if( (circle->seed->octant_mask & (1 << octant)) == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
if( y >= 0 &&
|
||||||
|
y < circle->nearest->distance->Ysize &&
|
||||||
|
x >= 0 &&
|
||||||
|
x < circle->nearest->distance->Xsize )
|
||||||
|
vips_nearest_pixel( circle, x, y, r, octant );
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
vips_nearest_scanline( VipsImage *image,
|
||||||
|
int y, int x1, int x2, int quadrant, void *client )
|
||||||
|
{
|
||||||
|
Circle *circle = (Circle *) client;
|
||||||
|
|
||||||
|
circle->nearest_pixel( circle, x1, y, circle->seed->r, quadrant );
|
||||||
|
circle->nearest_pixel( circle, x2, y, circle->seed->r, quadrant + 4 );
|
||||||
|
|
||||||
|
/* We have to do one point back as well, or we'll leave gaps at
|
||||||
|
* around 45 degrees.
|
||||||
|
*/
|
||||||
|
if( quadrant == 0 ) {
|
||||||
|
circle->nearest_pixel( circle,
|
||||||
|
x1, y - 1, circle->seed->r - 1, quadrant );
|
||||||
|
circle->nearest_pixel( circle,
|
||||||
|
x2, y - 1, circle->seed->r - 1, quadrant + 4 );
|
||||||
|
}
|
||||||
|
else if( quadrant == 1 ) {
|
||||||
|
circle->nearest_pixel( circle,
|
||||||
|
x1, y + 1, circle->seed->r - 1, quadrant );
|
||||||
|
circle->nearest_pixel( circle,
|
||||||
|
x2, y + 1, circle->seed->r - 1, quadrant + 4 );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
circle->nearest_pixel( circle,
|
||||||
|
x1 + 1, y, circle->seed->r - 1, quadrant );
|
||||||
|
circle->nearest_pixel( circle,
|
||||||
|
x2 - 1, y, circle->seed->r - 1, quadrant + 4 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
vips_nearest_grow_seed( VipsNearest *nearest, Seed *seed )
|
||||||
|
{
|
||||||
|
Circle circle;
|
||||||
|
|
||||||
|
circle.nearest = nearest;
|
||||||
|
circle.seed = seed;
|
||||||
|
circle.octant_mask = 0;
|
||||||
|
|
||||||
|
if( seed->x - seed->r >= 0 &&
|
||||||
|
seed->x + seed->r < nearest->distance->Xsize &&
|
||||||
|
seed->y - seed->r >= 0 &&
|
||||||
|
seed->y + seed->r < nearest->distance->Ysize )
|
||||||
|
circle.nearest_pixel = vips_nearest_pixel;
|
||||||
|
else
|
||||||
|
circle.nearest_pixel = vips_nearest_pixel_clip;
|
||||||
|
|
||||||
|
vips__draw_circle_direct( nearest->distance,
|
||||||
|
seed->x, seed->y, seed->r, vips_nearest_scanline, &circle );
|
||||||
|
|
||||||
|
/* Update the action_mask for this seed. Next time, we can skip any
|
||||||
|
* octants where we failed to act this time.
|
||||||
|
*/
|
||||||
|
seed->octant_mask = circle.octant_mask;
|
||||||
|
|
||||||
|
seed->r += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -104,20 +209,16 @@ vips_nearest_build( VipsObject *object )
|
|||||||
VipsMorphology *morphology = VIPS_MORPHOLOGY( object );
|
VipsMorphology *morphology = VIPS_MORPHOLOGY( object );
|
||||||
VipsNearest *nearest = (VipsNearest *) object;
|
VipsNearest *nearest = (VipsNearest *) object;
|
||||||
VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );
|
VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );
|
||||||
|
VipsImage *in = morphology->in;
|
||||||
|
|
||||||
VipsImage *in;
|
|
||||||
int ps;
|
int ps;
|
||||||
int x, y, i;
|
int x, y, i;
|
||||||
|
|
||||||
if( VIPS_OBJECT_CLASS( vips_nearest_parent_class )->build( object ) )
|
if( VIPS_OBJECT_CLASS( vips_nearest_parent_class )->build( object ) )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
|
|
||||||
in = morphology->in;
|
if( vips_image_wio_input( in ) )
|
||||||
|
|
||||||
if( vips_image_decode( in, &t[0] ) ||
|
|
||||||
vips_image_wio_input( t[0] ) )
|
|
||||||
return( -1 );
|
return( -1 );
|
||||||
in = t[0];
|
|
||||||
|
|
||||||
ps = VIPS_IMAGE_SIZEOF_PEL( in );
|
ps = VIPS_IMAGE_SIZEOF_PEL( in );
|
||||||
nearest->seeds = g_array_new( FALSE, FALSE, sizeof( Seed ) );
|
nearest->seeds = g_array_new( FALSE, FALSE, sizeof( Seed ) );
|
||||||
@ -139,41 +240,44 @@ vips_nearest_build( VipsObject *object )
|
|||||||
Seed, nearest->seeds->len - 1 );
|
Seed, nearest->seeds->len - 1 );
|
||||||
seed->x = x;
|
seed->x = x;
|
||||||
seed->y = y;
|
seed->y = y;
|
||||||
seed->radius = 0;
|
seed->r = 1;
|
||||||
seed->dead = FALSE;
|
seed->octant_mask = 255;
|
||||||
}
|
}
|
||||||
|
|
||||||
p += ps;
|
p += ps;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf( "found %d seeds\n", nearest->seeds->len );
|
|
||||||
#endif /*DEBUG*/
|
|
||||||
|
|
||||||
/* Create the output and distance images in memory.
|
/* Create the output and distance images in memory.
|
||||||
*/
|
*/
|
||||||
g_object_set( object, "out", vips_image_new_memory(), NULL );
|
|
||||||
g_object_set( object, "distance", vips_image_new_memory(), NULL );
|
g_object_set( object, "distance", vips_image_new_memory(), NULL );
|
||||||
|
|
||||||
if( vips_black( &t[1], in->Xsize, in->Ysize, NULL ) ||
|
if( vips_black( &t[1], in->Xsize, in->Ysize, NULL ) ||
|
||||||
vips_cast( t[1], &t[2], VIPS_FORMAT_UINT, NULL ) ||
|
vips_cast( t[1], &t[2], VIPS_FORMAT_UINT, NULL ) ||
|
||||||
vips_image_write( t[2], nearest->distance ) )
|
vips_image_write( t[2], nearest->distance ) )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
|
|
||||||
|
g_object_set( object, "out", vips_image_new_memory(), NULL );
|
||||||
if( vips_image_write( in, nearest->out ) )
|
if( vips_image_write( in, nearest->out ) )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
|
|
||||||
while( nearest->seeds->len > 0 ) {
|
while( nearest->seeds->len > 0 ) {
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf( "looping for %d seeds ...\n", nearest->seeds->len );
|
||||||
|
#endif /*DEBUG*/
|
||||||
|
|
||||||
|
/* Grow all seeds by one pixel.
|
||||||
|
*/
|
||||||
for( i = 0; i < nearest->seeds->len; i++ )
|
for( i = 0; i < nearest->seeds->len; i++ )
|
||||||
vips_nearest_seed( nearest,
|
vips_nearest_grow_seed( nearest,
|
||||||
&g_array_index( nearest->seeds, Seed, i ) );
|
&g_array_index( nearest->seeds, Seed, i ) );
|
||||||
|
|
||||||
|
/* Remove dead seeds.
|
||||||
|
*/
|
||||||
i = 0;
|
i = 0;
|
||||||
while( i < nearest->seeds->len ) {
|
while( i < nearest->seeds->len ) {
|
||||||
Seed *seed = &g_array_index( nearest->seeds, Seed, i );
|
Seed *seed = &g_array_index( nearest->seeds, Seed, i );
|
||||||
|
|
||||||
if( seed->dead )
|
if( seed->octant_mask == 0 )
|
||||||
g_array_remove_index_fast( nearest->seeds, i );
|
g_array_remove_index_fast( nearest->seeds, i );
|
||||||
else
|
else
|
||||||
i += 1;
|
i += 1;
|
||||||
@ -224,7 +328,7 @@ vips_nearest_init( VipsNearest *nearest )
|
|||||||
*
|
*
|
||||||
* Optional arguments:
|
* Optional arguments:
|
||||||
*
|
*
|
||||||
* * @distance: distance to nearest image pixel
|
* * @distance: output image of distance to nearest image pixel
|
||||||
*
|
*
|
||||||
* Flood outwards from every non-zero pixel in @in, setting pixels in @distance
|
* Flood outwards from every non-zero pixel in @in, setting pixels in @distance
|
||||||
* and @value.
|
* and @value.
|
||||||
|
Loading…
Reference in New Issue
Block a user