geodesic_dilation.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_dilation.h>
00027 #include <filters/morphology/segenerator.h>
00028 #include <filters/morphology/dilation.h>
00029 #include <filters/min.h>
00030
00031 #include <fvutils/statistical/imagediff.h>
00032 #include <fvutils/color/colorspaces.h>
00033 #include <fvutils/base/roi.h>
00034
00035 #include <cstdlib>
00036 #include <cstring>
00037
00038
00039 const unsigned int FilterGeodesicDilation::MARKER = 0;
00040
00041 const unsigned int FilterGeodesicDilation::MASK = 1;
00042
00043 #define ERROR(m) { \
00044 fawkes::Exception e("FilterGeodesicDilation failed"); \
00045 e.append("Function: %s", __FUNCTION__); \
00046 e.append("Message: %s", m); \
00047 throw e; \
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 FilterGeodesicDilation::FilterGeodesicDilation(unsigned int se_size)
00060 : MorphologicalFilter("Morphological Geodesic Dilation", 2)
00061 {
00062 this->se_size = (se_size > 0) ? se_size : 1;
00063 iterations = 0;
00064
00065 dilate = new FilterDilation();
00066 min = new FilterMin();
00067 diff = new ImageDiff();
00068
00069 isotropic_se = SEGenerator::square(this->se_size, this->se_size);
00070
00071 dilate->set_structuring_element( isotropic_se, se_size, se_size, se_size / 2, se_size / 2 );
00072
00073 src[MARKER] = src[MASK] = dst = NULL;
00074 src_roi[MARKER] = src_roi[MASK] = dst_roi = NULL;
00075 }
00076
00077
00078
00079 FilterGeodesicDilation::~FilterGeodesicDilation()
00080 {
00081 delete dilate;
00082 delete min;
00083 delete diff;
00084 free( isotropic_se );
00085 }
00086
00087
00088 void
00089 FilterGeodesicDilation::apply()
00090 {
00091 if ( dst == NULL ) ERROR("dst == NULL");
00092 if ( src[MASK] == NULL ) ERROR("src[MASK] == NULL");
00093 if ( src[MARKER] == NULL ) ERROR("src[MARKER] == NULL");
00094 if ( *(src_roi[MASK]) != *(src_roi[MARKER]) ) ERROR("marker and mask ROI differ");
00095
00096 unsigned char *tmp = (unsigned char *)malloc(colorspace_buffer_size(YUV422_PLANAR, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height) );
00097 memcpy( tmp, src[MARKER], colorspace_buffer_size(YUV422_PLANAR, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height) );
00098
00099 diff->setBufferA( tmp, src_roi[MARKER]->image_width, src_roi[MARKER]->image_height );
00100 diff->setBufferB( dst, dst_roi->image_width, dst_roi->image_height );
00101
00102 dilate->set_src_buffer( tmp, src_roi[MARKER] );
00103
00104 min->set_src_buffer( src[MASK], src_roi[MASK], 0 );
00105 min->set_src_buffer( tmp, src_roi[MARKER], 1 );
00106 min->set_dst_buffer( tmp, src_roi[MARKER] );
00107
00108
00109 iterations = 0;
00110 do {
00111 memcpy(dst, tmp, colorspace_buffer_size(YUV422_PLANAR, dst_roi->image_width, dst_roi->image_height) );
00112 dilate->apply();
00113 min->apply();
00114 } while (diff->different() && ( ++iterations < 255) );
00115
00116
00117
00118 free( tmp );
00119
00120 }
00121
00122
00123
00124
00125
00126
00127 unsigned int
00128 FilterGeodesicDilation::num_iterations()
00129 {
00130 return iterations;
00131 }