/** * im_scaleps: * @in: input image * @out: output image * * Scale a power spectrum. Transform with log10(1.0 + pow(x, 0.25)) + .5, * then scale so max == 255. * * See also: im_scale(). * * Returns: 0 on success, -1 on error */ int im_scaleps( IMAGE *in, IMAGE *out ) { IMAGE *t[4]; double mx; double scale; if( im_open_local_array( out, t, 4, "im_scaleps-1", "p" ) || im_max( in, &mx ) ) return( -1 ); if( mx <= 0.0 ) /* Range of zero: just return black. */ return( im_black( out, in->Xsize, in->Ysize, in->Bands ) ); scale = 255.0 / log10( 1.0 + pow( mx, .25 ) ); /* Transform! */ if( im_powtra( in, t[0], 0.25 ) || im_lintra( 1.0, t[0], 1.0, t[1] ) || im_log10tra( t[1], t[2] ) || im_lintra( scale, t[2], 0.0, t[3] ) || im_clip2fmt( t[3], out, IM_BANDFMT_UCHAR ) ) return( -1 ); return( 0 ); }
/* Call im_black via arg vector. */ static int black_vec( im_object *argv ) { int xs = *((int *) argv[1]); int ys = *((int *) argv[2]); int bands = *((int *) argv[3]); return( im_black( argv[0], xs, ys, bands ) ); }
/** * im_label_regions: * @test: image to test * @mask: write labelled regions here * @segments: return number of regions here * * im_label_regions() repeatedly scans @test for regions of 4-connected pixels * with the same pixel value. Every time a region is discovered, those * pixels are marked in @mask with a unique serial number. Once all pixels * have been labelled, the operation returns, setting @segments to the number * of discrete regions which were detected. * * @mask is always a 1-band %IM_BANDFMT_UINT image of the same dimensions as * @test. * * This operation is useful for, for example, blob counting. You can use the * morphological operators to detect and isolate a series of objects, then use * im_label_regions() to number them all. * * Use im_histindexed() to (for example) find blob coordinates. * * See also: im_histindexed() * * Returns: 0 on success, -1 on error. */ int im_label_regions( IMAGE *test, IMAGE *mask, int *segments ) { IMAGE *t[2]; int serial; int *m; int x, y; /* Create the zero mask image. */ if( im_open_local_array( mask, t, 2, "im_label_regions", "p" ) || im_black( t[0], test->Xsize, test->Ysize, 1 ) || im_clip2fmt( t[0], t[1], IM_BANDFMT_INT ) ) return( -1 ); /* Search the mask image, flooding as we find zero pixels. */ if( im_rwcheck( t[1] ) ) return( -1 ); serial = 0; m = (int *) t[1]->data; for( y = 0; y < test->Ysize; y++ ) { for( x = 0; x < test->Xsize; x++ ) { if( !m[x] ) { /* if( im_flood_other_old( t[1], test, x, y, serial ) ) */ if( im_flood_other( test, t[1], x, y, serial, NULL ) ) // if( im_flood_other_old( t[1], test, // x, y, serial ) ) return( -1 ); serial += 1; } } m += test->Xsize; } /* Copy result to mask. */ if( im_copy( t[1], mask ) ) return( -1 ); if( segments ) *segments = serial; return( 0 ); }
/** * im_zerox: * @in: input image * @out: output image * @sign: detect positive or negative zero crossings * * im_zerox() detects the positive or negative zero crossings @in, * depending on @sign. If @sign is -1, negative zero crossings are returned, * if @sign is 1, positive zero crossings are returned. * * The output image is byte with zero crossing set to 255 and all other values * set to zero. Input can have any number of channels, and be any non-complex * type. * * See also: im_conv(), im_rot90. * * Returns: 0 on success, -1 on error */ int im_zerox( IMAGE *in, IMAGE *out, int sign ) { IMAGE *t1; if( sign != -1 && sign != 1 ) { im_error( "im_zerox", "%s", _( "flag not -1 or 1" ) ); return( -1 ); } if( in->Xsize < 2 ) { im_error( "im_zerox", "%s", _( "image too narrow" ) ); return( -1 ); } if( !(t1 = im_open_local( out, "im_zerox" , "p" )) || im_piocheck( in, t1 ) || im_check_uncoded( "im_zerox", in ) || im_check_noncomplex( "im_zerox", in ) ) return( -1 ); if( vips_bandfmt_isuint( in->BandFmt ) ) /* Unsigned type, therefore there will be no zero-crossings. */ return( im_black( out, in->Xsize, in->Ysize, in->Bands ) ); /* Force output to be BYTE. Output is narrower than input by 1 pixel. */ if( im_cp_desc( t1, in ) ) return( -1 ); t1->BandFmt = IM_BANDFMT_UCHAR; t1->Xsize -= 1; /* Set hints - THINSTRIP is ok with us. */ if( im_demand_hint( t1, IM_THINSTRIP, NULL ) ) return( -1 ); /* Generate image. */ if( im_generate( t1, im_start_one, zerox_gen, im_stop_one, in, GINT_TO_POINTER( sign ) ) ) return( -1 ); /* Now embed it in a larger image. */ if( im_embed( t1, out, 0, 0, 0, in->Xsize, in->Ysize ) ) return( -1 ); return( 0 ); }
/* Find stats on an area of an IMAGE ... consider only pixels for which the * mask is true. */ static DOUBLEMASK * find_image_stats( IMAGE *in, IMAGE *mask, Rect *area ) { DOUBLEMASK *stats; IMAGE *t[4]; gint64 count; /* Extract area, build black image, mask out pixels we want. */ if( im_open_local_array( in, t, 4, "find_image_stats", "p" ) || extract_rect( in, t[0], area ) || im_black( t[1], t[0]->Xsize, t[0]->Ysize, t[0]->Bands ) || im_clip2fmt( t[1], t[2], t[0]->BandFmt ) || im_ifthenelse( mask, t[0], t[2], t[3] ) ) return( NULL ); /* Get stats from masked image. */ if( !(stats = local_mask( in, im_stats( t[3] ) )) ) return( NULL ); /* Number of non-zero pixels in mask. */ if( count_nonzero( mask, &count ) ) return( NULL ); /* And scale masked average to match. */ stats->coeff[4] *= (double) count / ((double) mask->Xsize * mask->Ysize); /* Yuk! Zap the deviation column with the pixel count. Used later to * determine if this is likely to be a significant overlap. */ stats->coeff[5] = count; #ifdef DEBUG if( count == 0 ) im_warn( "global_balance", _( "empty overlap!" ) ); #endif /*DEBUG*/ return( stats ); }
/* Transform a 1 band image with vips's built-in fft routine. */ static int fwfft1( IMAGE *dummy, IMAGE *in, IMAGE *out ) { int size = in->Xsize * in->Ysize; int bpx = im_ispoweroftwo( in->Xsize ); int bpy = im_ispoweroftwo( in->Ysize ); float *buf, *q, *p1, *p2; int x, y; /* Buffers for real and imaginary parts. */ IMAGE *real = im_open_local( dummy, "fwfft1:1", "t" ); IMAGE *imag = im_open_local( dummy, "fwfft1:2", "t" ); /* Temporaries. */ IMAGE *t1 = im_open_local( dummy, "fwfft1:3", "p" ); if( !real || !imag || !t1 ) return( -1 ); if( im_pincheck( in ) || im_outcheck( out ) ) return( -1 ); if( in->Coding != IM_CODING_NONE || in->Bands != 1 || im_iscomplex( in ) ) { im_error( "im_fwfft", _( "one band non-complex uncoded only" ) ); return( -1 ); } if( !bpx || !bpy ) { im_error( "im_fwfft", _( "sides must be power of 2" ) ); return( -1 ); } /* Make sure we have a float input image. */ if( im_clip2f( in, real ) ) return( -1 ); /* Make a buffer of 0 floats of the same size for the imaginary part. */ if( im_black( t1, in->Xsize, in->Ysize, 1 ) ) return( -1 ); if( im_clip2f( t1, imag ) ) return( -1 ); /* Transform! */ if( im__fft_sp( (float *) real->data, (float *) imag->data, bpx - 1, bpy - 1 ) ) { im_error( "im_fwfft", _( "fft_sp failed" ) ); return( -1 ); } /* WIO to out. */ if( im_cp_desc( out, in ) ) return( -1 ); out->Bbits = IM_BBITS_COMPLEX; out->BandFmt = IM_BANDFMT_COMPLEX; if( im_setupout( out ) ) return( -1 ); if( !(buf = (float *) IM_ARRAY( dummy, IM_IMAGE_SIZEOF_LINE( out ), PEL )) ) return( -1 ); /* Gather together real and imag parts. We have to normalise output! */ for( p1 = (float *) real->data, p2 = (float *) imag->data, y = 0; y < out->Ysize; y++ ) { q = buf; for( x = 0; x < out->Xsize; x++ ) { q[0] = *p1++ / size; q[1] = *p2++ / size; q += 2; } if( im_writeline( y, out, (PEL *) buf ) ) return( -1 ); } return( 0 ); }
/* The main part of the benchmark ... transform labq to labq. Chain several of * these together to get a CPU-bound operation. */ static int benchmark( IMAGE *in, IMAGE *out ) { IMAGE *t[18]; double one[3] = { 1.0, 1.0, 1.0 }; double zero[3] = { 0.0, 0.0, 0.0 }; double darken[3] = { 1.0 / 1.18, 1.0, 1.0 }; double whitepoint[3] = { 1.06, 1.0, 1.01 }; double shadow[3] = { -2, 0, 0 }; double white[3] = { 100, 0, 0 }; DOUBLEMASK *d652d50 = im_create_dmaskv( "d652d50", 3, 3, 1.13529, -0.0604663, -0.0606321, 0.0975399, 0.935024, -0.0256156, -0.0336428, 0.0414702, 0.994135 ); im_add_close_callback( out, (im_callback_fn) im_free_dmask, d652d50, NULL ); return( /* Set of descriptors for this operation. */ im_open_local_array( out, t, 18, "im_benchmark", "p" ) || /* Unpack to float. */ im_LabQ2Lab( in, t[0] ) || /* Crop 100 pixels off all edges. */ im_extract_area( t[0], t[1], 100, 100, t[0]->Xsize - 200, t[0]->Ysize - 200 ) || /* Shrink by 10%, bilinear interp. */ im_affinei_all( t[1], t[2], vips_interpolate_bilinear_static(), 0.9, 0, 0, 0.9, 0, 0 ) || /* Find L ~= 100 areas (white surround). */ im_extract_band( t[2], t[3], 0 ) || im_moreconst( t[3], t[4], 99 ) || /* Adjust white point and shadows. */ im_lintra_vec( 3, darken, t[2], zero, t[5] ) || im_Lab2XYZ( t[5], t[6] ) || im_recomb( t[6], t[7], d652d50 ) || im_lintra_vec( 3, whitepoint, t[7], zero, t[8] ) || im_lintra( 1.5, t[8], 0.0, t[9] ) || im_XYZ2Lab( t[9], t[10] ) || im_lintra_vec( 3, one, t[10], shadow, t[11] ) || /* Make a solid white image. */ im_black( t[12], t[4]->Xsize, t[4]->Ysize, 3 ) || im_lintra_vec( 3, zero, t[12], white, t[13] ) || /* Reattach border. */ im_ifthenelse( t[4], t[13], t[11], t[14] ) || /* Sharpen. */ im_Lab2LabQ( t[14], t[15] ) || im_sharpen( t[15], out, 11, 2.5, 40, 20, 0.5, 1.5 ) ); }