File: mosaic.c

package info (click to toggle)
vips 8.4.5-1%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 25,724 kB
  • sloc: ansic: 102,605; cpp: 57,527; sh: 4,957; xml: 3,317; python: 3,105; makefile: 1,089; perl: 40
file content (365 lines) | stat: -rw-r--r-- 9,173 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
/* mosaic two images left/right or up/down
 *
 * 22/5/14
 * 	- from vips_mosaic()
 */

/*

    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

 */

/* This is a simple wrapper over the old vips7 functions. At some point we
 * should rewrite this as a pure vips8 class and redo the vips7 functions as
 * wrappers over this.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>

#include <stdio.h>

#include <vips/vips.h>

#include "pmosaicing.h"

typedef struct {
	VipsOperation parent_instance;

	VipsImage *ref;
	VipsImage *sec;
	VipsImage *out;
	VipsDirection direction;
	int xref;
	int yref;
	int xsec;
	int ysec;
	int mblend;
	int bandno;
	int hwindow;
	int harea;
	int dx0;
	int dy0;
	double scale1;
	double angle1;
	double dx1;
	double dy1;

} VipsMosaic;

typedef VipsOperationClass VipsMosaicClass;

G_DEFINE_TYPE( VipsMosaic, vips_mosaic, VIPS_TYPE_OPERATION );

static int
vips_mosaic_build( VipsObject *object )
{
	VipsMosaic *mosaic = (VipsMosaic *) object;

	VipsImage *x;
	int dx0;
	int dy0;
	double scale1;
	double angle1;
	double dx1;
	double dy1;

	g_object_set( mosaic, "out", vips_image_new(), NULL ); 

	if( VIPS_OBJECT_CLASS( vips_mosaic_parent_class )->build( object ) )
		return( -1 );

	/* A placeholder used to ensure that memory used by the analysis 
	 * phase is freed as soon as possible.
	 */
	x = vips_image_new(); 

	switch( mosaic->direction ) { 
	case VIPS_DIRECTION_HORIZONTAL:
		if( im__find_lroverlap( mosaic->ref, mosaic->sec, x,
			mosaic->bandno, 
			mosaic->xref, mosaic->yref, mosaic->xsec, mosaic->ysec,
			mosaic->hwindow, mosaic->harea, 
			&dx0, &dy0,
			&scale1, &angle1, 
			&dx1, &dy1 ) ) {
			g_object_unref( x );
			return( -1 );
		}
		g_object_unref( x );
		break;

	case VIPS_DIRECTION_VERTICAL:
		if( im__find_tboverlap( mosaic->ref, mosaic->sec, x,
			mosaic->bandno, 
			mosaic->xref, mosaic->yref, mosaic->xsec, mosaic->ysec,
			mosaic->hwindow, mosaic->harea, 
			&dx0, &dy0,
			&scale1, &angle1, 
			&dx1, &dy1 ) ) {
			g_object_unref( x );
			return( -1 );
		}
		g_object_unref( x );
		break;

	default:
		g_assert_not_reached();

		/* Compiler warnings.
		 */
		dx0 = 0;
		dy0 = 0;
		scale1 = 1;
		angle1 = 1;
		dx1 = 0;
		dy1 = 0;
	}

	g_object_set( mosaic, 
		"dx0", dx0, 
		"dy0", dy0,
		"scale1", scale1, 
		"angle1", angle1, 
		"dx1", dx1, 
		"dy1", dy1,
		NULL ); 

	if( vips_merge( mosaic->ref, mosaic->sec, &x, 
		mosaic->direction, mosaic->dx0, mosaic->dy0, 
		"mblend", mosaic->mblend,
		NULL ) )
		return( -1 );
	if( vips_image_write( x, mosaic->out ) ) {
		g_object_unref( x ); 
		return( -1 ); 
	}
	g_object_unref( x ); 

	return( 0 );
}

static void
vips_mosaic_class_init( VipsMosaicClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = (VipsObjectClass *) class;

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	object_class->nickname = "mosaic";
	object_class->description = _( "mosaic two images" );
	object_class->build = vips_mosaic_build;

	VIPS_ARG_IMAGE( class, "ref", 1, 
		_( "Reference" ), 
		_( "Reference image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsMosaic, ref ) );

	VIPS_ARG_IMAGE( class, "sec", 2, 
		_( "Secondary" ), 
		_( "Secondary image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsMosaic, sec ) );

	VIPS_ARG_IMAGE( class, "out", 3, 
		_( "Output" ), 
		_( "Output image" ),
		VIPS_ARGUMENT_REQUIRED_OUTPUT, 
		G_STRUCT_OFFSET( VipsMosaic, out ) );

	VIPS_ARG_ENUM( class, "direction", 4, 
		_( "Direction" ), 
		_( "Horizontal or vertcial mosaic" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsMosaic, direction ), 
		VIPS_TYPE_DIRECTION, VIPS_DIRECTION_HORIZONTAL ); 

	VIPS_ARG_INT( class, "xref", 5, 
		_( "xref" ), 
		_( "Position of reference tie-point" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, xref ),
		0, 1000000000, 1 );

	VIPS_ARG_INT( class, "yref", 6, 
		_( "yref" ), 
		_( "Position of reference tie-point" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, yref ),
		0, 1000000000, 1 );

	VIPS_ARG_INT( class, "xsec", 7, 
		_( "xsec" ), 
		_( "Position of secondary tie-point" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, xsec ),
		0, 1000000000, 1 );

	VIPS_ARG_INT( class, "ysec", 8, 
		_( "ysec" ), 
		_( "Position of secondary tie-point" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, ysec ),
		0, 1000000000, 1 );

	VIPS_ARG_INT( class, "hwindow", 9, 
		_( "hwindow" ), 
		_( "Half window size" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, hwindow ),
		0, 1000000000, 1 );

	VIPS_ARG_INT( class, "harea", 10, 
		_( "harea" ), 
		_( "Half area size" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, harea ),
		0, 1000000000, 1 );

	VIPS_ARG_INT( class, "mblend", 11, 
		_( "Max blend" ), 
		_( "Maximum blend size" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, mblend ),
		0, 10000, 10 );

	VIPS_ARG_INT( class, "bandno", 12, 
		_( "Search band" ), 
		_( "Band to search for features on" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsMosaic, bandno ),
		0, 10000, 0 );

	VIPS_ARG_INT( class, "dx0", 13, 
		_( "Integer offset" ), 
		_( "Detected integer offset" ), 
		VIPS_ARGUMENT_OPTIONAL_OUTPUT,
		G_STRUCT_OFFSET( VipsMosaic, dx0 ),
		-10000000, 10000000, 0 );

	VIPS_ARG_INT( class, "dy0", 14, 
		_( "Integer offset" ), 
		_( "Detected integer offset" ), 
		VIPS_ARGUMENT_OPTIONAL_OUTPUT,
		G_STRUCT_OFFSET( VipsMosaic, dy0 ),
		-10000000, 10000000, 0 );

	VIPS_ARG_DOUBLE( class, "scale1", 15, 
		_( "Scale" ), 
		_( "Detected scale" ), 
		VIPS_ARGUMENT_OPTIONAL_OUTPUT,
		G_STRUCT_OFFSET( VipsMosaic, scale1 ),
		-10000000.0, 10000000.0, 1.0 );

	VIPS_ARG_DOUBLE( class, "angle1", 16, 
		_( "Angle" ), 
		_( "Detected rotation" ), 
		VIPS_ARGUMENT_OPTIONAL_OUTPUT,
		G_STRUCT_OFFSET( VipsMosaic, angle1 ),
		-10000000.0, 10000000.0, 0.0 );

	VIPS_ARG_DOUBLE( class, "dx1", 17, 
		_( "First-order displacement" ), 
		_( "Detected first-order displacement" ), 
		VIPS_ARGUMENT_OPTIONAL_OUTPUT,
		G_STRUCT_OFFSET( VipsMosaic, dx1 ),
		-10000000.0, 10000000.0, 0.0 );

	VIPS_ARG_DOUBLE( class, "dy1", 17, 
		_( "First-order displacement" ), 
		_( "Detected first-order displacement" ), 
		VIPS_ARGUMENT_OPTIONAL_OUTPUT,
		G_STRUCT_OFFSET( VipsMosaic, dy1 ),
		-10000000.0, 10000000.0, 0.0 );

}

static void
vips_mosaic_init( VipsMosaic *mosaic )
{
	mosaic->mblend = 10;
	mosaic->hwindow = 5;
	mosaic->harea = 15;
	mosaic->scale1 = 1.0;
}

/**
 * vips_mosaic:
 * @ref: reference image
 * @sec: secondary image
 * @out: output image
 * @direction: horizontal or vertical join
 * @xref: position in reference image
 * @yref: position in reference image
 * @xsec: position in secondary image
 * @ysec: position in secondary image
 * @...: %NULL-terminated list of optional named arguments
 * 
 * Optional arguments:
 *
 * * @bandno: %gint, band to search for features
 * * @hwindow: %gint, half window size
 * * @harea: %gint, half search size 
 * * @mblend: %gint, maximum blend size
 *
 * This operation joins two images left-right (with @ref on the left) or
 * top-bottom (with @ref above) given an approximate overlap.
 *
 * @sec is positioned so that the pixel (@xsec, @ysec) in @sec lies on top of 
 * the pixel (@xref, @yref) in @ref. The overlap area is divided into three
 * sections, 20 high-contrast points in band @bandno of image @ref are found 
 * in each, and each high-contrast point is searched for in @sec using
 * @hwindow and @harea (see vips_correl()). 
 *
 * A linear model is fitted to the 60 tie-points, points a long way from the
 * fit are discarded, and the model refitted until either too few points
 * remain or the model reaches good agreement. 
 *
 * The detected displacement is used with vips_merge() to join the two images
 * together. 
 *
 * See also: vips_merge(), vips_insert().
 *
 * Returns: 0 on success, -1 on error
 */
int 
vips_mosaic( VipsImage *ref, VipsImage *sec, VipsImage **out, 
	VipsDirection direction, int xref, int yref, int xsec, int ysec, ... )
{
	va_list ap;
	int result;

	va_start( ap, ysec );
	result = vips_call_split( "mosaic", ap, ref, sec, out, 
		direction, xref, yref, xsec, ysec );
	va_end( ap );

	return( result );
}