ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkParabolicMorphUtils.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkParabolicMorphUtils_h
19#define itkParabolicMorphUtils_h
20
21#include <itkArray.h>
22
23#include "itkProgressReporter.h"
24
25namespace itk
26{
27// contact point algorithm
28template <typename LineBufferType, typename RealType, typename TInputPixel, bool doDilate>
29void
30DoLineCP(LineBufferType & LineBuf, LineBufferType & tmpLineBuf, const RealType magnitude)
31{
32 static constexpr RealType extreme =
34
35 // contact point algorithm
36 long koffset = 0, newcontact = 0; // how far away the search starts.
37
38 const long LineLength = LineBuf.size();
39
40 // negative half of the parabola
41 for (long pos = 0; pos < LineLength; pos++)
42 {
43 auto BaseVal = extreme; // the base value for comparison
44 for (long krange = koffset; krange <= 0; krange++)
45 {
46 // difference needs to be paramaterised
47 RealType T = LineBuf[pos + krange] - magnitude * krange * krange;
48 // switch on template parameter - hopefully gets optimized away.
49 if (doDilate ? (T >= BaseVal) : (T <= BaseVal))
50 {
51 BaseVal = T;
52 newcontact = krange;
53 }
54 }
55 tmpLineBuf[pos] = BaseVal;
56 koffset = newcontact - 1;
57 }
58 // positive half of parabola
59 koffset = newcontact = 0;
60 for (long pos = LineLength - 1; pos >= 0; pos--)
61 {
62 auto BaseVal = extreme; // the base value for comparison
63 for (long krange = koffset; krange >= 0; krange--)
64 {
65 RealType T = tmpLineBuf[pos + krange] - magnitude * krange * krange;
66 if (doDilate ? (T >= BaseVal) : (T <= BaseVal))
67 {
68 BaseVal = T;
69 newcontact = krange;
70 }
71 }
72 LineBuf[pos] = BaseVal;
73 koffset = newcontact + 1;
74 }
75}
76
77// intersection algorithm
78// This algorithm has been described a couple of times. First by van
79// den Boomgaard and more recently by Felzenszwalb and Huttenlocher,
80// in the context of generalized distance transform
81template <typename LineBufferType, typename IndexBufferType, typename EnvBufferType, typename RealType, bool doDilate>
82void
83DoLineIntAlg(LineBufferType & LineBuf,
84 EnvBufferType & F,
85 IndexBufferType & v,
86 EnvBufferType & z,
87 const RealType magnitude)
88{
89 int k; /* Index of rightmost parabola in lower envelope */
90 /* Locations of parabolas in lower envelope */
91 /* these need to be int, rather than unsigned, to make boundary
92 conditions easy to test */
93
94 // v stores locations of parabolas in the lower envelope
95 // z stores thr location of boundaries between parabolas
96
97 // I've gone nuts with the static casts etc, because I seemed to
98 // have strange behaviour when I didn't do this. Also managed to get
99 // rid of all the warnings by sticking to size_t and equivalents.
100 RealType s;
101
102 /* holds precomputed scale*f(q) + q^2 for speedup */
103 // LineBufferType F(LineBuf.size());
104
105 // initialize
106 k = 0;
107 v[0] = 0;
110 F[0] = LineBuf[0] / magnitude;
111 const size_t N(LineBuf.size());
112
113 for (size_t q = 1; q < N; q++) /* main loop */
114 {
115 if (doDilate)
116 {
117 /* precompute f(q) + q^2 for speedup */
118 F[q] = (LineBuf[q] / magnitude) - (static_cast<RealType>(q) * static_cast<RealType>(q));
119 k++;
120 do
121 {
122 /* remove last parabola from surface */
123 k--;
124 /* compute intersection */
125 s = (F[q] - F[v[k]]) / (2.0 * (v[k] - static_cast<RealType>(q)));
126 } while (s <= z[k]);
127 /* bump k to add new parabola */
128 k++;
129 }
130 else
131 {
132 /* precompute f(q) + q^2 for speedup */
133 F[q] = (LineBuf[q] / magnitude) + (static_cast<RealType>(q) * static_cast<RealType>(q));
134 k++;
135 do
136 {
137 /* remove last parabola from surface */
138 k--;
139 /* compute intersection */
140 s = (F[q] - F[v[k]]) / (2.0 * (static_cast<RealType>(q) - v[k]));
141 } while (s <= z[k]);
142 /* bump k to add new parabola */
143 k++;
144 }
145 v[k] = q;
146 z[k] = s;
147 itkAssertInDebugAndIgnoreInReleaseMacro((size_t)(k + 1) <= N);
148 z[k + 1] = NumericTraits<int>::max();
149 } /* for q */
150 /* now reconstruct output */
151 if (doDilate)
152 {
153 k = 0;
154 for (size_t q = 0; q < N; q++)
155 {
156 while (z[k + 1] < static_cast<typename IndexBufferType::ValueType>(q))
157 {
158 k++;
159 }
160 itkAssertInDebugAndIgnoreInReleaseMacro(static_cast<size_t>(v[k]) < N);
161 itkAssertInDebugAndIgnoreInReleaseMacro(static_cast<size_t>(v[k]) >= 0);
162 LineBuf[q] = static_cast<RealType>(
163 (F[v[k]] - (static_cast<RealType>(q) * (static_cast<RealType>(q) - 2 * v[k]))) * magnitude);
164 }
165 }
166 else
167 {
168 k = 0;
169 for (size_t q = 0; q < N; q++)
170 {
171 while (z[k + 1] < static_cast<typename IndexBufferType::ValueType>(q))
172 {
173 k++;
174 }
175 itkAssertInDebugAndIgnoreInReleaseMacro(static_cast<size_t>(v[k]) < N);
176 itkAssertInDebugAndIgnoreInReleaseMacro(static_cast<size_t>(v[k]) >= 0);
177 LineBuf[q] = ((static_cast<RealType>(q) * (static_cast<RealType>(q) - 2 * v[k]) + F[v[k]]) * magnitude);
178 }
179 }
180}
181
182template <typename TInIter,
183 typename TOutIter,
184 typename RealType,
185 typename TInputPixel,
186 typename OutputPixelType,
187 bool doDilate>
188void
189doOneDimension(TInIter & inputIterator,
190 TOutIter & outputIterator,
191 ProgressReporter & progress,
192 const long LineLength,
193 const unsigned direction,
194 const bool m_UseImageSpacing,
195 const RealType image_scale,
196 const RealType Sigma,
197 int ParabolicAlgorithmChoice)
198{
199 enum ParabolicAlgorithm
200 {
201 NOCHOICE = 0, // decices based on scale - experimental
202 CONTACTPOINT = 1, // sometimes faster at low scale
203 INTERSECTION = 2 // default
204 };
205
206 // using LineBufferType = typename std::vector<RealType>;
207
208 // message from M.Starring suggested performance gain using Array
209 // instead of std::vector.
210 using LineBufferType = typename itk::Array<RealType>;
211 RealType iscale = 1.0;
212 if (m_UseImageSpacing)
213 {
214 iscale = image_scale;
215 }
216 if (ParabolicAlgorithmChoice == NOCHOICE)
217 {
218 // both set to true or false - use scale to figure it out
219 if ((2.0 * Sigma) < 0.2)
220 {
221 ParabolicAlgorithmChoice = CONTACTPOINT;
222 }
223 else
224 {
225 ParabolicAlgorithmChoice = INTERSECTION;
226 }
227 }
228
229 if (ParabolicAlgorithmChoice == CONTACTPOINT)
230 {
231 // using the contact point algorithm
232
233 // const RealType magnitude = magnitudeSign * 1.0/(2.0 *
234 // Sigma/(iscale*iscale));
235 // restructure equation to reduce numerical error
236 constexpr int magnitudeSign = doDilate ? 1 : -1;
237 const RealType magnitudeCP = (magnitudeSign * iscale * iscale) / (2.0 * Sigma);
238
239 LineBufferType LineBuf(LineLength);
240 LineBufferType tmpLineBuf(LineLength);
241 inputIterator.SetDirection(direction);
242 outputIterator.SetDirection(direction);
243 inputIterator.GoToBegin();
244 outputIterator.GoToBegin();
245
246 while (!inputIterator.IsAtEnd() && !outputIterator.IsAtEnd())
247 {
248 // process this direction
249 // fetch the line into the buffer - this methodology is like
250 // the gaussian filters
251
252 unsigned int i = 0;
253 while (!inputIterator.IsAtEndOfLine())
254 {
255 LineBuf[i++] = static_cast<RealType>(inputIterator.Get());
256 ++inputIterator;
257 }
258
259 DoLineCP<LineBufferType, RealType, TInputPixel, doDilate>(LineBuf, tmpLineBuf, magnitudeCP);
260 // copy the line back
261 unsigned int j = 0;
262 while (!outputIterator.IsAtEndOfLine())
263 {
264 outputIterator.Set(static_cast<OutputPixelType>(LineBuf[j++]));
265 ++outputIterator;
266 }
267
268 // now onto the next line
269 inputIterator.NextLine();
270 outputIterator.NextLine();
271 progress.CompletedPixel();
272 }
273 }
274 else
275 {
276 // using the Intersection algorithm
277 using IndexBufferType = typename itk::Array<int>;
278
279 const RealType magnitudeInt = (iscale * iscale) / (2.0 * Sigma);
280 LineBufferType LineBuf(LineLength);
281 LineBufferType Fbuf(LineLength);
282 IndexBufferType Vbuf(LineLength);
283 LineBufferType Zbuf(LineLength + 1);
284
285 inputIterator.SetDirection(direction);
286 outputIterator.SetDirection(direction);
287 inputIterator.GoToBegin();
288 outputIterator.GoToBegin();
289
290 while (!inputIterator.IsAtEnd() && !outputIterator.IsAtEnd())
291 {
292 // process this direction
293 // fetch the line into the buffer - this methodology is like
294 // the gaussian filters
295
296 unsigned int i = 0;
297 while (!inputIterator.IsAtEndOfLine())
298 {
299 LineBuf[i++] = static_cast<RealType>(inputIterator.Get());
300 ++inputIterator;
301 }
303 LineBuf, Fbuf, Vbuf, Zbuf, magnitudeInt);
304 // copy the line back
305 unsigned int j = 0;
306 while (!outputIterator.IsAtEndOfLine())
307 {
308 outputIterator.Set(static_cast<OutputPixelType>(LineBuf[j++]));
309 ++outputIterator;
310 }
311
312 // now onto the next line
313 inputIterator.NextLine();
314 outputIterator.NextLine();
315 progress.CompletedPixel();
316 }
317 }
318}
319} // namespace itk
320#endif
Array class with size defined at construction time.
Definition itkArray.h:48
static constexpr T NonpositiveMin()
static constexpr T max(const T &)
Implements progress tracking for a filter.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void DoLineCP(LineBufferType &LineBuf, LineBufferType &tmpLineBuf, const RealType magnitude)
void DoLineIntAlg(LineBufferType &LineBuf, EnvBufferType &F, IndexBufferType &v, EnvBufferType &z, const RealType magnitude)
void doOneDimension(TInIter &inputIterator, TOutIter &outputIterator, ProgressReporter &progress, const long LineLength, const unsigned direction, const bool m_UseImageSpacing, const RealType image_scale, const RealType Sigma, int ParabolicAlgorithmChoice)