SMIL  1.0.4
DSkeleton.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''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _D_SKELETON_HPP
31 #define _D_SKELETON_HPP
32 
33 #include "DMorphoBase.hpp"
34 #include "DHitOrMiss.hpp"
35 
36 namespace smil
37 {
69  template <class T>
70  RES_T skiz(const Image<T> &imIn, Image<T> &imOut)
71  {
72  ASSERT_ALLOCATED(&imIn, &imOut);
73  ASSERT_SAME_SIZE(&imIn, &imOut);
74 
75  ImageFreezer freezer(imOut);
76  Image<T> tmpIm(imIn);
77  inv(imIn, imOut);
78  fullThin(imOut, HMT_hL(6), tmpIm);
79  fullThin(tmpIm, HMT_hM(6), imOut);
80 
81  return RES_OK;
82  }
83 
91  template <class T>
92  RES_T skeleton(const Image<T> &imIn, Image<T> &imOut,
93  const StrElt &se = DEFAULT_SE)
94  {
95  ASSERT_ALLOCATED(&imIn, &imOut);
96  ASSERT_SAME_SIZE(&imIn, &imOut);
97 
98  ImageFreezer freezer(imOut);
99 
100  Image<T> imEro(imIn);
101  Image<T> imTemp(imIn);
102 
103  copy(imIn, imEro);
104  fill(imOut, ImDtTypes<T>::min());
105 
106  bool idempt = false;
107 
108  do {
109  erode(imEro, imEro, se);
110  open(imEro, imTemp, se);
111  sub(imEro, imTemp, imTemp);
112  sup(imOut, imTemp, imTemp);
113  idempt = equ(imTemp, imOut);
114  copy(imTemp, imOut);
115  } while (!idempt);
116 
117  return RES_OK;
118  }
119 
127  template <class T1, class T2>
128  RES_T extinctionValues(const Image<T1> &imIn, Image<T2> &imOut,
129  const StrElt &se = DEFAULT_SE)
130  {
131  ASSERT_ALLOCATED(&imIn, &imOut);
132  ASSERT_SAME_SIZE(&imIn, &imOut);
133 
134  ImageFreezer freezer(imOut);
135 
136  Image<T1> imEro(imIn);
137  Image<T1> imTemp1(imIn);
138  Image<T2> imTemp2(imOut);
139 
140  copy(imIn, imEro);
141  fill(imOut, ImDtTypes<T2>::min());
142 
143  T2 r = 1;
144  bool idempt = false;
145  do {
146  erode(imEro, imEro, se);
147  open(imEro, imTemp1, se);
148  sub(imEro, imTemp1, imTemp1);
149  test(imTemp1, r++, imOut, imTemp2);
150  idempt = equ(imTemp2, imOut);
151  copy(imTemp2, imOut);
152  } while (!idempt);
153 
154  return RES_OK;
155  }
156 
157 
158  /*
159  *
160  * ##### ##### # # # # ######
161  * # # # # # # ## # #
162  * # # # # # # # # # #####
163  * ##### ##### # # # # # #
164  * # # # # # # ## #
165  * # # # #### # # ######
166  *
167  */
168 
177  template <class T>
178  RES_T pruneSkiz(const Image<T> &imIn, Image<T> &imOut,
179  const StrElt &se = DEFAULT_SE)
180  {
181  ASSERT_ALLOCATED(&imIn, &imOut);
182  ASSERT_SAME_SIZE(&imIn, &imOut);
183 
184  T *in = imIn.getPixels();
185  T *out = imOut.getPixels();
186 
187  fill<T>(imOut, T(0));
188 
189  size_t Size[3];
190  imIn.getSize(Size);
191 
192  size_t nbrPixels = Size[0] * Size[1] * Size[2];
193  size_t sePtsNumber = se.points.size();
194 
195 #ifdef USE_OPEN_MP
196  UINT nthreads = Core::getInstance()->getNumberOfThreads();
197 #pragma omp parallel num_threads(nthreads)
198 #endif
199  {
200 #ifdef USE_OPEN_MP
201 #pragma omp for
202 #endif
203  for (size_t i = 0; i < nbrPixels; ++i) {
204  ImageBox pt(Size);
205  pt.setReference(i);
206 
207  bool up = false;
208  bool down = false;
209  if (in[pt.reference] > 0 && in[pt.reference] != ImDtTypes<T>::max()) {
210  for (UINT pts = 0; pts < sePtsNumber; ++pts) {
211  ImageBox qt(Size);
212  qt = pt;
213  qt.shift(se.points[pts]);
214 
215  if (qt.inImage()) {
216  if (in[qt.reference] != ImDtTypes<T>::max()) {
217  if (in[qt.reference] >= in[pt.reference] + 1) {
218  up = true;
219  }
220  if (in[qt.reference] <= in[pt.reference] - 1) {
221  down = true;
222  }
223  }
224  }
225  }
226 
227  if (!up || !down) {
228  out[pt.reference] = in[pt.reference];
229  }
230  }
231  }
232  }
233 
234  return RES_OK;
235  }
236 
239 } // namespace smil
240 
241 #endif // _D_SKELETON_HPP
void getSize(size_t *w, size_t *h, size_t *d) const
Get image size.
Definition: DBaseImage.h:118
Hexagonal L ([1,2], [4,5])
Definition: DCompositeSE.h:216
Hexagonal M ([1], [3,4,5])
Definition: DCompositeSE.h:238
ImageBox.
Definition: DCommon.h:267
void setReference(off_t x, off_t y, off_t z=0)
setReference() - set reference point
Definition: DCommon.h:320
bool inImage()
inImage() - check if the reference point is inside image bounds
Definition: DCommon.h:396
void shift(off_t dx, off_t dy, off_t dz=0)
shift() - move the point by some displacements
Definition: DCommon.h:371
Definition: DBaseImage.h:386
Main Image class.
Definition: DImage.hpp:57
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:105
Base structuring element.
Definition: DStructuringElement.h:68
RES_T sub(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
sub() - Subtraction between two images
Definition: DImageArith.hpp:160
RES_T inv(const Image< T > &imIn, Image< T > &imOut)
inv() - Invert an image.
Definition: DImageArith.hpp:70
RES_T sup(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
sup() - Sup of two images
Definition: DImageArith.hpp:254
RES_T equ(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
equ() - Equality operator (pixel by pixel)
Definition: DImageArith.hpp:366
RES_T test(const Image< T1 > &imIn, const Image< T2 > &imInT, const Image< T2 > &imInF, Image< T2 > &imOut)
test() - Test
Definition: DImageArith.hpp:1109
RES_T fill(Image< T > &imOut, const T &value)
fill() - Fill an image with a given value.
Definition: DImageArith.hpp:1456
RES_T open(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
open() - Morphological grayscale opening
Definition: DMorphoFilter.hpp:123
RES_T fullThin(const Image< T > &imIn, const CompStrEltList &mhtSE, Image< T > &imOut)
fullThin() - Thinning transform (full)
Definition: DHitOrMiss.hpp:303
RES_T erode(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE, const T borderVal=ImDtTypes< T >::max())
erode() - Morphological grayscale erosion
Definition: DMorphoBase.hpp:112
RES_T skeleton(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
skeleton() - Morphological skeleton
Definition: DSkeleton.hpp:92
RES_T pruneSkiz(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
pruneSkiz() -
Definition: DSkeleton.hpp:178
RES_T skiz(const Image< T > &imIn, Image< T > &imOut)
skiz() - Skeleton by Influence Zones (Skiz)
Definition: DSkeleton.hpp:70
RES_T extinctionValues(const Image< T1 > &imIn, Image< T2 > &imOut, const StrElt &se=DEFAULT_SE)
extinctionValues() - Extinction values
Definition: DSkeleton.hpp:128
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() - Copy image (or a zone) into an output image
Definition: DImageTransform.hpp:84
Definition: DTypes.hpp:88