Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (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 
14 
15 #include "mitkImageDataItem.h"
16 #include "mitkPointSet.h"
19 
21 : m_Subset(0), m_CameraIntrinsics(), m_InterPixelDistance()
22 {
23  m_InterPixelDistance.Fill(0.045);
25  m_CameraIntrinsics->SetFocalLength(5.9421434211923247e+02,5.9104053696870778e+02);
26  m_CameraIntrinsics->SetPrincipalPoint(3.3930780975300314e+02,2.4273913761751615e+02);
27  m_CameraIntrinsics->SetDistorsionCoeffs(-0.36874385358645773f,-0.14339503290129013,0.0033210108720361795,-0.004277703352074105);
28  m_ReconstructionMode = true;
29 }
30 
32 {
33 }
34 
36 {
37  this->SetInput(0,distanceImage);
38 }
39 
40 void mitk::ToFDistanceImageToPointSetFilter::SetInput( unsigned int idx,const Image* distanceImage )
41 {
42  if ((distanceImage == nullptr) && (idx == this->GetNumberOfInputs() - 1)) // if the last input is set to nullptr, reduce the number of inputs by one
43  {
44  this->SetNumberOfIndexedInputs(this->GetNumberOfInputs() - 1);
45  }
46  else
47  {
48  this->ProcessObject::SetNthInput(idx, const_cast<mitk::Image*>(distanceImage)); // Process object is not const-correct so the const_cast is required here
50  }
51 }
52 
54 {
55  return this->GetInput(0);
56 }
57 
59 {
60  if (this->GetNumberOfInputs() < 1)
61  return nullptr;
62 
63  return static_cast< mitk::Image*>(this->ProcessObject::GetInput(idx));
64 }
65 
67 {
68  // check if points of PointSet are inside the input image
69  mitk::Image::Pointer input = this->GetInput();
70 
71  unsigned int xDim = UINT_MAX;
72  unsigned int yDim = UINT_MAX;
73 
74  if(input.IsNotNull() && input->IsInitialized())
75  {
76  xDim = input->GetDimension(0);
77  yDim = input->GetDimension(1);
78  }
79 
80  bool pointSetValid = true;
81  for (unsigned int i=0; i<subset.size(); i++)
82  {
83  itk::Index<3> currentIndex = subset.at(i);
84  if (currentIndex[0] < 0 || currentIndex[0] > static_cast<itk::IndexValueType>(xDim) ||
85  currentIndex[1] < 0 || currentIndex[1] > static_cast<itk::IndexValueType>(yDim))
86  {
87  pointSetValid = false;
88  }
89  }
90  if (pointSetValid)
91  {
92  m_Subset = subset;
93  }
94  else
95  {
96  MITK_ERROR<<"One or more indizes are located outside the image domain";
97  }
98 }
99 
101 {
102  std::vector<itk::Index<3> > subset;
103  for (int i=0; i<pointSet->GetSize(); i++)
104  {
105  mitk::Point3D currentPoint = pointSet->GetPoint(i);
106  itk::Index<3> currentIndex;
107  currentIndex[0] = currentPoint[0];
108  currentIndex[1] = currentPoint[1];
109  currentIndex[2] = currentPoint[2];
110  subset.push_back(currentIndex);
111  }
112  this->SetSubset(subset);
113 }
114 
116 {
117  //calculate world coordinates
118  mitk::ToFProcessingCommon::ToFPoint2D focalLengthInPixelUnits;
121  {
122  focalLengthInPixelUnits[0] = m_CameraIntrinsics->GetFocalLengthX();
123  focalLengthInPixelUnits[1] = m_CameraIntrinsics->GetFocalLengthY();
124  focalLengthInMm = 0.0;
125  }
126  else
127  {
128  focalLengthInPixelUnits[0] = 0.0;
129  focalLengthInPixelUnits[1] = 0.0;
130  focalLengthInMm = (m_CameraIntrinsics->GetFocalLengthX()*m_InterPixelDistance[0]+m_CameraIntrinsics->GetFocalLengthY()*m_InterPixelDistance[1])/2.0;
131  }
132 
134  principalPoint[0] = m_CameraIntrinsics->GetPrincipalPointX();
135  principalPoint[1] = m_CameraIntrinsics->GetPrincipalPointY();
136 
137  mitk::PointSet::Pointer output = this->GetOutput();
138  assert(output);
139  mitk::Image::Pointer input = this->GetInput();
140  assert(input);
141 
142  //compute subset of points if input PointSet is defined
143  if (m_Subset.size()!=0)
144  {
145  mitk::ImagePixelReadAccessor<float,2> imageAcces(input, input->GetSliceData(0));
146  for (unsigned int i=0; i<m_Subset.size(); i++)
147  {
148  itk::Index<3> currentIndex = m_Subset.at(i);
149  itk::Index<2> index2D;
150  index2D[0] = currentIndex[0];
151  index2D[1] = currentIndex[1];
152  mitk::ToFProcessingCommon::ToFScalarType distance = (double)imageAcces.GetPixelByIndex(index2D);
153 
154  mitk::Point3D currentPoint;
156  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinates(currentIndex,distance,focalLengthInPixelUnits,principalPoint);
157  else
158  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinatesWithInterpixdist(currentIndex,distance,focalLengthInMm,m_InterPixelDistance,principalPoint);
159 
160  output->InsertPoint(i,currentPoint);
161  }
162  }
163  else //compute PointSet holding cartesian coordinates for every image point
164  {
165  int xDimension = (int)input->GetDimension(0);
166  int yDimension = (int)input->GetDimension(1);
167  int pointCount = 0;
168  mitk::ImagePixelReadAccessor<float,2> imageAcces(input, input->GetSliceData(0));
169  for (int j=0; j<yDimension; j++)
170  {
171  for (int i=0; i<xDimension; i++)
172  {
173  itk::Index<2> pixel;
174  pixel[0] = i;
175  pixel[1] = j;
176 
177  mitk::ToFProcessingCommon::ToFScalarType distance = (double)imageAcces.GetPixelByIndex(pixel);
178 
179  mitk::Point3D currentPoint;
181  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinates(i,j,distance,focalLengthInPixelUnits,principalPoint);
182  else
183  currentPoint = mitk::ToFProcessingCommon::IndexToCartesianCoordinatesWithInterpixdist(i,j,distance,focalLengthInMm,m_InterPixelDistance,principalPoint);
184 
185  if (distance>mitk::eps)
186  {
187  output->InsertPoint( pointCount, currentPoint );
188  pointCount++;
189  }
190  }
191  }
192  }
193 }
194 
196 {
197  this->SetNumberOfIndexedOutputs(this->GetNumberOfInputs()); // create outputs for all inputs
198  for (unsigned int idx = 0; idx < this->GetNumberOfIndexedOutputs(); ++idx)
199  if (this->GetOutput(idx) == nullptr)
200  {
201  DataObjectPointer newOutput = this->MakeOutput(idx);
202  this->SetNthOutput(idx, newOutput);
203  }
204  this->Modified();
205 }
206 
208 {
209  this->GetOutput();
210  itkDebugMacro(<<"GenerateOutputInformation()");
211 }
212 
214 {
215  this->m_ReconstructionMode = withoutInterpixdist;
216 }
217 
219 {
220  return (this->m_ReconstructionMode);
221 }
Gives locked and index-based read access for a particular image part. The class provides several set-...
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
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:20
std::vector< itk::Index< 3 > > m_Subset
If this subset is specified only the contained indizes are converted to cartesian coordinates...
OutputType * GetOutput()
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:72
ToFProcessingCommon::ToFPoint2D m_InterPixelDistance
distance in mm between two adjacent pixels on the ToF camera chip
static Pointer New()
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.