1 /*============================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center (DKFZ)
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
11 ============================================================================*/
13 /*===================================================================
15 This file is based heavily on a corresponding ITK filter.
17 ===================================================================*/
19 #ifndef __itkContourExtractor2DImageFilter_txx
20 #define __itkContourExtractor2DImageFilter_txx
22 #include "itkConstShapedNeighborhoodIterator.h"
23 #include "itkConstShapedNeighborhoodIterator.h"
24 #include "itkContourExtractor2DImageFilter.h"
25 #include "itkProgressReporter.h"
26 #include "vcl_cmath.h"
31 template <class TInputImage>
32 ContourExtractor2DImageFilter<TInputImage>::ContourExtractor2DImageFilter()
34 this->m_ContourValue = NumericTraits<InputRealType>::Zero;
35 this->m_ReverseContourOrientation = false;
36 this->m_VertexConnectHighPixels = false;
37 this->m_UseCustomRegion = false;
38 this->m_NumberOfContoursCreated = 0;
42 template <class TInputImage>
43 ContourExtractor2DImageFilter<TInputImage>::~ContourExtractor2DImageFilter()
47 template <class TInputImage>
48 void ContourExtractor2DImageFilter<TInputImage>::GenerateData()
50 // Make sure the structures for containing, looking up, and numbering the
51 // growing contours are empty and ready.
53 m_ContourStarts.clear();
54 m_ContourEnds.clear();
55 m_NumberOfContoursCreated = 0;
57 // Set up an iterator to "march the squares" across the image.
58 // We associate each 2px-by-2px square with the pixel in the upper left of
59 // that square. We then iterate across the image, examining these 2x2 squares
60 // and building the contour. By iterating the upper-left pixel of our
61 // "current square" across every pixel in the image except those on the
62 // bottom row and rightmost column, we have visited every valid square in the
65 InputRegionType region = this->GetInput()->GetRequestedRegion();
66 typename InputRegionType::SizeType shrunkSize = region.GetSize();
69 InputRegionType shrunkRegion(region.GetIndex(), shrunkSize);
71 // Set up a progress reporter
72 ProgressReporter progress(this, 0, shrunkRegion.GetNumberOfPixels());
74 // A 1-pixel radius sets up a neighborhood with the following indices:
78 // We are interested only in the square of 4,5,7,8 which is the 2x2 square
79 // with the center pixel at the top-left. So we only activate the
80 // coresponding offsets, and only query pixels 4, 5, 7, and 8 with the
81 // iterator's GetPixel method.
82 typedef ConstShapedNeighborhoodIterator<InputImageType> SquareIterator;
83 typename SquareIterator::RadiusType radius = {{1, 1}};
84 SquareIterator it(radius, this->GetInput(), shrunkRegion);
85 InputOffsetType none = {{0, 0}};
86 InputOffsetType right = {{1, 0}};
87 InputOffsetType down = {{0, 1}};
88 InputOffsetType diag = {{1, 1}};
89 it.ActivateOffset(none);
90 it.ActivateOffset(right);
91 it.ActivateOffset(down);
92 it.ActivateOffset(diag);
94 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
96 // There are sixteen different possible square types, diagramed below.
97 // A + indicates that the vertex is above the contour value, and a -
98 // indicates that the vertex is below or equal to the contour value.
99 // The vertices of each square are here numbered:
102 // and treated as a binary value with the bits in that order. Thus each
103 // square can be so numbered:
104 // 0-- 1+- 2-+ 3++ 4-- 5+- 6-+ 7++
105 // -- -- -- -- +- +- +- +-
107 // 8-- 9+- 10-+ 11++ 12-- 13+- 14-+ 15++
108 // -+ -+ -+ -+ ++ ++ ++ ++
110 // The position of the line segment that cuts through (or doesn't, in case
111 // 0 and 15) each square is clear, except in cases 6 and 9. In this case,
112 // where the segments are placed is determined by
113 // m_VertexConnectHighPixels. If m_VertexConnectHighPixels is false, then
114 // lines like are drawn through square 6, and lines like are drawn through
115 // square 9. Otherwise, the situation is reversed.
116 // Finally, recall that we draw the lines so that (moving from tail to
117 // head) the lower-valued pixels are on the left of the line. So, for
118 // example, case 1 entails a line slanting from the middle of the top of
119 // the square to the middle of the left side of the square.
121 // (1) Determine what number square we are currently inspecting. Remember
122 // that as far as the neighborhood iterator is concerned, our square
123 // 01 is numbered as 45
126 InputPixelType v0, v1, v2, v3;
131 InputIndexType index = it.GetIndex();
132 unsigned char squareCase = 0;
133 if (v0 > m_ContourValue)
135 if (v1 > m_ContourValue)
137 if (v2 > m_ContourValue)
139 if (v3 > m_ContourValue)
142 // Set up macros to find the ContinuousIndex where the contour intersects
143 // one of the sides of the square. Normally macros should, of course, be
144 // eschewed, but since this is an inner loop not calling the function four
145 // times when two would do is probably worth while. Plus, copy-pasting
146 // these into the switch below is even worse. InterpolateContourPosition
147 // takes the values at two vertices, the index of the first vertex, and the
148 // offset between the two vertices.
149 #define TOP_ this->InterpolateContourPosition(v0, v1, index, right)
150 #define BOTTOM_ this->InterpolateContourPosition(v2, v3, index + down, right)
151 #define LEFT_ this->InterpolateContourPosition(v0, v2, index, down)
152 #define RIGHT_ this->InterpolateContourPosition(v1, v3, index + right, down)
154 // (2) Add line segments to the growing contours as defined by the cases.
155 // AddSegment takes a "from" vertex and a "to" vertex, and adds it to the
156 // a growing contour, creates a new contour, or merges two together.
161 case 1: // top to left
162 this->AddSegment(TOP_, LEFT_);
164 case 2: // right to top
165 this->AddSegment(RIGHT_, TOP_);
167 case 3: // right to left
168 this->AddSegment(RIGHT_, LEFT_);
170 case 4: // left to bottom
171 this->AddSegment(LEFT_, BOTTOM_);
173 case 5: // top to bottom
174 this->AddSegment(TOP_, BOTTOM_);
177 if (m_VertexConnectHighPixels)
180 this->AddSegment(LEFT_, TOP_);
182 this->AddSegment(RIGHT_, BOTTOM_);
187 this->AddSegment(RIGHT_, TOP_);
189 this->AddSegment(LEFT_, BOTTOM_);
192 case 7: // right to bottom
193 this->AddSegment(RIGHT_, BOTTOM_);
195 case 8: // bottom to right
196 this->AddSegment(BOTTOM_, RIGHT_);
199 if (m_VertexConnectHighPixels)
202 this->AddSegment(TOP_, RIGHT_);
204 this->AddSegment(BOTTOM_, LEFT_);
209 this->AddSegment(TOP_, LEFT_);
211 this->AddSegment(BOTTOM_, RIGHT_);
214 case 10: // bottom to top
215 this->AddSegment(BOTTOM_, TOP_);
217 case 11: // bottom to left
218 this->AddSegment(BOTTOM_, LEFT_);
220 case 12: // left to right
221 this->AddSegment(LEFT_, RIGHT_);
223 case 13: // top to right
224 this->AddSegment(TOP_, RIGHT_);
226 case 14: // left to top
227 this->AddSegment(LEFT_, TOP_);
231 } // switch squareCase
232 progress.CompletedPixel();
235 // Now create the outputs paths from the deques we've been using.
238 m_ContourStarts.clear();
239 m_ContourEnds.clear();
240 m_NumberOfContoursCreated = 0;
243 template <class TInputImage>
244 inline typename ContourExtractor2DImageFilter<TInputImage>::VertexType
245 ContourExtractor2DImageFilter<TInputImage>::InterpolateContourPosition(InputPixelType fromValue,
246 InputPixelType toValue,
247 InputIndexType fromIndex,
248 InputOffsetType toOffset)
251 // Now calculate the fraction of the way from 'from' to 'to' that the contour
252 // crosses. Interpolate linearly: y = v0 + (v1 - v0) * x, and solve for the
253 // x that gives y = m_ContourValue: x = (m_ContourValue - v0) / (v1 - v0).
254 // This assumes that v0 and v1 are separated by exactly ONE unit. So the to
255 // Offset. value must have exactly one component 1 and the other component 0.
256 // Also this assumes that fromValue and toValue are different. Otherwise we
257 // can't interpolate anything!
258 itkAssertOrThrowMacro((fromValue != toValue), "source and destination are the same");
260 itkAssertOrThrowMacro(((toOffset[0] == 0 && toOffset[1] == 1) || (toOffset[0] == 1 && toOffset[1] == 0)),
261 "toOffset has unexpected values");
264 (m_ContourValue - static_cast<InputRealType>(fromValue)) / (toValue - static_cast<InputRealType>(fromValue));
266 output[0] = fromIndex[0] + x * toOffset[0];
267 output[1] = fromIndex[1] + x * toOffset[1];
272 template <class TInputImage>
273 void ContourExtractor2DImageFilter<TInputImage>::AddSegment(VertexType from, VertexType to)
277 // Arc is degenerate: ignore, and the from/two point will be connected
278 // later by other squares. Degeneracy happens when (and only when) a square
279 // has exactly one vertex that is the contour value, and the rest are above
284 // Try to find an existing contour that starts where the new segment ends.
285 VertexMapIterator newTail = m_ContourStarts.find(to);
286 // Try to find an existing contour that ends where the new segment starts.
287 VertexMapIterator newHead = m_ContourEnds.find(from);
289 if (newTail != m_ContourStarts.end() && newHead != m_ContourEnds.end())
291 // We need to connect these two contours. The act of connecting them will
292 // add the needed arc.
293 auto tail = newTail->second;
294 itkAssertOrThrowMacro((tail->front() == to), "End doesn't match Beginning");
295 auto head = newHead->second;
296 itkAssertOrThrowMacro((head->back() == from), "Beginning doesn't match End");
299 // We've closed a contour. Add the end point, and remove from the maps
301 m_ContourStarts.erase(newTail);
302 // erase the front of tail. Because head and tail are the same contour,
303 // don't worry about erasing the front of head!
304 m_ContourEnds.erase(newHead); // erase the end of head/tail.
308 // We have found two distinct contours that need to be joined. Careful
309 // here: we want to keep the first segment in the list when merging so
310 // that contours are always returned in top-to-bottom, right-to-left
311 // order (with regard to the image pixel found to be inside the contour).
312 if (tail->m_ContourNumber > head->m_ContourNumber)
314 // if tail was created later than head...
315 // Copy tail to the end of head and remove
316 // tail from everything.
317 head->insert(head->end(), tail->begin(), tail->end());
319 // Now remove 'tail' from the list and the maps because it has been
321 m_ContourStarts.erase(newTail);
322 int erased = m_ContourEnds.erase(tail->back());
323 // There should be exactly one entry in the hash for that endpoint
326 itkWarningMacro(<< "There should be exactly one entry in the hash for that endpoint, but there are "
329 m_Contours.erase(tail); // remove from the master list
331 // Now remove the old end of 'head' from the ends map and add
333 m_ContourEnds.erase(newHead);
334 m_ContourEnds.insert(VertexContourRefPair(head->back(), head));
338 // Copy head to the beginning of tail and remove
339 // head from everything.
340 tail->insert(tail->begin(), head->begin(), head->end());
342 // Now remove 'head' from the list and the maps because
343 // it has been subsumed.
344 m_ContourEnds.erase(newHead);
345 int erased = m_ContourStarts.erase(head->front());
348 itkWarningMacro(<< "There should be exactly one entry in the hash for that endpoint, but there are "
351 m_Contours.erase(head); // remove from the master list
353 // Now remove the old start of 'tail' from the starts map and
354 // add the new start.
355 m_ContourStarts.erase(newTail);
356 m_ContourStarts.insert(VertexContourRefPair(tail->front(), tail));
360 else if (newTail == m_ContourStarts.end() && newHead == m_ContourEnds.end())
362 // No contours found: add a new one.
363 // Make it on the heap. It will be copied into m_Contours.
367 contour.push_front(from);
368 contour.push_back(to);
369 contour.m_ContourNumber = m_NumberOfContoursCreated++;
370 // Add the contour to the end of the list and get a reference to it.
371 m_Contours.push_back(contour);
373 // recall that end() is an iterator to one past the back!
374 auto newContour = --m_Contours.end();
375 // add the endpoints and an iterator pointing to the contour
376 // in the list to the maps.
377 m_ContourStarts.insert(VertexContourRefPair(from, newContour));
378 m_ContourEnds.insert(VertexContourRefPair(to, newContour));
380 else if (newTail != m_ContourStarts.end() && newHead == m_ContourEnds.end())
382 // Found a single contour to which the new arc should be prepended.
383 auto tail = newTail->second;
384 itkAssertOrThrowMacro((tail->front() == to), "End doesn't match Beginning");
385 tail->push_front(from);
386 // erase the old start of this contour
387 m_ContourStarts.erase(newTail);
388 // Now add the new start of this contour.
389 m_ContourStarts.insert(VertexContourRefPair(from, tail));
391 else if (newTail == m_ContourStarts.end() && newHead != m_ContourEnds.end())
393 // Found a single contour to which the new arc should be appended.
394 auto head = newHead->second;
395 itkAssertOrThrowMacro((head->back() == from), "Beginning doesn't match End");
397 // erase the old end of this contour
398 m_ContourEnds.erase(newHead);
399 // Now add the new start of this contour.
400 m_ContourEnds.insert(VertexContourRefPair(to, head));
404 template <class TInputImage>
405 void ContourExtractor2DImageFilter<TInputImage>::FillOutputs()
407 this->SetNumberOfIndexedOutputs(m_Contours.size());
409 for (auto it = m_Contours.begin(); it != m_Contours.end(); it++, i++)
411 OutputPathPointer output = this->GetOutput(i);
414 // Static cast is OK because we know PathSource will make its templated
416 output = static_cast<OutputPathType *>(this->MakeOutput(i).GetPointer());
417 this->SetNthOutput(i, output.GetPointer());
419 typename VertexListType::Pointer path = const_cast<VertexListType *>(output->GetVertexList());
421 path->reserve(it->size()); // use std::vector version of 'reserve()'
422 // instead of VectorContainer::Reserve() to work around
423 // the fact that the latter is essentially std::vector::resize(),
424 // which is not what we want.
426 // Now put all the points from the contour deque into the path and
427 // mark output as modified
429 typedef typename ContourType::const_iterator ConstIteratorType;
430 if (m_ReverseContourOrientation)
432 ConstIteratorType itC = (*it).end();
436 path->push_back(*itC);
437 } while (itC != (*it).begin());
441 ConstIteratorType itC = (*it).begin();
442 while (itC != (*it).end())
444 path->push_back(*itC);
452 template <class TInputImage>
453 void ContourExtractor2DImageFilter<TInputImage>::SetRequestedRegion(const InputRegionType region)
455 itkDebugMacro("setting RequestedRegion to " << region);
456 m_UseCustomRegion = true;
457 if (this->m_RequestedRegion != region)
459 this->m_RequestedRegion = region;
464 template <class TInputImage>
465 void ContourExtractor2DImageFilter<TInputImage>::ClearRequestedRegion()
467 itkDebugMacro("Clearing RequestedRegion.");
468 if (this->m_UseCustomRegion == true)
470 this->m_UseCustomRegion = false;
475 template <class TInputImage>
476 void ContourExtractor2DImageFilter<TInputImage>::GenerateInputRequestedRegion()
478 InputImageType *input = const_cast<InputImageType *>(this->GetInput());
482 if (m_UseCustomRegion)
484 InputRegionType requestedRegion = m_RequestedRegion;
485 if (requestedRegion.Crop(input->GetLargestPossibleRegion()))
487 input->SetRequestedRegion(requestedRegion);
492 // Couldn't crop the region (requested region is outside the largest
493 // possible region). Throw an exception.
495 // store what we tried to request (prior to trying to crop)
496 input->SetRequestedRegion(requestedRegion);
498 // build an exception
499 InvalidRequestedRegionError e(__FILE__, __LINE__);
500 e.SetLocation(ITK_LOCATION);
501 e.SetDescription("Requested region is outside the largest possible region.");
502 e.SetDataObject(input);
508 input->SetRequestedRegion(input->GetLargestPossibleRegion());
513 * Standard "PrintSelf" method
515 template <class TInputImage>
516 void ContourExtractor2DImageFilter<TInputImage>::PrintSelf(std::ostream &os, Indent indent) const
518 Superclass::PrintSelf(os, indent);
519 os << indent << "ReverseContourOrientation: " << m_ReverseContourOrientation << std::endl;
520 os << indent << "VertexConnectHighPixels: " << m_VertexConnectHighPixels << std::endl;
521 os << indent << "UseCustomRegion: " << m_UseCustomRegion << std::endl;
522 os << indent << "NumericTraits: " << m_UseCustomRegion << std::endl;
523 os << indent << "NumberOfContoursCreated: " << m_NumberOfContoursCreated << std::endl;
524 if (m_UseCustomRegion)
526 os << indent << "Custom region: " << m_RequestedRegion << std::endl;
529 typedef typename NumericTraits<InputRealType>::PrintType InputRealPrintType;
531 os << indent << "Contour value: " << static_cast<InputRealPrintType>(m_ContourValue) << std::endl;
534 } // end namespace itk