geodesic_dilation.cpp

00001
00002 /***************************************************************************
00003  *  geodesic_dilation.cpp - implementation of morphological geodesic dilation
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_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 /** Marker */
00039 const unsigned int FilterGeodesicDilation::MARKER = 0;
00040 /** Mask */
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 /** @class FilterGeodesicDilation <filters/morphology/geodesic_dilation.h>
00052  * Morphological geodesic dilation.
00053  * @author Tim Niemueller
00054  */
00055 
00056 /** Constructor.
00057  * @param se_size Structuring element size.
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 /** Destructor. */
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   // std::cout << i << " iterations done for geodesic dilation" << std::endl;
00117
00118   free( tmp );
00119
00120 }
00121
00122 
00123 /** Get the number of iterations.
00124  * @return the number of iterations that were necessary to get a stable result in the
00125  * last call to apply().
00126  */
00127 unsigned int
00128 FilterGeodesicDilation::num_iterations()
00129 {
00130   return iterations;
00131 }