tophat_closing.cpp

00001
00002 /***************************************************************************
00003  *  tophat_closing.cpp - implementation of morphological tophat closing
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/tophat_closing.h>
00027 #include <filters/morphology/segenerator.h>
00028 #include <filters/morphology/closing.h>
00029 #include <filters/difference.h>
00030
00031 #include <cstddef>
00032 
00033 /** Image that we subtract from */
00034 const unsigned int FilterTophatClosing::SUBTRACTFROM = 0;
00035 /** Image to filter. */
00036 const unsigned int FilterTophatClosing::FILTERIMAGE  = 1;
00037
00038 #define ERROR(m) {                                                      \
00039     fawkes::Exception e("FilterTophatClosing failed");                          \
00040     e.append("Function: %s", __FUNCTION__);                             \
00041     e.append("Message:  %s", m);                                        \
00042     throw e;                                                            \
00043   }
00044 
00045 /** @class FilterTophatClosing <filters/morphology/tophat_closing.h>
00046  * Morphological tophat closing.
00047  * @author Tim Niemueller
00048  */
00049 
00050 /** Constructor. */
00051 FilterTophatClosing::FilterTophatClosing()
00052   : MorphologicalFilter("Morphological Tophat Closing")
00053 {
00054   closing  = new FilterClosing();
00055   diff     = new FilterDifference();
00056
00057   src[SUBTRACTFROM] = src[FILTERIMAGE] = dst = NULL;
00058   src_roi[SUBTRACTFROM] = src_roi[FILTERIMAGE] = dst_roi = NULL;
00059 }
00060
00061 
00062 /** Destructor. */
00063 FilterTophatClosing::~FilterTophatClosing()
00064 {
00065   delete closing;
00066   delete diff;
00067 }
00068
00069
00070 void
00071 FilterTophatClosing::apply()
00072 {
00073   if ( dst == NULL ) ERROR("dst == NULL");
00074   if ( src[SUBTRACTFROM] == NULL ) ERROR("src[SUBTRACTFROM] == NULL");
00075   if ( src[FILTERIMAGE] == NULL ) ERROR("src[FILTERIMAGE] == NULL");
00076   if ( *(src_roi[SUBTRACTFROM]) != *(src_roi[FILTERIMAGE]) ) ERROR("marker and mask ROI differ");
00077
00078   closing->set_structuring_element( se, se_width, se_height, se_anchor_x, se_anchor_y );
00079
00080   closing->set_src_buffer( src[FILTERIMAGE], src_roi[FILTERIMAGE] );
00081   closing->set_dst_buffer( dst, dst_roi );
00082
00083   diff->set_src_buffer( src[SUBTRACTFROM], src_roi[SUBTRACTFROM], 1 );
00084   diff->set_src_buffer( dst, dst_roi, 0 );
00085   diff->set_dst_buffer( dst, dst_roi );
00086
00087   closing->apply();
00088   diff->apply();
00089 }