Medical Imaging Interaction Toolkit  2022.04.99-194dd5d8
Medical Imaging Interaction Toolkit
itkShortestPathImageFilter.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #ifndef __itkShortestPathImageFilter_h
13 #define __itkShortestPathImageFilter_h
14 
15 #include "itkImageToImageFilter.h"
17 #include "itkShortestPathNode.h"
18 #include <itkImageRegionIteratorWithIndex.h>
19 
20 #include <itkMacro.h>
21 
22 // ------- INFORMATION ----------
24 // void SetInput( ItkImage ) // Compulsory
25 // void SetStartIndex (const IndexType & StartIndex); // Compulsory
26 // void SetEndIndex(const IndexType & EndIndex); // Compulsory
27 // void SetFullNeighborsMode(bool) // Optional (default=false), if false N4, if true N26
28 // void SetActivateTimeOut(bool) // Optional (default=false), for debug issues: after 30s algorithms terminates. You can
29 // have a look at the VectorOrderImage to see how far it came
30 // void SetMakeOutputImage(bool) // Optional (default=true), Generate an outputimage of the path. You can also get the
31 // path directoy with GetVectorPath()
32 // void SetCalcAllDistances(bool) // Optional (default=false), Calculate Distances over the whole image. CAREFUL,
33 // algorithm time extends a lot. Necessary for GetDistanceImage
34 // void SetStoreVectorOrder(bool) // Optional (default=false), Stores in which order the pixels were checked. Necessary
35 // for GetVectorOrderImage
36 // void AddEndIndex(const IndexType & EndIndex) //Optional. By calling this function you can add several endpoints! The
37 // algorithm will look for several shortest Pathes. From Start to all Endpoints.
38 //
40 // std::vector< itk::Index<3> > GetVectorPath(); // returns the shortest path as vector
41 // std::vector< std::vector< itk::Index<3> > GetMultipleVectorPathe(); // returns a vector of shortest Pathes (which are
42 // vectors of points)
43 // GetDistanceImage // Returns the distance image
44 // GetVectorOrderIMage // Returns the Vector Order image
45 //
46 // EXAMPLE USE
47 // pleae see qmitkmitralvalvesegmentation4dtee bundle
48 
49 namespace itk
50 {
51  template <class TInputImageType, class TOutputImageType>
52  class ShortestPathImageFilter : public ImageToImageFilter<TInputImageType, TOutputImageType>
53  {
54  public:
55  // Standard Typedefs
57  typedef ImageToImageFilter<TInputImageType, TOutputImageType> Superclass;
60 
61  // Typdefs for metric
64 
65  // More typdefs for convenience
66  typedef TInputImageType InputImageType;
67  typedef typename TInputImageType::Pointer InputImagePointer;
68  typedef typename TInputImageType::PixelType InputImagePixelType;
69  typedef typename TInputImageType::SizeType InputImageSizeType;
70  typedef typename TInputImageType::IndexType IndexType;
71  typedef typename itk::ImageRegionIteratorWithIndex<InputImageType> InputImageIteratorType;
72 
73  typedef TOutputImageType OutputImageType;
74  typedef typename TOutputImageType::Pointer OutputImagePointer;
75  typedef typename TOutputImageType::PixelType OutputImagePixelType;
76  typedef typename TOutputImageType::IndexType OutputImageIndexType;
77  typedef ImageRegionIteratorWithIndex<OutputImageType> OutputImageIteratorType;
78  typedef itk::ShapedNeighborhoodIterator<TInputImageType> itkShapedNeighborhoodIteratorType;
79 
80  // New Macro for smartpointer instantiation
81  itkFactorylessNewMacro(Self);
82  itkCloneMacro(Self);
83 
84  // Run-time type information
85  itkTypeMacro(ShortestPathImageFilter, ImageToImageFilter);
86 
87  // Display
88  void PrintSelf(std::ostream &os, Indent indent) const override;
89 
90  // Compare function for A_STAR
92  {
94  };
95 
96  // \brief Set Starpoint for ShortestPath Calculation
97  void SetStartIndex(const IndexType &StartIndex);
98 
99  // \brief Adds Endpoint for multiple ShortestPath Calculation
100  void AddEndIndex(const IndexType &index);
101 
102  // \brief Set Endpoint for ShortestPath Calculation
103  void SetEndIndex(const IndexType &EndIndex);
104 
105  // \brief Set FullNeighborsMode. false = no diagonal neighbors, in 2D this means N4 Neigborhood. true = would be N8
106  // in 2D
107  itkSetMacro(FullNeighborsMode, bool);
108  itkGetMacro(FullNeighborsMode, bool);
109 
110  // \brief Set Graph_fullNeighbors. false = no diagonal neighbors, in 2D this means N4 Neigborhood. true = would be
111  // N8 in 2D
112  itkSetMacro(Graph_fullNeighbors, bool);
113 
114  // \brief (default=true), Produce output image, which shows the shortest path. But you can also get the shortest
115  // Path directly as vector with the function GetVectorPath
116  itkSetMacro(MakeOutputImage, bool);
117  itkGetMacro(MakeOutputImage, bool);
118 
119  // \brief (default=false), Store an Vector of Order, so you can call getVectorOrderImage after update
120  itkSetMacro(StoreVectorOrder, bool);
121  itkGetMacro(StoreVectorOrder, bool);
122 
123  // \brief (default=false), // Calculate all Distances to all pixels, so you can call getDistanceImage after update
124  // (warning algo will take a long time)
125  itkSetMacro(CalcAllDistances, bool);
126  itkGetMacro(CalcAllDistances, bool);
127 
128  // \brief (default=false), for debug issues: after 30s algorithms terminates. You can have a look at the
129  // VectorOrderImage to see how far it came
130  itkSetMacro(ActivateTimeOut, bool);
131  itkGetMacro(ActivateTimeOut, bool);
132 
133  // \brief returns shortest Path as vector
134  std::vector<IndexType> GetVectorPath();
135 
136  // \brief returns Multiple shortest Paths. You can call this function, when u performed a multiple shortest path
137  // search (one start, several ends)
138  std::vector<std::vector<IndexType>> GetMultipleVectorPaths();
139 
140  // \brief returns the vector order image. It shows in which order the pixels were checked. good for debugging. Be
141  // sure to have m_StoreVectorOrder=true
142  OutputImagePointer GetVectorOrderImage();
143 
144  // \brief returns the distance image. It shows the distances from the startpoint to all other pixels. Be sure to
145  // have m_CalcAllDistances=true
146  OutputImagePointer GetDistanceImage();
147 
148  // \brief Fill m_VectorPath
149  void MakeShortestPathVector();
150 
151  // \brief cleans up the filter
152  void CleanUp();
153 
154  itkSetObjectMacro(CostFunction,
155  CostFunctionType); // itkSetObjectMacro = set function that uses pointer as parameter
156  itkGetObjectMacro(CostFunction, CostFunctionType);
157 
158  void SetUseCostFunction(bool doUseCostFunction) { m_useCostFunction = doUseCostFunction; };
160 
161  protected:
162  std::vector<IndexType>
163  m_endPoints; // if you fill this vector, the algo will not rest until all endPoints have been reached
164  std::vector<IndexType> m_endPointsClosed;
165 
166  ShortestPathNode *m_Nodes; // main list that contains all nodes
172  std::vector<ShortestPathNode *> m_Graph_DiscoveredNodeList;
173  ShortestPathImageFilter(Self &); // intentionally not implemented
174  void operator=(const Self &); // intentionally not implemented
175  const static int BACKGROUND = 0;
176  const static int FOREGROUND = 255;
178 
180  bool m_StoreVectorOrder; // Store an Vector of Order, so you can call getVectorOrderImage after update
181  bool m_CalcAllDistances; // Calculate all Distances, so you can call getDistanceImage after update (warning algo
182  // will take a long time)
183 
185 
186  bool m_ActivateTimeOut; // if true, then i search max. 30 secs. then abort
187 
189 
190  CostFunctionTypePointer m_CostFunction;
192  std::vector<IndexType> m_VectorPath;
193  std::vector<std::vector<IndexType>> m_MultipleVectorPaths;
194 
195  std::vector<NodeNumType> m_VectorOrder;
196 
198 
199  ~ShortestPathImageFilter() override;
200 
201  // \brief Create all the outputs
202  void MakeOutputs();
203 
204  // \brief Generate Data
205  void GenerateData() override;
206 
207  // \brief gets the estimate costs from pixel a to target.
208  double getEstimatedCostsToTarget(const IndexType &a);
209 
210  typename InputImageType::Pointer m_magnitudeImage;
211 
212  // \brief Convert a indexnumber of a node in m_Nodes to image coordinates
213  typename TInputImageType::IndexType NodeToCoord(NodeNumType);
214 
215  // \brief Convert image coordinate to a indexnumber of a node in m_Nodes
216  unsigned int CoordToNode(IndexType);
217 
218  // \brief Returns the neighbors of a node
219  std::vector<ShortestPathNode *> GetNeighbors(NodeNumType nodeNum, bool FullNeighbors);
220 
221  // \brief Check if coords are in bounds of image
222  bool CoordIsInBounds(IndexType);
223 
224  // \brief Initializes the graph
225  void InitGraph();
226 
227  // \brief Start ShortestPathSearch
229  };
230 
231 } // end of namespace itk
232 
233 #include "itkShortestPathImageFilter.txx"
234 
235 #endif
std::vector< std::vector< IndexType > > GetMultipleVectorPaths()
bool CoordIsInBounds(IndexType)
std::vector< std::vector< IndexType > > m_MultipleVectorPaths
void SetEndIndex(const IndexType &EndIndex)
SmartPointer< const Self > ConstPointer
std::vector< IndexType > GetVectorPath()
TInputImageType::PixelType InputImagePixelType
void PrintSelf(std::ostream &os, Indent indent) const override
TInputImageType::Pointer InputImagePointer
unsigned int CoordToNode(IndexType)
itk::ShapedNeighborhoodIterator< TInputImageType > itkShapedNeighborhoodIteratorType
void SetStartIndex(const IndexType &StartIndex)
std::vector< ShortestPathNode * > GetNeighbors(NodeNumType nodeNum, bool FullNeighbors)
OutputImagePointer GetVectorOrderImage()
std::vector< NodeNumType > m_VectorOrder
void GenerateData() override
TInputImageType::SizeType InputImageSizeType
TInputImageType::IndexType IndexType
void AddEndIndex(const IndexType &index)
CostFunctionType::Pointer CostFunctionTypePointer
double getEstimatedCostsToTarget(const IndexType &a)
bool operator()(ShortestPathNode *a, ShortestPathNode *b)
TOutputImageType::PixelType OutputImagePixelType
ImageRegionIteratorWithIndex< OutputImageType > OutputImageIteratorType
std::vector< ShortestPathNode * > m_Graph_DiscoveredNodeList
TOutputImageType::IndexType OutputImageIndexType
std::vector< IndexType > m_endPointsClosed
ImageToImageFilter< TInputImageType, TOutputImageType > Superclass
TInputImageType::IndexType NodeToCoord(NodeNumType)
TOutputImageType::Pointer OutputImagePointer
void SetUseCostFunction(bool doUseCostFunction)
OutputImagePointer GetDistanceImage()
InputImageType::Pointer m_magnitudeImage
ShortestPathCostFunction< TInputImageType > CostFunctionType
void operator=(const Self &)
unsigned int NodeNumType
itk::ImageRegionIteratorWithIndex< InputImageType > InputImageIteratorType