Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTractAnalyzer.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef __mitkTractAnalyzer_cpp
18 #define __mitkTractAnalyzer_cpp
19 
20 
21 #include <mitkProgressBar.h>
22 #include <mitkLogMacros.h>
23 #include <mitkTractAnalyzer.h>
24 
25 #include <itkImageRegionIteratorWithIndex.h>
26 
27 
30 
31 #include <iostream>
32 #include <fstream>
33 
34 using namespace std;
35 
36 namespace mitk {
37 
38  TractAnalyzer::TractAnalyzer() { }
39 
40 
41  void TractAnalyzer::MakeRoi()
42  {
43 
44  m_CostSum = 0.0;
45 
46  int n = 0;
47  if(m_PointSetNode.IsNotNull())
48  {
49  n = m_PointSetNode->GetSize();
50  if(n==0)
51  {
52  mitkThrow() << "No points have been set yet.";
53  }
54  }
55  else{
56  mitkThrow() << "No points have been set yet.";
57  }
58 
59  std::string pathDescription = "";
60  std::vector< itk::Index<3> > totalPath;
61 
62  if(n>0)
63  {
64  for(int i=0; i<n-1; ++i)
65  {
66 
68 
69  mitk::Point3D p = m_PointSetNode->GetPoint(i);
70  mitk::Point3D p2 = m_PointSetNode->GetPoint(i+1);
71 
72 
73  itk::Index<3> startPoint;
74  itk::Index<3> endPoint;
75 
76  m_InputImage->GetGeometry()->WorldToIndex(p,startPoint);
77  m_InputImage->GetGeometry()->WorldToIndex(p2,endPoint);
78 
79  MITK_INFO << "create roi";
80 
81  std::vector< itk::Index<3> > path = CreateSegment(startPoint, endPoint);
82 
83  for(auto it = path.begin();
84  it != path.end(); it++)
85  {
86  itk::Index<3> ix = *it;
87 
88  if (!(ix==endPoint))
89  {
91 
92  totalPath.push_back(ix);
93  std::stringstream ss;
94  ss << ix[0] << " " << ix[1] << " " << ix[2] << "\n";
95  pathDescription += ss.str();
96  }
97  else
98  {
99  // Only when dealing with the last segment the last point should be added. This one will not occur
100  // as the first point of the next roi segment.
101  if(i == (n-2))
102  {
103  totalPath.push_back(endPoint);
104  std::stringstream ss;
105  ss << endPoint[0] << " " << endPoint[1] << " " << endPoint[2] << "\n";
106  pathDescription += ss.str();
107  }
108 
109  }
110 
111  }
112 
113  }
114 
115 
116  // save pathDescription to m_PathDescription
117  m_PathDescription = pathDescription;
118 
120  mitk::CastToItkImage(m_InputImage, itkImg);
121 
123  roiImg->SetRegions(itkImg->GetLargestPossibleRegion().GetSize());
124  roiImg->SetOrigin(itkImg->GetOrigin());
125  roiImg->SetSpacing(itkImg->GetSpacing());
126  roiImg->SetDirection(itkImg->GetDirection());
127  roiImg->Allocate();
128  roiImg->FillBuffer(0);
129 
130 
131  std::vector< itk::Index<3> > roi;
132 
133  std::vector< itk::Index<3> >::iterator it;
134  for(it = totalPath.begin();
135  it != totalPath.end();
136  it++)
137  {
138  itk::Index<3> ix = *it;
139  roiImg->SetPixel(ix, 1);
140  roi.push_back(ix);
141  }
142 
143 
144  m_TbssRoi = mitk::TbssRoiImage::New();
145 
146  m_TbssRoi->SetRoi(roi);
147 
148  m_TbssRoi->SetImage(roiImg);
149 
150  m_TbssRoi->InitializeFromImage();
151 
152 
153 
154  }
155 
156 
157 
158  }
159 
160 
161 
162  std::vector< itk::Index<3> > TractAnalyzer::CreateSegment(itk::Index<3> startPoint, itk::Index<3> endPoint)
163  {
164 
166  typedef itk::ShortestPathCostFunctionTbss<FloatImageType> CostFunctionType;
167 
168 
169  FloatImageType::Pointer meanSkeleton;
170 
171  mitk::CastToItkImage(m_InputImage, meanSkeleton);
172 
173  // Only use the mitk image
174 
175 
176 
177  if(meanSkeleton)
178  {
180  costFunction->SetImage(meanSkeleton);
181  costFunction->SetStartIndex(startPoint);
182  costFunction->SetEndIndex(endPoint);
183  costFunction->SetThreshold(m_Threshold);
184 
186  pathFinder->SetCostFunction(costFunction);
187  pathFinder->SetFullNeighborsMode(true);
188  pathFinder->SetGraph_fullNeighbors(true);
189  //pathFinder->SetCalcMode(ShortestPathFilterType::A_STAR);
190  pathFinder->SetInput(meanSkeleton);
191  pathFinder->SetStartIndex(startPoint);
192  pathFinder->SetEndIndex(endPoint);
193  pathFinder->Update();
194 
195  double segmentCost = 0.0;
196  std::vector< itk::Index<3> > path = pathFinder->GetVectorPath();
197 
198  for(unsigned int i=0; i<path.size()-1; i++)
199  {
200  itk::Index<3> ix1 = path[i];
201  itk::Index<3> ix2 = path[i+1];
202 
203  segmentCost += costFunction->GetCost(ix1, ix2);
204  }
205 
206  m_CostSum += segmentCost;
207 
208 
209  return pathFinder->GetVectorPath();
210 
211 
212  }
213  return std::vector< itk::Index<3> >();
214  }
215 
216 
217 }
218 #endif
void Progress(unsigned int steps=1)
Sets the current amount of progress to current progress + steps.
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
static ProgressBar * GetInstance()
static method to get the GUI dependent ProgressBar-instance so the methods for steps to do and progre...
#define mitkThrow()
static Pointer New()
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.