SMIL  0.9.1
DMorphoBase.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _D_MORPHO_BASE_HPP
31 #define _D_MORPHO_BASE_HPP
32 
33 #include "Core/include/DImage.h"
34 #include "Base/include/private/DImageArith.hpp"
35 #include "Morpho/include/DMorphoInstance.h"
36 #include "DMorphImageOperations.hxx"
37 #include "DHitOrMiss.hpp"
38 
39 
40 namespace smil
41 {
64  template <class T>
65  RES_T dilate(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE, T borderVal=ImDtTypes<T>::min())
66  {
67  ASSERT_ALLOCATED(&imIn, &imOut);
68  ASSERT_SAME_SIZE(&imIn, &imOut);
69 
70  MorphImageFunction<T, supLine<T> > iFunc(borderVal);
71  return iFunc(imIn, imOut, se);
72  }
73 
80  template <class T>
81  RES_T dilate(const Image<T> &imIn, Image<T> &imOut, UINT seSize, T borderVal=ImDtTypes<T>::min())
82  {
83  return dilate(imIn, imOut, DEFAULT_SE(seSize), borderVal);
84  }
85 
100  template <class T>
101  RES_T erode(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE, T borderVal=ImDtTypes<T>::max())
102  {
103  ASSERT_ALLOCATED(&imIn, &imOut);
104  ASSERT_SAME_SIZE(&imIn, &imOut);
105 
106  MorphImageFunction<T, infLine<T> > iFunc(borderVal);
107  return iFunc(imIn, imOut, se.transpose());
108  }
109 
116  template <class T>
117  RES_T erode(const Image<T> &imIn, Image<T> &imOut, UINT seSize, T borderVal=ImDtTypes<T>::max())
118  {
119  return erode(imIn, imOut, DEFAULT_SE(seSize), borderVal);
120  }
121 
126  template <class T>
127  RES_T close(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
128  {
129  ASSERT_ALLOCATED(&imIn, &imOut);
130  ASSERT_SAME_SIZE(&imIn, &imOut);
131  ImageFreezer freeze(imOut);
132 
133  bool inplaceSafe = MorphImageFunction<T, supLine<T> >::isInplaceSafe(se);
134  Image<T> *imTmp;
135  if (inplaceSafe)
136  imTmp = &imOut;
137  else imTmp = new Image<T>(imIn);
138 
139  ASSERT((dilate(imIn, *imTmp, se)==RES_OK));
140  ASSERT((erode(*imTmp, imOut, se)==RES_OK));
141 
142  if (!inplaceSafe)
143  delete imTmp;
144 
145  return RES_OK;
146  }
147 
148  template <class T>
149  RES_T close(const Image<T> &imIn, Image<T> &imOut, UINT seSize)
150  {
151  return close(imIn, imOut, DEFAULT_SE(seSize));
152  }
153 
158  template <class T>
159  RES_T open(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
160  {
161  ASSERT_ALLOCATED(&imIn, &imOut);
162  ASSERT_SAME_SIZE(&imIn, &imOut);
163  ImageFreezer freeze(imOut);
164 
165  bool inplaceSafe = MorphImageFunction<T, supLine<T> >::isInplaceSafe(se);
166  Image<T> *imTmp;
167  if (inplaceSafe)
168  imTmp = &imOut;
169  else imTmp = new Image<T>(imIn);
170 
171  ASSERT((erode(imIn, *imTmp, se)==RES_OK));
172  ASSERT((dilate(*imTmp, imOut, se)==RES_OK));
173 
174  if (!inplaceSafe)
175  delete imTmp;
176 
177  return RES_OK;
178  }
179 
180  template <class T>
181  RES_T open(const Image<T> &imIn, Image<T> &imOut, UINT seSize)
182  {
183  return open(imIn, imOut, DEFAULT_SE(seSize));
184  }
185 
188 } // namespace smil
189 
190 
191 
192 
193 #endif // _D_MORPHO_BASE_HPP
194 
Definition: DColorConvert.h:38
Definition: DBaseImage.h:235
Definition: DMorphImageOperations.hpp:133
RES_T dilate(const Image< T > &imIn, Image< T > &imOut, UINT seSize, T borderVal=ImDtTypes< T >::min())
Definition: DMorphoBase.hpp:81
RES_T erode(const Image< T > &imIn, Image< T > &imOut, UINT seSize, T borderVal=ImDtTypes< T >::max())
Definition: DMorphoBase.hpp:117
RES_T close(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Morphological grayscale closing.
Definition: DMorphoBase.hpp:127
Base structuring element.
Definition: DStructuringElement.h:51
Definition: DTypes.hpp:78
Main Image class.
Definition: DQVtkViewer.hpp:44
RES_T open(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Morphological grayscale opening.
Definition: DMorphoBase.hpp:159