2013-09-20 15:33:34 +02:00
|
|
|
/* find percent of pixels
|
|
|
|
*
|
|
|
|
* Copyright: 1990, N. Dessipris
|
|
|
|
*
|
|
|
|
* Author: N. Dessipris
|
|
|
|
* Written on: 02/08/1990
|
|
|
|
* Modified on : 29/4/93 K.Martinez for Sys5
|
|
|
|
* 20/2/95 JC
|
|
|
|
* - now returns result through parameter
|
|
|
|
* - ANSIfied a little
|
|
|
|
* 19/1/07
|
|
|
|
* - redone with the vips hist operators
|
|
|
|
* 25/3/10
|
|
|
|
* - gtkdoc
|
|
|
|
* 20/9/13
|
|
|
|
* - wrap as a class
|
2013-09-21 17:19:51 +02:00
|
|
|
* - more accurate
|
2013-09-20 15:33:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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 <vips/intl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
|
2013-10-02 11:16:46 +02:00
|
|
|
typedef struct _VipsPercent {
|
2013-09-20 15:33:34 +02:00
|
|
|
VipsOperation parent_instance;
|
|
|
|
|
|
|
|
VipsImage *in;
|
|
|
|
double percent;
|
|
|
|
int threshold;
|
|
|
|
|
2013-10-02 11:16:46 +02:00
|
|
|
} VipsPercent;
|
2013-09-20 15:33:34 +02:00
|
|
|
|
2013-10-02 11:16:46 +02:00
|
|
|
typedef VipsOperationClass VipsPercentClass;
|
2013-09-20 15:33:34 +02:00
|
|
|
|
2013-10-02 11:16:46 +02:00
|
|
|
G_DEFINE_TYPE( VipsPercent, vips_percent, VIPS_TYPE_OPERATION );
|
2013-09-20 15:33:34 +02:00
|
|
|
|
|
|
|
static int
|
2013-10-02 11:16:46 +02:00
|
|
|
vips_percent_build( VipsObject *object )
|
2013-09-20 15:33:34 +02:00
|
|
|
{
|
2013-10-02 11:16:46 +02:00
|
|
|
VipsPercent *percent = (VipsPercent *) object;
|
2013-09-21 17:19:51 +02:00
|
|
|
VipsImage **t = (VipsImage **) vips_object_local_array( object, 7 );
|
2013-09-20 15:33:34 +02:00
|
|
|
|
|
|
|
double threshold;
|
|
|
|
|
2013-10-02 11:16:46 +02:00
|
|
|
if( VIPS_OBJECT_CLASS( vips_percent_parent_class )->
|
2013-09-20 15:33:34 +02:00
|
|
|
build( object ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if( vips_hist_find( percent->in, &t[0], NULL ) ||
|
|
|
|
vips_hist_cum( t[0], &t[1], NULL ) ||
|
|
|
|
vips_hist_norm( t[1], &t[2], NULL ) ||
|
2013-09-21 17:19:51 +02:00
|
|
|
vips_more_const1( t[2], &t[3],
|
|
|
|
(percent->percent / 100.0) * t[2]->Xsize, NULL ) ||
|
|
|
|
vips_profile( t[3], &t[5], &t[6], NULL ) ||
|
|
|
|
vips_avg( t[6], &threshold, NULL ) )
|
2013-09-20 15:33:34 +02:00
|
|
|
return( -1 );
|
|
|
|
|
2013-09-21 17:19:51 +02:00
|
|
|
g_object_set( object, "threshold", (int) threshold, NULL );
|
2013-09-20 15:33:34 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-02 11:16:46 +02:00
|
|
|
vips_percent_class_init( VipsPercentClass *class )
|
2013-09-20 15:33:34 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2013-10-02 11:16:46 +02:00
|
|
|
object_class->nickname = "percent";
|
2013-09-20 15:33:34 +02:00
|
|
|
object_class->description = _( "find threshold for percent of pixels" );
|
2013-10-02 11:16:46 +02:00
|
|
|
object_class->build = vips_percent_build;
|
2013-09-20 15:33:34 +02:00
|
|
|
|
|
|
|
VIPS_ARG_IMAGE( class, "in", 1,
|
|
|
|
_( "Input" ),
|
|
|
|
_( "Input image" ),
|
|
|
|
VIPS_ARGUMENT_REQUIRED_INPUT,
|
2013-10-02 11:16:46 +02:00
|
|
|
G_STRUCT_OFFSET( VipsPercent, in ) );
|
2013-09-20 15:33:34 +02:00
|
|
|
|
|
|
|
VIPS_ARG_DOUBLE( class, "percent", 2,
|
|
|
|
_( "Percent" ),
|
2013-10-02 11:16:46 +02:00
|
|
|
_( "Percent of pixels" ),
|
2013-09-20 15:33:34 +02:00
|
|
|
VIPS_ARGUMENT_REQUIRED_INPUT,
|
2013-10-02 11:16:46 +02:00
|
|
|
G_STRUCT_OFFSET( VipsPercent, percent ),
|
2013-09-20 15:33:34 +02:00
|
|
|
0, 100, 50 );
|
|
|
|
|
2013-09-21 17:19:51 +02:00
|
|
|
VIPS_ARG_INT( class, "threshold", 3,
|
2013-10-02 11:16:46 +02:00
|
|
|
_( "Threshold" ),
|
|
|
|
_( "Threshold above which lie percent of pixels" ),
|
2013-09-20 15:33:34 +02:00
|
|
|
VIPS_ARGUMENT_REQUIRED_OUTPUT,
|
2013-10-02 11:16:46 +02:00
|
|
|
G_STRUCT_OFFSET( VipsPercent, threshold ),
|
2013-09-20 15:33:34 +02:00
|
|
|
0, 65535, 0 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-02 11:16:46 +02:00
|
|
|
vips_percent_init( VipsPercent *percent )
|
2013-09-20 15:33:34 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-30 23:45:43 +02:00
|
|
|
* vips_percent: (method)
|
2013-09-20 15:33:34 +02:00
|
|
|
* @in: input image
|
|
|
|
* @percent: threshold percentage
|
2017-09-30 23:45:43 +02:00
|
|
|
* @threshold: (out): output threshold value
|
2013-09-20 15:33:34 +02:00
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* vips_percent() returns (through the @threshold parameter) the threshold
|
2020-02-21 12:53:55 +01:00
|
|
|
* below which there are @percent values of @in. For example:
|
|
|
|
*
|
|
|
|
* |[
|
2021-12-21 15:53:14 +01:00
|
|
|
* $ vips percent k2.jpg 90
|
2020-02-21 12:53:55 +01:00
|
|
|
* 214
|
|
|
|
* ]|
|
|
|
|
*
|
|
|
|
* Means that 90% of pixels in `k2.jpg` have a value less than 214.
|
2013-09-20 15:33:34 +02:00
|
|
|
*
|
|
|
|
* The function works for uchar and ushort images only. It can be used
|
|
|
|
* to threshold the scaled result of a filtering operation.
|
|
|
|
*
|
|
|
|
* See also: vips_hist_find(), vips_profile().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
2013-10-02 11:16:46 +02:00
|
|
|
vips_percent( VipsImage *in, double percent, int *threshold, ... )
|
2013-09-20 15:33:34 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start( ap, threshold );
|
2013-10-02 11:16:46 +02:00
|
|
|
result = vips_call_split( "percent", ap, in, percent, threshold );
|
2013-09-20 15:33:34 +02:00
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|