redo im_dilate()/erode() as classes

just a thin wrapper over the vips7 operations for now
This commit is contained in:
John Cupitt 2013-10-23 13:15:31 +01:00
parent 8d270d49c4
commit 5475cabbf2
11 changed files with 235 additions and 73 deletions

View File

@ -1,5 +1,6 @@
19/10/13 started 7.37.0
- redone im_rotate_*mask45(), im_gauss_*mask*(), im_log_*mask() as classes
- redone im_rotate_*mask45(), im_gauss_*mask*(), im_log_*mask(), im_dilate(),
im_erode() as classes
- vips_init() now does some ABI compat checking, though this change requires
an ABI break
- add "interlace" option to vips_jpegsave()

3
TODO
View File

@ -1,6 +1,3 @@
- vipsthumbnail needs non-square BBs, perhaps
--size 200x300
- do conv and morph quickly as simple wrappers over the vips7 operations

View File

@ -4,6 +4,7 @@ libconvolution_la_SOURCES = \
convolution.c \
pconvolution.h \
conv.c \
morph.c \
convol_dispatch.c \
im_addgnoise.c \
im_compass.c \

View File

@ -147,6 +147,8 @@ void
vips_convolution_operation_init( void )
{
extern int vips_conv_get_type( void );
extern int vips_morph_get_type( void );
vips_conv_get_type();
vips_morph_get_type();
}

179
libvips/convolution/morph.c Normal file
View File

@ -0,0 +1,179 @@
/* morphology
*
* 23/10/13
* - from vips_conv()
*/
/*
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 "pconvolution.h"
typedef struct {
VipsConvolution parent_instance;
VipsOperationMorphology morph;
} VipsMorph;
typedef VipsConvolutionClass VipsMorphClass;
G_DEFINE_TYPE( VipsMorph, vips_morph, VIPS_TYPE_CONVOLUTION );
static int
vips_morph_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConvolution *convolution = (VipsConvolution *) object;
VipsMorph *morph = (VipsMorph *) object;
INTMASK *imsk;
g_object_set( morph, "out", vips_image_new(), NULL );
if( VIPS_OBJECT_CLASS( vips_morph_parent_class )->build( object ) )
return( -1 );
if( !(imsk = im_vips2imask( convolution->M, class->nickname )) ||
!im_local_imask( convolution->out, imsk ) )
return( -1 );
switch( morph->morph ) {
case VIPS_OPERATION_MORPHOLOGY_DILATE:
if( im_dilate( convolution->in, convolution->out, imsk ) )
return( -1 );
break;
case VIPS_OPERATION_MORPHOLOGY_ERODE:
if( im_erode( convolution->in, convolution->out, imsk ) )
return( -1 );
break;
default:
g_assert( 0 );
}
return( 0 );
}
static void
vips_morph_class_init( VipsMorphClass *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 = "morph";
object_class->description = _( "convolution operation" );
object_class->build = vips_morph_build;
VIPS_ARG_ENUM( class, "morph", 103,
_( "Morphology" ),
_( "Morphological operation to perform" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsMorph, morph ),
VIPS_TYPE_OPERATION_MORPHOLOGY,
VIPS_OPERATION_MORPHOLOGY_ERODE );
}
static void
vips_morph_init( VipsMorph *morph )
{
morph->morph = VIPS_OPERATION_MORPHOLOGY_ERODE;
}
/**
* vips_morph:
* @in: input image
* @out: output image
* @mask: morphology with this mask
* @morph: operation to perform
* @...: %NULL-terminated list of optional named arguments
*
* Performs a morphological operation on @in using @mask as a
* structuring element.
*
* The image should have 0 (black) for no object and 255
* (non-zero) for an object. Note that this is the reverse of the usual
* convention for these operations, but more convenient when combined with the
* boolean operators. The output image is the same
* size as the input image: edge pxels are made by expanding the input image
* as necessary.
*
* Mask coefficients can be either 0 (for object) or 255 (for background)
* or 128 (for do not care). The origin of the mask is at location
* (m.xsize / 2, m.ysize / 2), integer division. All algorithms have been
* based on the book "Fundamentals of Digital Image Processing" by A. Jain,
* pp 384-388, Prentice-Hall, 1989.
*
* For #VIPS_OPERATION_MOPHOLOGY_ERODE,
* the whole mask must match for the output pixel to be
* set, that is, the result is the logical AND of the selected input pixels.
*
* For #VIPS_OPERATION_MOPHOLOGY_DILATE,
* the output pixel is set if any part of the mask
* matches, that is, the result is the logical OR of the selected input pixels.
*
* See the boolean operations vips_andimage(), vips_orimage() and
* vips_eorimage()
* for analogues of the usual set difference and set union operations.
*
* Operations are performed using the processor's vector unit,
* if possible. Disable this with --vips-novector or IM_NOVECTOR.
*
* Returns: 0 on success, -1 on error
*/
int
vips_morph( VipsImage *in, VipsImage **out, VipsImage *mask,
VipsOperationMorphology morph, ... )
{
va_list ap;
int result;
va_start( ap, morph );
result = vips_call_split( "morph", ap, in, out, mask, morph );
va_end( ap );
return( result );
}

View File

@ -65,6 +65,7 @@ vips_scan_headers = \
${top_srcdir}/libvips/include/vips/colour.h \
${top_srcdir}/libvips/include/vips/operation.h \
${top_srcdir}/libvips/include/vips/convolution.h \
${top_srcdir}/libvips/include/vips/morphology.h \
${top_srcdir}/libvips/include/vips/object.h
enumtypes.h: $(vips_scan_headers) Makefile

View File

@ -74,6 +74,9 @@ GType vips_operation_flags_get_type (void) G_GNUC_CONST;
/* enumerations from "../../../libvips/include/vips/convolution.h" */
GType vips_precision_get_type (void) G_GNUC_CONST;
#define VIPS_TYPE_PRECISION (vips_precision_get_type())
/* enumerations from "../../../libvips/include/vips/morphology.h" */
GType vips_operation_morphology_get_type (void) G_GNUC_CONST;
#define VIPS_TYPE_OPERATION_MORPHOLOGY (vips_operation_morphology_get_type())
/* enumerations from "../../../libvips/include/vips/object.h" */
GType vips_argument_flags_get_type (void) G_GNUC_CONST;
#define VIPS_TYPE_ARGUMENT_FLAGS (vips_argument_flags_get_type())

View File

@ -31,13 +31,37 @@
*/
#ifndef IM_MORPHOLOGY_H
#define IM_MORPHOLOGY_H
#ifndef VIPS_MORPHOLOGY_H
#define VIPS_MORPHOLOGY_H
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/
/**
* VipsOperationMorphology:
* @VIPS_OPERATION_MORPHOLOGY_ERODE: true if all set
* @VIPS_OPERATION_MORPHOLOGY_DILATE: true if one set
*
* More like hit-miss, really.
*
* See also: vips_morph().
*/
typedef enum {
VIPS_OPERATION_MORPHOLOGY_ERODE,
VIPS_OPERATION_MORPHOLOGY_DILATE,
VIPS_OPERATION_MORPHOLOGY_LAST
} VipsOperationMorphology;
int vips_morph( VipsImage *in, VipsImage **out, VipsImage *mask,
VipsOperationMorphology morph, ... )
__attribute__((sentinel));
int im_dilate( VipsImage *in, VipsImage *out, INTMASK *mask );
int im_erode( VipsImage *in, VipsImage *out, INTMASK *mask );
@ -53,4 +77,4 @@ int im_label_regions( VipsImage *test, VipsImage *mask, int *segments );
}
#endif /*__cplusplus*/
#endif /*IM_MORPHOLOGY_H*/
#endif /*VIPS_MORPHOLOGY_H*/

View File

@ -50,6 +50,7 @@ vips_scan_headers = \
${top_srcdir}/libvips/include/vips/colour.h \
${top_srcdir}/libvips/include/vips/operation.h \
${top_srcdir}/libvips/include/vips/convolution.h \
${top_srcdir}/libvips/include/vips/morphology.h \
${top_srcdir}/libvips/include/vips/object.h
enumtypes.c: $(vips_scan_headers) Makefile

View File

@ -633,6 +633,25 @@ vips_precision_get_type( void )
return( etype );
}
/* enumerations from "../../libvips/include/vips/morphology.h" */
GType
vips_operation_morphology_get_type( void )
{
static GType etype = 0;
if( etype == 0 ) {
static const GEnumValue values[] = {
{VIPS_OPERATION_MORPHOLOGY_ERODE, "VIPS_OPERATION_MORPHOLOGY_ERODE", "erode"},
{VIPS_OPERATION_MORPHOLOGY_DILATE, "VIPS_OPERATION_MORPHOLOGY_DILATE", "dilate"},
{VIPS_OPERATION_MORPHOLOGY_LAST, "VIPS_OPERATION_MORPHOLOGY_LAST", "last"},
{0, NULL, NULL}
};
etype = g_enum_register_static( "VipsOperationMorphology", values );
}
return( etype );
}
/* enumerations from "../../libvips/include/vips/object.h" */
GType
vips_argument_flags_get_type( void )

View File

@ -766,39 +766,6 @@ im_erode_raw( IMAGE *in, IMAGE *out, INTMASK *mask )
return( morphology( in, out, mask, ERODE ) );
}
/**
* im_dilate:
* @in: input image
* @out: output image
* @mask: mask
*
* im_dilate() performs a morphological dilate operation on @in using @mask as a
* structuring element. The output pixel is set if any part of the mask
* matches, that is, the result is the logical OR of the selected input pixels.
*
* The image should have 0 (black) for no object and 255
* (non-zero) for an object. Note that this is the reverse of the usual
* convention for these operations, but more convenient when combined with the
* boolean operators im_andimage() and friends. The output image is the same
* size as the input image: edge pxels are made by expanding the input image
* as necessary in the manner of im_conv().
*
* Mask coefficients can be either 0 (for object) or 255 (for background)
* or 128 (for do not care). The origin of the mask is at location
* (m.xsize / 2, m.ysize / 2), integer division. All algorithms have been
* based on the book "Fundamentals of Digital Image Processing" by A. Jain,
* pp 384-388, Prentice-Hall, 1989.
*
* See the boolean operations im_andimage(), im_orimage() and im_eorimage()
* for analogues of the usual set difference and set union operations.
*
* Operations are performed using the processor's vector unit,
* if possible. Disable this with --vips-novector or IM_NOVECTOR.
*
* See also: im_erode().
*
* Returns: 0 on success, -1 on error
*/
int
im_dilate( IMAGE *in, IMAGE *out, INTMASK *mask )
{
@ -817,39 +784,6 @@ im_dilate( IMAGE *in, IMAGE *out, INTMASK *mask )
return( 0 );
}
/**
* im_erode:
* @in: input image
* @out: output image
* @mask: mask
*
* im_erode() performs a morphological erode operation on @in using @mask as a
* structuring element. The whole mask must match for the output pixel to be
* set, that is, the result is the logical AND of the selected input pixels.
*
* The image should have 0 (black) for no object and 255
* (non-zero) for an object. Note that this is the reverse of the usual
* convention for these operations, but more convenient when combined with the
* boolean operators im_andimage() and friends. The output image is the same
* size as the input image: edge pxels are made by expanding the input image
* as necessary in the manner of im_conv().
*
* Mask coefficients can be either 0 (for object) or 255 (for background)
* or 128 (for do not care). The origin of the mask is at location
* (m.xsize / 2, m.ysize / 2), integer division. All algorithms have been
* based on the book "Fundamentals of Digital Image Processing" by A. Jain,
* pp 384-388, Prentice-Hall, 1989.
*
* See the boolean operations im_andimage(), im_orimage() and im_eorimage()
* for analogues of the usual set difference and set union operations.
*
* Operations are performed using the processor's vector unit,
* if possible. Disable this with --vips-novector or IM_NOVECTOR.
*
* See also: im_dilate().
*
* Returns: 0 on success, -1 on error
*/
int
im_erode( IMAGE *in, IMAGE *out, INTMASK *mask )
{