geodesic_erosion.cpp

00001
00002 /***************************************************************************
00003  *  geodesic_erosion.cpp - implementation of morphological geodesic erosion
00004  *
00005  *  Created: Sat Jun 10 16:21:30 2006
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
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 /** Marker */
00038 const unsigned int FilterGeodesicErosion::MARKER = 0;
00039 /** Mask */
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 /** @class FilterGeodesicErosion <filters/morphology/geodesic_erosion.h>
00050  * Morphological geodesic erosion.
00051  * @author Tim Niemueller
00052  */
00053 
00054 /** Constructor.
00055  * @param se_size Structuring element size.
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 /** Destructor. */
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   // std::cout << i << " iterations done for geodesic erosion" << std::endl;
00112
00113
00114   free( tmp );
00115
00116 }
00117
00118 
00119 /** Get the number of iterations.
00120  * @return the number of iterations that were necessary to get a stable result in the
00121  * last call to apply().
00122  */
00123 unsigned int
00124 FilterGeodesicErosion::num_iterations()
00125 {
00126   return iterations;
00127 }