Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkToFDistanceImageToPointSetFilter.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 
18 
19 #include "mitkImageDataItem.h"
20 #include "mitkPointSet.h"
23 
25 : m_Subset(NULL), m_CameraIntrinsics(), m_InterPixelDistance()
26 {
27  m_InterPixelDistance.Fill(0.045);
29  m_CameraIntrinsics->SetFocalLength(5.9421434211923247e+02,5.9104053696870778e+02);
30  m_CameraIntrinsics->SetPrincipalPoint(3.3930780975300314e+02,2.4273913761751615e+02);
31  m_CameraIntrinsics->SetDistorsionCoeffs(-0.36874385358645773f,-0.14339503290129013,0.0033210108720361795,-0.004277703352074105);
32  m_ReconstructionMode = true;
33 }
34 
36 {
37 }
38 
40 {
41  this->SetInput(0,distanceImage);
42 }
43 
44 void mitk::ToFDistanceImageToPointSetFilter::SetInput( unsigned int idx,const mitk::Image* distanceImage )
45 {
46  if ((distanceImage == nullptr) && (idx == this->GetNumberOfInputs() - 1)) // if the last input is set to NULL, reduce the number of inputs by one
47  {
48  this->SetNumberOfInputs(this->GetNumberOfInputs() - 1);
49  }
50  else
51  {
52  this->ProcessObject::SetNthInput(idx, const_cast<mitk::Image*>(distanceImage)); // Process object is not const-correct so the const_cast is required here
53  this->CreateOutputsForAllInputs();
54  }
55 }
56 
58 {
59  return this->GetInput(0);
60 }
61 
63 {
64  if (this->GetNumberOfInputs() < 1)
65  return nullptr;
66 
67  return static_cast< mitk::Image*>(this->ProcessObject::GetInput(idx));
68 }
69 
71 {
72  // check if points of PointSet are inside the input image
73  mitk::Image::Pointer input = this->GetInput();
74 
75  unsigned int xDim = UINT_MAX;
76  unsigned int yDim = UINT_MAX;
77  if(input.IsNotNull() && input->IsInitialized())
78  {
79  unsigned int xDim = input->GetDimension(0);
80  unsigned int yDim = input->GetDimension(1);
81  }
82 
83  bool pointSetValid = true;
84  for (unsigned int i=0; i<subset.size(); i++)
85  {
86  itk::Index<3> currentIndex = subset.at(i);
87  if (currentIndex[0]<0||currentIndex[0]>xDim||currentIndex[1]<0||currentIndex[1]>yDim)
88  {
89  pointSetValid = false;
90  }
91  }
92  if (pointSetValid)
93  {
94  m_Subset = subset;
95  }
96  else
97  {
98  MITK_ERROR<<"One or more indizes are located outside the image domain";
99  }
100 }
101 
103 {
104  std::vector<itk::Index<3> > subset;
105  for (int i=0; i<pointSet->GetSize(); i++)
106  {
107  mitk::Point3D currentPoint = pointSet->GetPoint(i);
108  itk::Index<3> currentIndex;
109  currentIndex[0] = currentPoint[0];
110  currentIndex[1] = currentPoint[1];
111  currentIndex[2] = currentPoint[2];
112  subset.push_back(currentIndex);
113  }
114  this->SetSubset(subset);
115 }
116 
118 {
119  //calculate world coordinates
120  mitk::ToFProcessingCommon::ToFPoint2D focalLengthInPixelUnits;
122  if (m_ReconstructionMode)
123  {
124  focalLengthInPixelUnits[0] = m_CameraIntrinsics->GetFocalLengthX();
125  focalLengthInPixelUnits[1] = m_CameraIntrinsics->GetFocalLengthY();
126  }
127  else
128  focalLengthInMm = (m_CameraIntrinsics->GetFocalLengthX()*m_InterPixelDistance[0]+m_CameraIntrinsics->GetFocalLengthY()*m_InterPixelDistance[1])/2.0;
129 
131  principalPoint[0] = m_CameraIntrinsics->GetPrincipalPointX();
132  principalPoint[1] = m_CameraIntrinsics->GetPrincipalPointY();
133 
134  mitk::PointSet::Pointer output = this->GetOutput();
135  assert(output);
136  mitk::Image::Pointer input = this->GetInput();
137  assert(input);
138 
139  //compute subset of points if input PointSet is defined
140  if (m_Subset.size()!=0)
141  {
142  mitk::ImagePixelReadAccessor<float,2> imageAcces(input, input->GetSliceData(0));
143  for (unsigned int i=0; i<m_Subset.size(); i++)
144  {
145  itk::Index<3> currentIndex = m_Subset.at(i);
146  itk::Index<2> index2D;
147  index2D[0] = currentIndex[0];
148  index2D[1] = currentIndex[1];
149  mitk::ToFProcessingCommon::ToFScalarType distance = (double)imageAcces.GetPixelByIndex(index2D);
150 
151  mitk::Point3D currentPoint;
152  if (m_ReconstructionMode)
153  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinates(currentIndex,distance,focalLengthInPixelUnits,principalPoint);
154  else
155  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinatesWithInterpixdist(currentIndex,distance,focalLengthInMm,m_InterPixelDistance,principalPoint);
156 
157  output->InsertPoint(i,currentPoint);
158  }
159  }
160  else //compute PointSet holding cartesian coordinates for every image point
161  {
162  int xDimension = (int)input->GetDimension(0);
163  int yDimension = (int)input->GetDimension(1);
164  int pointCount = 0;
165  mitk::ImagePixelReadAccessor<float,2> imageAcces(input, input->GetSliceData(0));
166  for (int j=0; j<yDimension; j++)
167  {
168  for (int i=0; i<xDimension; i++)
169  {
170  itk::Index<2> pixel;
171  pixel[0] = i;
172  pixel[1] = j;
173 
174  mitk::ToFProcessingCommon::ToFScalarType distance = (double)imageAcces.GetPixelByIndex(pixel);
175 
176  mitk::Point3D currentPoint;
177  if (m_ReconstructionMode)
178  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinates(i,j,distance,focalLengthInPixelUnits,principalPoint);
179  else
180  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinatesWithInterpixdist(i,j,distance,focalLengthInMm,m_InterPixelDistance,principalPoint);
181 
182  if (distance>mitk::eps)
183  {
184  output->InsertPoint( pointCount, currentPoint );
185  pointCount++;
186  }
187  }
188  }
189  }
190 }
191 
193 {
194  this->SetNumberOfOutputs(this->GetNumberOfInputs()); // create outputs for all inputs
195  for (unsigned int idx = 0; idx < this->GetNumberOfIndexedOutputs(); ++idx)
196  if (this->GetOutput(idx) == nullptr)
197  {
198  DataObjectPointer newOutput = this->MakeOutput(idx);
199  this->SetNthOutput(idx, newOutput);
200  }
201  this->Modified();
202 }
203 
205 {
206  this->GetOutput();
207  itkDebugMacro(<<"GenerateOutputInformation()");
208 }
209 
211 {
212  this->m_ReconstructionMode = withoutInterpixdist;
213 }
214 
216 {
217  return (this->m_ReconstructionMode);
218 }
Gives locked and index-based read access for a particular image part. The class provides several set-...
static ToFPoint3D IndexToCartesianCoordinatesWithInterpixdist(unsigned int i, unsigned int j, ToFScalarType distance, ToFScalarType focalLength, ToFScalarType interPixelDistanceX, ToFScalarType interPixelDistanceY, ToFScalarType principalPointX, ToFScalarType principalPointY)
Convert index based distances to cartesian coordinates.
bool m_ReconstructionMode
true = Reconstruction without interpixeldistance and with focal lengths in pixel units. false = Reconstruction with interpixeldistance and with focal length in mm.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
Image * GetInput()
Returns the input of this filter.
virtual void SetInput(const Image *distanceImage)
Sets the input of this filter.
bool GetReconstructionMode()
Returns the reconstruction mode.
void CreateOutputsForAllInputs()
Create an output for each input.
void SetReconstructionMode(bool withoutInterpixdist=true)
Sets the reconstruction mode, if using no interpixeldistances and focal lenghts in pixel units (=true...
itk::Point< ToFScalarType, 2 > ToFPoint2D
Image class for storing images.
Definition: mitkImage.h:76
ToFProcessingCommon::ToFPoint2D m_InterPixelDistance
distance in mm between two adjacent pixels on the ToF camera chip
static Pointer New()
virtual void GenerateData() override
Method generating the output of this filter. Called in the updated process of the pipeline...
MITKCORE_EXPORT const ScalarType eps
mitk::CameraIntrinsics::Pointer m_CameraIntrinsics
Member holding the intrinsic parameters needed for PointSet calculation.
void SetSubset(std::vector< itk::Index< 3 > > subset)
If this subset is defined, the cartesian coordinates are only computed for the contained indizes...
static ToFPoint3D IndexToCartesianCoordinates(unsigned int i, unsigned int j, ToFScalarType distance, ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY)
Convert index based distances to cartesian coordinates.