Fawkes API  Fawkes Development Version
geodesic_dilation.cpp
1 
2 /***************************************************************************
3  * geodesic_dilation.cpp - implementation of morphological geodesic dilation
4  *
5  * Created: Sat Jun 10 16:21:30 2006
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <core/exception.h>
25 
26 #include <fvfilters/morphology/geodesic_dilation.h>
27 #include <fvfilters/morphology/segenerator.h>
28 #include <fvfilters/morphology/dilation.h>
29 #include <fvfilters/min.h>
30 
31 #include <fvutils/statistical/imagediff.h>
32 #include <fvutils/color/colorspaces.h>
33 #include <fvutils/base/roi.h>
34 
35 #include <cstdlib>
36 #include <cstring>
37 
38 namespace firevision {
39 #if 0 /* just to make Emacs auto-indent happy */
40 }
41 #endif
42 
43 /** Marker */
44 const unsigned int FilterGeodesicDilation::MARKER = 0;
45 /** Mask */
46 const unsigned int FilterGeodesicDilation::MASK = 1;
47 
48 #define ERROR(m) { \
49  fawkes::Exception e("FilterGeodesicDilation failed"); \
50  e.append("Function: %s", __FUNCTION__); \
51  e.append("Message: %s", m); \
52  throw e; \
53  }
54 
55 
56 /** @class FilterGeodesicDilation <fvfilters/morphology/geodesic_dilation.h>
57  * Morphological geodesic dilation.
58  * @author Tim Niemueller
59  */
60 
61 /** Constructor.
62  * @param se_size Structuring element size.
63  */
65  : MorphologicalFilter("Morphological Geodesic Dilation", 2)
66 {
67  this->se_size = (se_size > 0) ? se_size : 1;
68  iterations = 0;
69 
70  dilate = new FilterDilation();
71  min = new FilterMin();
72  diff = new ImageDiff();
73 
74  isotropic_se = SEGenerator::square(this->se_size, this->se_size);
75 
76  dilate->set_structuring_element( isotropic_se, se_size, se_size, se_size / 2, se_size / 2 );
77 
78  src[MARKER] = src[MASK] = dst = NULL;
79  src_roi[MARKER] = src_roi[MASK] = dst_roi = NULL;
80 }
81 
82 
83 /** Destructor. */
85 {
86  delete dilate;
87  delete min;
88  delete diff;
89  free( isotropic_se );
90 }
91 
92 
93 void
95 {
96  if ( dst == NULL ) ERROR("dst == NULL");
97  if ( src[MASK] == NULL ) ERROR("src[MASK] == NULL");
98  if ( src[MARKER] == NULL ) ERROR("src[MARKER] == NULL");
99  if ( *(src_roi[MASK]) != *(src_roi[MARKER]) ) ERROR("marker and mask ROI differ");
100 
101  unsigned char *tmp = (unsigned char *)malloc(colorspace_buffer_size(YUV422_PLANAR, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height) );
102  memcpy( tmp, src[MARKER], colorspace_buffer_size(YUV422_PLANAR, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height) );
103 
104  diff->setBufferA( tmp, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height );
106 
107  dilate->set_src_buffer( tmp, src_roi[MARKER] );
108 
109  min->set_src_buffer( src[MASK], src_roi[MASK], 0 );
110  min->set_src_buffer( tmp, src_roi[MARKER], 1 );
111  min->set_dst_buffer( tmp, src_roi[MARKER] );
112 
113 
114  iterations = 0;
115  do {
116  memcpy(dst, tmp, colorspace_buffer_size(YUV422_PLANAR, dst_roi->image_width, dst_roi->image_height) );
117  dilate->apply();
118  min->apply();
119  } while (diff->different() && ( ++iterations < 255) );
120 
121  // std::cout << i << " iterations done for geodesic dilation" << std::endl;
122 
123  free( tmp );
124 
125 }
126 
127 
128 /** Get the number of iterations.
129  * @return the number of iterations that were necessary to get a stable result in the
130  * last call to apply().
131  */
132 unsigned int
134 {
135  return iterations;
136 }
137 
138 } // end namespace firevision
virtual void set_structuring_element(unsigned char *se, unsigned int se_width, unsigned int se_height, unsigned int se_anchor_x, unsigned int se_anchor_y)
Set the structuring element for successive filter runs.
virtual ~FilterGeodesicDilation()
Destructor.
virtual void set_src_buffer(unsigned char *buf, ROI *roi, orientation_t ori=ORI_HORIZONTAL, unsigned int buffer_num=0)
Set source buffer with orientation.
Definition: filter.cpp:93
void setBufferA(unsigned char *yuv422planar_buffer, unsigned int width, unsigned int height)
Set first buffer.
Definition: imagediff.cpp:70
bool different()
Check if images are different.
Definition: imagediff.cpp:102
virtual void apply()
Apply the filter.
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:125
Morphological dilation.
Definition: dilation.h:33
Image difference checker.
Definition: imagediff.h:35
FilterGeodesicDilation(unsigned int se_size=3)
Constructor.
static unsigned char * square(unsigned int width, unsigned int height)
Generate square structuring element.
Morphological filter interface.
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:65
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:127
virtual void apply()
Apply the filter.
Definition: min.cpp:50
static const unsigned int MASK
Mask.
virtual unsigned int num_iterations()
Get the number of iterations.
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:70
static const unsigned int MARKER
Marker.
Minimum filter.
Definition: min.h:34
void setBufferB(unsigned char *yuv422planar_buffer, unsigned int width, unsigned int height)
Set second buffer.
Definition: imagediff.cpp:85
virtual void apply()
Apply the filter.
Definition: dilation.cpp:83
unsigned char * dst
Destination buffer.
Definition: filter.h:67
ROI * dst_roi
Destination ROI.
Definition: filter.h:72
virtual void set_dst_buffer(unsigned char *buf, ROI *roi)
Set the destination buffer.
Definition: filter.cpp:134