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
|
/* im_measure.c
*
* Modified:
* 19/8/94 JC
* - now uses doubles for addressing
* - could miss by up to h pixels previously!
* - ANSIfied
* - now issues warning if any deviations are greater than 20% of the
* mean
* 31/10/95 JC
* - more careful about warning for averages <0, or averages near zero
* - can get these cases with im_measure() of IM_TYPE_LAB images
* 28/10/02 JC
* - number bands from zero in error messages
* 7/7/04
* - works on labq
* 18/8/08
* - add gtkdoc comments
* - remove deprecated im_extract()
* 30/11/09
* - changes for im_extract() broke averaging
* 9/11/11
* - redo as a class
* 19/5/14
* - add auto-unpack
*/
/*
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
*/
/*
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include "statistic.h"
typedef struct _VipsMeasure {
VipsOperation parent_instance;
VipsImage *in;
VipsImage *out;
int left;
int top;
int width;
int height;
int h;
int v;
} VipsMeasure;
typedef VipsOperationClass VipsMeasureClass;
G_DEFINE_TYPE( VipsMeasure, vips_measure, VIPS_TYPE_OPERATION );
static int
vips_measure_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsMeasure *measure = (VipsMeasure *) object;
VipsImage *ready;
int bands;
double pw;
double ph;
int j, i;
int w, h;
int b;
if( VIPS_OBJECT_CLASS( vips_measure_parent_class )->build( object ) )
return( -1 );
if( vips_image_decode( measure->in, &ready ) )
return( -1 );
vips_object_local( measure, ready );
bands = vips_image_get_bands( ready );
g_object_set( object,
"out", vips_image_new_matrix( bands, measure->h * measure->v ),
NULL );
/* left/top/width/height default to the size of the image.
*/
if( !vips_object_argument_isset( object, "width" ) )
g_object_set( object,
"width", vips_image_get_width( ready ),
NULL );
if( !vips_object_argument_isset( object, "height" ) )
g_object_set( object,
"height", vips_image_get_height( ready ),
NULL );
/* How large are the patches we are to measure?
*/
pw = (double) measure->width / measure->h;
ph = (double) measure->height / measure->v;
/* The size of a patch.
*/
w = (pw + 1) / 2;
h = (ph + 1) / 2;
for( j = 0; j < measure->v; j++ ) {
for( i = 0; i < measure->h; i++ ) {
int x = measure->left + i * pw + (pw + 2) / 4;
int y = measure->top + j * ph + (ph + 2) / 4;
double avg, dev;
for( b = 0; b < bands; b++ ) {
VipsImage **t = (VipsImage **)
vips_object_local_array( object, 2 );
/* Extract and measure.
*/
if( vips_extract_area( ready, &t[0],
x, y, w, h, NULL ) ||
vips_extract_band( t[0], &t[1],
b, NULL ) ||
vips_avg( t[1], &avg, NULL ) ||
vips_deviate( t[1], &dev, NULL ) )
return( -1 );
/* Is the deviation large compared with the
* average? This could be a clue that our
* parameters have caused us to miss the
* patch. Look out for averages <0, or
* averages near zero (can get these if use
* measure on IM_TYPE_LAB images).
*/
if( dev * 5 > VIPS_FABS( avg ) &&
VIPS_FABS( avg ) > 3 )
vips_warn( class->nickname,
_( "patch %d x %d, band %d: "
"avg = %g, sdev = %g" ),
i, j, b, avg, dev );
*VIPS_MATRIX( measure->out,
b, i + j * measure->h ) = avg;
}
}
}
return( 0 );
}
/* xy range we sanity check on ... just to stop crazy numbers from 1/0 etc.
* causing g_assert() failures later.
*/
#define RANGE (100000000)
static void
vips_measure_class_init( VipsMeasureClass *class )
{
GObjectClass *gobject_class = (GObjectClass *) 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 = "measure";
object_class->description =
_( "measure a set of patches on a color chart" );
object_class->build = vips_measure_build;
VIPS_ARG_IMAGE( class, "in", 1,
_( "in" ),
_( "Image to measure" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsMeasure, in ) );
VIPS_ARG_IMAGE( class, "out", 2,
_( "Output" ),
_( "Output array of statistics" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsMeasure, out ) );
VIPS_ARG_INT( class, "h", 5,
_( "Across" ),
_( "Number of patches across chart" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsMeasure, h ),
1, RANGE, 1 );
VIPS_ARG_INT( class, "v", 6,
_( "Down" ),
_( "Number of patches down chart" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsMeasure, v ),
1, RANGE, 1 );
VIPS_ARG_INT( class, "left", 10,
_( "Left" ),
_( "Left edge of extract area" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsMeasure, left ),
0, RANGE, 0 );
VIPS_ARG_INT( class, "top", 11,
_( "Top" ),
_( "Top edge of extract area" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsMeasure, top ),
0, RANGE, 0 );
VIPS_ARG_INT( class, "width", 12,
_( "Width" ),
_( "Width of extract area" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsMeasure, width ),
1, RANGE, 1 );
VIPS_ARG_INT( class, "height", 13,
_( "Height" ),
_( "Height of extract area" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsMeasure, height ),
1, RANGE, 1 );
}
static void
vips_measure_init( VipsMeasure *measure )
{
}
/**
* vips_measure:
* @in: image to measure
* @out: array of measurements
* @h: patches across chart
* @v: patches down chart
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @left: area of image containing chart
* * @top: area of image containing chart
* * @width: area of image containing chart
* * @height: area of image containing chart
*
* Analyse a grid of colour patches, producing an array of patch averages.
* The mask has a row for each measured patch and a column for each image
* band. The operations issues a warning if any patch has a deviation more
* than 20% of
* the mean. Only the central 50% of each patch is averaged.
*
* If the chart does not fill the whole image, use the optional @left, @top,
* @width, @height arguments to indicate the
* position of the chart.
*
* See also: vips_avg(), vips_deviate().
*
* Returns: 0 on success, -1 on error
*/
int
vips_measure( VipsImage *in, VipsImage **out, int h, int v, ... )
{
va_list ap;
int result;
va_start( ap, v );
result = vips_call_split( "measure", ap, in, out, h, v );
va_end( ap );
return( result );
}
|