File: spcor.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (381 lines) | stat: -rw-r--r-- 9,323 bytes parent folder | download
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* spcor
 *
 * Copyright: 1990, N. Dessipris; 2006, 2007 Nottingham Trent University.
 *
 *
 * Author: Nicos Dessipris
 * Written on: 02/05/1990
 * Modified on : 
 * 20/2/95 JC
 *	- updated
 *	- ANSIfied, a little
 * 21/2/95 JC
 *	- rewritten
 *	- partialed 
 *	- speed-ups
 *	- new correlation coefficient (see above), from Niblack "An
 *	  Introduction to Digital Image Processing", Prentice/Hall, pp 138.
 * 4/9/97 JC
 *	- now does short/ushort as well
 * 13/2/03 JC
 *	- oops, could segv for short images
 * 14/4/04 JC
 *	- sets Xoffset / Yoffset
 * 8/3/06 JC
 *	- use im_embed() with edge stretching on the input, not the output
 *
 * 2006-10-24 tcv
 *      - add im_spcor2
 *
 * 2007-11-12 tcv
 *      - make im_spcor a wrapper selecting either im__spcor or im__spcor2
 * 2008-09-09 JC
 * 	- roll back the windowed version for now, it has some tile edge effects
 * 3/2/10
 * 	- gtkdoc
 * 	- cleanups
 * 7/11/13
 * 	- redone as a class
 * 8/4/15
 * 	- avoid /0 for constant reference or zero image
 */

/*

    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 <glib/gi18n-lib.h>

#include <stdio.h>
#include <math.h>

#include <vips/vips.h>

#include "pconvolution.h"
#include "correlation.h"

typedef struct _VipsSpcor {
	VipsCorrelation parent_instance;

	/* Per-band mean of ref images.
	 */
	double *rmean;

	/* Per band sqrt(sumij (ref(i,j)-mean(ref))^2) 
	 */
	double *c1;
} VipsSpcor; 

typedef VipsCorrelationClass VipsSpcorClass;

G_DEFINE_TYPE( VipsSpcor, vips_spcor, VIPS_TYPE_CORRELATION );

static int
vips_spcor_pre_generate( VipsCorrelation *correlation )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( correlation );
	VipsSpcor *spcor = (VipsSpcor *) correlation;
	VipsImage *ref = correlation->ref_ready;
	int bands = ref->Bands;
	VipsImage **b = (VipsImage **) 
		vips_object_local_array( VIPS_OBJECT( spcor ), bands );
	VipsImage **t = (VipsImage **) 
		vips_object_local_array( VIPS_OBJECT( spcor ), 2 );
	VipsImage **b2 = (VipsImage **) 
		vips_object_local_array( VIPS_OBJECT( spcor ), bands );

	int i; 
	double *offset;
	double *scale;

	if( vips_check_noncomplex( class->nickname, ref ) )
		return( -1 ); 

	/* Per-band mean.
	 */
	if( !(spcor->rmean = VIPS_ARRAY( spcor, bands, double )) ||
		!(spcor->c1 = VIPS_ARRAY( spcor, bands, double )) )
		return( -1 ); 
	for( i = 0; i < bands; i++ ) 
		if( vips_extract_band( ref, &b[i], i, NULL ) ||
			vips_avg( b[i], &spcor->rmean[i], NULL ) )
			return( -1 );

	/* Per band sqrt(sumij (ref(i,j)-mean(ref))^2) 
	 */
	if( !(offset = VIPS_ARRAY( spcor, bands, double )) ||
		!(scale = VIPS_ARRAY( spcor, bands, double )) )
		return( -1 ); 
	for( i = 0; i < bands; i++ ) {
		offset[i] = -spcor->rmean[i];
		scale[i] = 1.0;
	}
	if( vips_linear( ref, &t[0], scale, offset, bands, NULL ) ||
		vips_multiply( t[0], t[0], &t[1], NULL ) )
		return( -1 ); 
	for( i = 0; i < bands; i++ ) 
		if( vips_extract_band( t[1], &b2[i], i, NULL ) ||
			vips_avg( b2[i], &spcor->c1[i], NULL ) )
			return( -1 );
	for( i = 0; i < bands; i++ ) {
		spcor->c1[i] *= ref->Xsize * ref->Ysize;
		spcor->c1[i] = sqrt( spcor->c1[i] );
	}

	return( 0 );
}

#define LOOP( IN ) { \
	IN *r1 = ((IN *) ref->data) + b; \
	IN *p1 = ((IN *) p) + b; \
	int in_lsk = lsk / sizeof( IN ); \
	IN *r1a; \
	IN *p1a; \
 	\
	/* Mean of area of in corresponding to ref. \
	 */ \
	p1a = p1; \
	sum1 = 0.0; \
	for( j = 0; j < ref->Ysize; j++ ) { \
		for( i = 0; i < sz; i += bands ) \
			sum1 += p1a[i]; \
		p1a += in_lsk;  \
	} \
	imean = sum1 / VIPS_IMAGE_N_PELS( ref ); \
 	\
	/* Calculate sum-of-squares-of-differences for this window on \
	 * in, and also sum-of-products-of-differences from mean. \
	 */ \
	p1a = p1; \
	r1a = r1; \
	sum2 = 0.0; \
	sum3 = 0.0; \
	for( j = 0; j < ref->Ysize; j++ ) { \
		for( i = 0; i < sz; i += bands ) { \
			/* Reference pel and input pel. \
			 */ \
			IN ip = p1a[i]; \
			IN rp = r1a[i]; \
			\
			/* Accumulate sum-of-squares-of-differences for \
			 * input image. \
			 */ \
			double t = ip - imean; \
			sum2 += t * t; \
			\
			/* Accumulate product-of-difference from mean. \
			 */ \
			sum3 += (rp - spcor->rmean[b]) * t; \
		} \
		\
		p1a += in_lsk; \
		r1a += sz; \
	} \
}

static void
vips_spcor_correlation( VipsCorrelation *correlation,
	VipsRegion *in, VipsRegion *out )
{
	VipsSpcor *spcor = (VipsSpcor *) correlation;
	VipsRect *r = &out->valid;
	VipsImage *ref = correlation->ref_ready;
	int bands = vips_band_format_iscomplex( ref->BandFmt ) ? 
		ref->Bands * 2 : ref->Bands; 
	int sz = ref->Xsize * bands; 
	int lsk = VIPS_REGION_LSKIP( in ); 

	int x, y, b, j, i;

	double imean;
	double sum1;
	double sum2, sum3;
	double c2, cc;

	for( y = 0; y < r->height; y++ ) {
		float *q = (float *) 
			VIPS_REGION_ADDR( out, r->left, r->top + y );

		for( x = 0; x < r->width; x++ ) {
			VipsPel *p = 
				VIPS_REGION_ADDR( in, r->left + x, r->top + y );

			for( b = 0; b < bands; b++ ) { 
				switch( vips_image_get_format( ref ) ) {
				case VIPS_FORMAT_UCHAR:	
					LOOP( unsigned char ); 
					break;

				case VIPS_FORMAT_CHAR:	
					LOOP( signed char ); 
					break;

				case VIPS_FORMAT_USHORT: 
					LOOP( unsigned short ); 
					break;

				case VIPS_FORMAT_SHORT:	
					LOOP( signed short ); 
					break;

				case VIPS_FORMAT_UINT: 	
					LOOP( unsigned int ); 
					break; 

				case VIPS_FORMAT_INT: 	
					LOOP( signed int ); 
					break; 

				case VIPS_FORMAT_FLOAT:	
				case VIPS_FORMAT_COMPLEX: 
					LOOP( float ); 
					break; 

				case VIPS_FORMAT_DOUBLE: 
				case VIPS_FORMAT_DPCOMPLEX: 
					LOOP( double ); 
					break;

				default:
					g_assert_not_reached();

					/* Stop compiler warnings.
					 */
					sum2 = 0;
					sum3 = 0;
				}

				c2 = spcor->c1[b] * sqrt( sum2 );

				if( c2 == 0.0 )
					/* Something like constant ref.
					 * We regard this as uncorrelated.
					 */
					cc = 0.0;
				else
					cc = sum3 / c2;

				*q++ = cc;
			}
		}
	}
}

/* Save a bit of typing.
 */
#define UC VIPS_FORMAT_UCHAR
#define C VIPS_FORMAT_CHAR
#define US VIPS_FORMAT_USHORT
#define S VIPS_FORMAT_SHORT
#define UI VIPS_FORMAT_UINT
#define I VIPS_FORMAT_INT
#define F VIPS_FORMAT_FLOAT
#define X VIPS_FORMAT_COMPLEX
#define D VIPS_FORMAT_DOUBLE
#define DX VIPS_FORMAT_DPCOMPLEX

static const VipsBandFormat vips_spcor_format_table[10] = {
	/* Band format:  UC C  US S  UI I  F  X  D  DX */
	/* Promotion: */ F, F, F, F, F, F, F, F, F, F
};

static void
vips_spcor_class_init( VipsSpcorClass *class )
{
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsCorrelationClass *cclass = VIPS_CORRELATION_CLASS( class );

	object_class->nickname = "spcor";
	object_class->description = _( "spatial correlation" );

	cclass->format_table = vips_spcor_format_table;
	cclass->pre_generate = vips_spcor_pre_generate;
	cclass->correlation = vips_spcor_correlation;
}

static void
vips_spcor_init( VipsSpcor *spcor )
{
}

/**
 * vips_spcor: (method)
 * @in: input image
 * @ref: reference image
 * @out: (out): output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Calculate a correlation surface.
 *
 * @ref is placed at every position in @in and the correlation coefficient
 * calculated. The output
 * image is always float.
 *
 * The output
 * image is the same size as the input. Extra input edge pixels are made by 
 * copying the existing edges outwards. 
 *
 * The correlation coefficient is calculated as:
 *
 * |[
 *          sumij (ref(i,j)-mean(ref))(inkl(i,j)-mean(inkl))
 * c(k,l) = ------------------------------------------------
 *          sqrt(sumij (ref(i,j)-mean(ref))^2) *
 *                      sqrt(sumij (inkl(i,j)-mean(inkl))^2)
 * ]|
 *
 * where inkl is the area of @in centred at position (k,l).
 *
 * from Niblack "An Introduction to Digital Image Processing", 
 * Prentice/Hall, pp 138.
 *
 * If the number of bands differs, one of the images 
 * must have one band. In this case, an n-band image is formed from the 
 * one-band image by joining n copies of the one-band image together, and then
 * the two n-band images are operated upon.
 *
 * The output image is always float, unless either of the two inputs is
 * double, in which case the output is also double.
 *
 * See also: vips_fastcor().
 *
 * Returns: 0 on success, -1 on error
 */
int 
vips_spcor( VipsImage *in, VipsImage *ref, VipsImage **out, ... )
{
	va_list ap;
	int result;

	va_start( ap, out );
	result = vips_call_split( "spcor", ap, in, ref, out );
	va_end( ap );

	return( result );
}