geodesic_erosion.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <core/exception.h>
00025
00026 #include <filters/morphology/geodesic_erosion.h>
00027 #include <filters/morphology/segenerator.h>
00028 #include <filters/morphology/erosion.h>
00029 #include <filters/max.h>
00030
00031 #include <fvutils/color/colorspaces.h>
00032 #include <fvutils/statistical/imagediff.h>
00033
00034 #include <cstdlib>
00035 #include <cstring>
00036
00037
00038 const unsigned int FilterGeodesicErosion::MARKER = 0;
00039
00040 const unsigned int FilterGeodesicErosion::MASK = 1;
00041
00042 #define ERROR(m) { \
00043 fawkes::Exception e("FilterGeodesicErosion failed"); \
00044 e.append("Function: %s", __FUNCTION__); \
00045 e.append("Message: %s", m); \
00046 throw e; \
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 FilterGeodesicErosion::FilterGeodesicErosion(unsigned int se_size)
00058 : MorphologicalFilter("Morphological Geodesic Erosion")
00059 {
00060 this->se_size = (se_size > 0) ? se_size : 1;
00061 iterations = 0;
00062
00063 erode = new FilterErosion();
00064 max = new FilterMax();
00065 diff = new ImageDiff();
00066
00067 isotropic_se = SEGenerator::square(this->se_size, this->se_size);
00068
00069 erode->set_structuring_element( isotropic_se, se_size, se_size, se_size / 2, se_size / 2 );
00070 }
00071
00072
00073
00074 FilterGeodesicErosion::~FilterGeodesicErosion()
00075 {
00076 delete erode;
00077 delete max;
00078 delete diff;
00079 free( isotropic_se );
00080 }
00081
00082
00083 void
00084 FilterGeodesicErosion::apply()
00085 {
00086 if ( dst == NULL ) ERROR("dst == NULL");
00087 if ( src[MASK] == NULL ) ERROR("src[MASK] == NULL");
00088 if ( src[MARKER] == NULL ) ERROR("src[MARKER] == NULL");
00089 if ( *(src_roi[MASK]) != *(src_roi[MARKER]) ) ERROR("marker and mask ROI differ");
00090
00091 unsigned char *tmp = (unsigned char *)malloc(colorspace_buffer_size(YUV422_PLANAR, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height) );
00092 memcpy( tmp, src[MARKER], colorspace_buffer_size(YUV422_PLANAR, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height) );
00093
00094 diff->setBufferA( tmp, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height );
00095 diff->setBufferB( dst, dst_roi->image_width, dst_roi->image_height );
00096
00097 erode->set_src_buffer( tmp, src_roi[MARKER] );
00098
00099 max->set_src_buffer( src[MASK], src_roi[MASK], 0 );
00100 max->set_src_buffer( tmp, src_roi[MARKER], 1 );
00101 max->set_dst_buffer( tmp, src_roi[MARKER] );
00102
00103
00104 iterations = 0;
00105 do {
00106 memcpy(dst, tmp, colorspace_buffer_size(YUV422_PLANAR, dst_roi->image_width, dst_roi->image_height) );
00107 erode->apply();
00108 max->apply();
00109 } while (diff->different() && ( ++iterations < 255) );
00110
00111
00112
00113
00114 free( tmp );
00115
00116 }
00117
00118
00119
00120
00121
00122
00123 unsigned int
00124 FilterGeodesicErosion::num_iterations()
00125 {
00126 return iterations;
00127 }