SMIL  0.9.1
DHitOrMiss.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_THINNING_HPP
31 #define _D_THINNING_HPP
32 
33 #include "Morpho/include/DCompositeSE.h"
34 #include "Morpho/include/private/DMorphoBase.hpp"
35 
36 
37 namespace smil
38 {
49  template <class T>
50  RES_T hitOrMiss(const Image<T> &imIn, const StrElt &foreSE, const StrElt &backSE, Image<T> &imOut, T borderVal=ImDtTypes<T>::min())
51  {
52  ASSERT_ALLOCATED(&imIn, &imOut);
53  ASSERT_SAME_SIZE(&imIn, &imOut);
54 
55  Image<T> tmpIm(imIn);
56  ASSERT_ALLOCATED(&tmpIm);
57  ImageFreezer freezer(imOut);
58  ASSERT((inv<T>(imIn, tmpIm)==RES_OK));
59  ASSERT((erode(tmpIm, imOut, backSE, borderVal)==RES_OK));
60  ASSERT((erode(imIn, tmpIm, foreSE, borderVal)==RES_OK));
61  ASSERT((inf(tmpIm, imOut, imOut)==RES_OK));
62 
63  return RES_OK;
64  }
65 
69  template <class T>
70  RES_T hitOrMiss(const Image<T> &imIn, const CompStrElt &compSE, Image<T> &imOut, T borderVal=ImDtTypes<T>::min())
71  {
72  return hitOrMiss(imIn, compSE.fgSE, compSE.bgSE, imOut, borderVal);
73  }
74 
78  template <class T>
79  RES_T hitOrMiss(const Image<T> &imIn, const CompStrEltList &mhtSE, Image<T> &imOut, T borderVal=ImDtTypes<T>::min())
80  {
81  ASSERT_ALLOCATED(&imIn, &imOut);
82  ASSERT_SAME_SIZE(&imIn, &imOut);
83 
84  Image<T> tmpIm(imIn);
85  ASSERT_ALLOCATED(&tmpIm);
86 
87  ImageFreezer freezer(imOut);
88  ASSERT((fill(imOut, ImDtTypes<T>::min())==RES_OK));
89  for (std::vector<CompStrElt>::const_iterator it=mhtSE.compSeList.begin();it!=mhtSE.compSeList.end();it++)
90  {
91  ASSERT((hitOrMiss<T>(imIn, (*it).fgSE, (*it).bgSE, tmpIm, borderVal)==RES_OK));
92  ASSERT((sup(imOut, tmpIm, imOut)==RES_OK));
93  }
94 
95  return RES_OK;
96  }
97 
101  template <class T>
102  RES_T thin(const Image<T> &imIn, const StrElt &foreSE, const StrElt &backSE, Image<T> &imOut)
103  {
104  ASSERT_ALLOCATED(&imIn, &imOut);
105  ASSERT_SAME_SIZE(&imIn, &imOut);
106 
107  Image<T> tmpIm(imIn);
108  ASSERT_ALLOCATED(&tmpIm);
109  ImageFreezer freezer(imOut);
110 
111  ASSERT((hitOrMiss(imIn, foreSE, backSE, tmpIm)==RES_OK));
112  ASSERT((inv(tmpIm, tmpIm)==RES_OK));
113  ASSERT((inf(imIn, tmpIm, imOut)==RES_OK));
114 
115  return RES_OK;
116  }
117 
118  template <class T>
119  RES_T thin(const Image<T> &imIn, const CompStrElt &compSE, Image<T> &imOut)
120  {
121  return thin(imIn, compSE.fgSE, compSE.bgSE, imOut);
122  }
123 
124  template <class T>
125  RES_T thin(const Image<T> &imIn, const CompStrEltList &mhtSE, Image<T> &imOut)
126  {
127  ASSERT_ALLOCATED(&imIn, &imOut);
128  ASSERT_SAME_SIZE(&imIn, &imOut);
129 
130  Image<T> tmpIm(imIn, true); // clone
131  ASSERT_ALLOCATED(&tmpIm);
132 
133  ImageFreezer freezer(imOut);
134  ASSERT((fill(imOut, ImDtTypes<T>::min())==RES_OK));
135  for (std::vector<CompStrElt>::const_iterator it=mhtSE.compSeList.begin();it!=mhtSE.compSeList.end();it++)
136  {
137  ASSERT((thin<T>(tmpIm, (*it).fgSE, (*it).bgSE, tmpIm)==RES_OK));
138  }
139  copy(tmpIm, imOut);
140 
141  return RES_OK;
142  }
143 
144 
145 
149  template <class T>
150  RES_T thick(const Image<T> &imIn, const StrElt &foreSE, const StrElt &backSE, Image<T> &imOut)
151  {
152  ASSERT_ALLOCATED(&imIn, &imOut);
153  ASSERT_SAME_SIZE(&imIn, &imOut);
154 
155  Image<T> tmpIm(imIn);
156  ASSERT_ALLOCATED(&tmpIm);
157  ImageFreezer freezer(imOut);
158 
159  ASSERT((hitOrMiss(imIn, foreSE, backSE, tmpIm)==RES_OK));
160  ASSERT((sup(imIn, tmpIm, imOut)==RES_OK));
161 
162  return RES_OK;
163  }
164 
165  template <class T>
166  RES_T thick(const Image<T> &imIn, const CompStrElt &compSE, Image<T> &imOut)
167  {
168  return thick(imIn, compSE.fgSE, compSE.bgSE, imOut);
169  }
170 
171  template <class T>
172  RES_T thick(const Image<T> &imIn, const CompStrEltList &mhtSE, Image<T> &imOut)
173  {
174  ASSERT_ALLOCATED(&imIn, &imOut);
175  ASSERT_SAME_SIZE(&imIn, &imOut);
176 
177  Image<T> tmpIm(imIn, true); // clone
178  ASSERT_ALLOCATED(&tmpIm);
179 
180  ImageFreezer freezer(imOut);
181  ASSERT((fill(imOut, ImDtTypes<T>::min())==RES_OK));
182  for (std::vector<CompStrElt>::const_iterator it=mhtSE.compSeList.begin();it!=mhtSE.compSeList.end();it++)
183  {
184  ASSERT((thick<T>(tmpIm, (*it).fgSE, (*it).bgSE, tmpIm)==RES_OK));
185  }
186  copy(tmpIm, imOut);
187 
188  return RES_OK;
189  }
190 
191 
192 
196  template <class T>
197  RES_T fullThin(const Image<T> &imIn, const CompStrEltList &mhtSE, Image<T> &imOut)
198  {
199  ASSERT_ALLOCATED(&imIn, &imOut);
200  ASSERT_SAME_SIZE(&imIn, &imOut);
201 
202  ImageFreezer freezer(imOut);
203 
204  double v1, v2;
205  ASSERT((thin<T>(imIn, mhtSE, imOut)==RES_OK));
206  v1 = vol(imOut);
207  while(true)
208  {
209  ASSERT((thin<T>(imOut, mhtSE, imOut)==RES_OK));
210  v2 = vol(imOut);
211  if (v2==v1)
212  break;
213  v1 = v2;
214  }
215 
216  return RES_OK;
217  }
218 
219  template <class T>
220  RES_T fullThin(const Image<T> &imIn, const CompStrElt &compSE, Image<T> &imOut)
221  {
222  return fullThin(imIn, CompStrEltList(compSE), imOut);
223  }
224 
225  template <class T>
226  RES_T fullThin(const Image<T> &imIn, const StrElt &foreSE, const StrElt &backSE, Image<T> &imOut)
227  {
228  return fullThin(imIn, CompStrEltList(CompStrElt(foreSE, backSE)), imOut);
229  }
230 
234  template <class T>
235  RES_T fullThick(const Image<T> &imIn, const CompStrEltList &mhtSE, Image<T> &imOut)
236  {
237  ASSERT_ALLOCATED(&imIn, &imOut);
238  ASSERT_SAME_SIZE(&imIn, &imOut);
239 
240  ImageFreezer freezer(imOut);
241  double v1, v2;
242  ASSERT((thick<T>(imIn, mhtSE, imOut)==RES_OK));
243  v1 = vol(imOut);
244  while(true)
245  {
246  ASSERT((thick<T>(imOut, mhtSE, imOut)==RES_OK));
247  v2 = vol(imOut);
248  if (v2==v1)
249  break;
250  v1 = v2;
251  }
252 
253  return RES_OK;
254  }
255 
256  template <class T>
257  RES_T fullThick(const Image<T> &imIn, const CompStrElt &compSE, Image<T> &imOut)
258  {
259  return fullThick(imIn, CompStrEltList(compSE), imOut);
260  }
261 
262  template <class T>
263  RES_T fullThick(const Image<T> &imIn, const StrElt &foreSE, const StrElt &backSE, Image<T> &imOut)
264  {
265  return fullThick(imIn, CompStrEltList(CompStrElt(foreSE, backSE)), imOut);
266  }
267 
270 } // namespace smil
271 
272 
273 #endif // _D_THINNING_HPP
274 
Definition: DColorConvert.h:38
RES_T copy(const Image< T1 > &imIn, size_t startX, size_t startY, size_t startZ, size_t sizeX, size_t sizeY, size_t sizeZ, Image< T2 > &imOut, size_t outStartX=0, size_t outStartY=0, size_t outStartZ=0)
Copy image.
Definition: DImageArith.hpp:114
RES_T thin(const Image< T > &imIn, const StrElt &foreSE, const StrElt &backSE, Image< T > &imOut)
Thinning transform.
Definition: DHitOrMiss.hpp:102
Definition: DBaseImage.h:235
double vol(const Image< T > &imIn)
Volume of an image.
Definition: DMeasures.hpp:96
RES_T inv(const Image< T > &imIn, Image< T > &imOut)
Invert an image.
Definition: DImageArith.hpp:375
RES_T fill(Image< T > &imOut, const T &value)
Fill an image with a given value.
Definition: DImageArith.hpp:62
Composite structuring element list.
Definition: DCompositeSE.h:74
RES_T sup(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
Sup of two images.
Definition: DImageArith.hpp:505
RES_T fullThick(const Image< T > &imIn, const CompStrEltList &mhtSE, Image< T > &imOut)
Thicking transform (full)
Definition: DHitOrMiss.hpp:235
Composite structuring element.
Definition: DCompositeSE.h:50
Base structuring element.
Definition: DStructuringElement.h:51
RES_T hitOrMiss(const Image< T > &imIn, const CompStrEltList &mhtSE, Image< T > &imOut, T borderVal=ImDtTypes< T >::min())
Hit Or Miss transform.
Definition: DHitOrMiss.hpp:79
Definition: DTypes.hpp:78
Main Image class.
Definition: DQVtkViewer.hpp:44
RES_T erode(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE, T borderVal=ImDtTypes< T >::max())
Morphological grayscale erosion.
Definition: DMorphoBase.hpp:101
RES_T inf(const Image< T > &imIn1, const T &value, Image< T > &imOut)
Inf of two images.
Definition: DImageArith.hpp:538
RES_T fullThin(const Image< T > &imIn, const CompStrEltList &mhtSE, Image< T > &imOut)
Thinning transform (full)
Definition: DHitOrMiss.hpp:197
RES_T thick(const Image< T > &imIn, const StrElt &foreSE, const StrElt &backSE, Image< T > &imOut)
Thicking transform.
Definition: DHitOrMiss.hpp:150