Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkToFProcessingCommon.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 ============================================================================*/
13 
14 namespace mitk
15 {
17  ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY)
18  {
19  ToFPoint3D cartesianCoordinates;
20 
21  // calculate image coordinates in pixel units;
22  // Note: pixel unit (pX) in x direction does normally not equal pixel unit (pY) in y direction.
23  // Therefore, a transformation in one of the pixel units is necessary
24  // Here, pX as image coordinate unit is chosen
25  // pY = (focalLengthX / focalLengthY) * pX
26  ToFScalarType imageX = i - principalPointX;
27  ToFScalarType imageY = j - principalPointY;
28  ToFScalarType imageY_in_pX = imageY * (focalLengthX / focalLengthY);
29 
30  //distance from pinhole to pixel (i,j) in pX units (pixel unit in x direction)
31  ToFScalarType d_in_pX = sqrt(imageX*imageX + imageY_in_pX*imageY_in_pX + focalLengthX*focalLengthX);
32 
33  cartesianCoordinates[0] = distance * imageX / d_in_pX; //Strahlensatz: x / imageX = distance / d
34  cartesianCoordinates[1] = distance * imageY_in_pX / d_in_pX; //Strahlensatz: y / imageY = distances / d
35  cartesianCoordinates[2] = distance * focalLengthX / d_in_pX; //Strahlensatz: z / f = distance / d.
36 
37  return cartesianCoordinates;
38  }
39 
40  //See also "Hacking the Kinect" - Jeff Kramer, Matt Parker, Daniel Herrera C., Nicolas Burrus, Florian Echtler, Chapter 7, Part 1 "Moving from Depth Map to Point Cloud.
42  ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY)
43  {
44  ToFPoint3D cartesianCoordinates;
45 
46  cartesianCoordinates[0] = distance * (i - principalPointX) / focalLengthX;
47  cartesianCoordinates[1] = distance * (j - principalPointY) / focalLengthY;
48  cartesianCoordinates[2] = distance;
49 
50  return cartesianCoordinates;
51  }
52 
53  ToFProcessingCommon::ToFPoint3D ToFProcessingCommon::CartesianToKinectIndexCoordinates(ToFScalarType cartesianPointX, ToFScalarType cartesianPointY, ToFScalarType cartesianPointZ, ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance)
54  {
55  ToFPoint3D indexCoordinatesAndDistanceValue;
56  indexCoordinatesAndDistanceValue[0] = ((cartesianPointX*focalLengthX)/cartesianPointZ) + principalPointX;
57  indexCoordinatesAndDistanceValue[1] = ((cartesianPointY*focalLengthY)/cartesianPointZ) + principalPointY;
58 
59  if (calculateDistance)
60  { //There is no computation for kinect. See KinectIndexToCartesianCoordinates.
61  indexCoordinatesAndDistanceValue[2] = cartesianPointZ;
62  }
63  else
64  {
65  indexCoordinatesAndDistanceValue[2] = 0.0;
66  }
67  return indexCoordinatesAndDistanceValue;
68  }
69 
71  ToFScalarType interPixelDistanceX, ToFScalarType interPixelDistanceY,
72  ToFScalarType principalPointX, ToFScalarType principalPointY)
73  {
74  ToFPoint3D cartesianCoordinates;
75 
76  // calculate image coordinates in mm;
77  ToFScalarType imageX = (( i - principalPointX ) * interPixelDistanceX);
78  ToFScalarType imageY = (( j - principalPointY ) * interPixelDistanceY);
79 
80  //distance from pinhole to pixel
81  ToFScalarType d = sqrt(imageX*imageX + imageY*imageY + focalLength*focalLength);
82 
83  cartesianCoordinates[0] = (distance)*imageX / d; //Strahlensatz: x / imageX = (distance) / d
84  cartesianCoordinates[1] = (distance)*imageY / d; //Strahlensatz: y / imageY = (distance) / d
85  cartesianCoordinates[2] = ((distance*focalLength) / d); //Strahlensatz: z / f = distance / d.
86 
87  return cartesianCoordinates;
88  }
89 
90 
91 
93  ToFScalarType focalLengthX, ToFScalarType focalLengthY,
94  ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance)
95  {
96  ToFPoint3D indexCoordinatesAndDistanceValue;
97 
98  ToFScalarType imageX = cartesianPointX*focalLengthX/cartesianPointZ; //Strahlensatz: cartesianPointX / imageX = cartesianPointZ / focalLengthX
99  ToFScalarType imageY = cartesianPointY*focalLengthY/cartesianPointZ; //Strahlensatz: cartesianPointY / imageY = cartesianPointZ / focalLengthY
100 
101  indexCoordinatesAndDistanceValue[0] = imageX + principalPointX;
102  indexCoordinatesAndDistanceValue[1] = imageY + principalPointY;
103 
104  // Note: pixel unit (pX) in x direction does normally not equal pixel unit (pY) in y direction.
105  // Therefore, a transformation in one of the pixel units is necessary (for calculation of the distance value only)
106  // Here, pX as image coordinate unit is chosen
107  // pY = (focalLengthX / focalLengthY) * pX
108  ToFScalarType imageY_in_pX = imageY * focalLengthX/focalLengthY;
109 
110  //distance from pinhole to pixel
111  ToFScalarType d_in_pX = sqrt(imageX*imageX + imageY_in_pX*imageY_in_pX + focalLengthX*focalLengthX);
112 
113  if (calculateDistance)
114  {
115  indexCoordinatesAndDistanceValue[2] = d_in_pX*(cartesianPointZ) / focalLengthX;
116  }
117  else
118  {
119  indexCoordinatesAndDistanceValue[2] = 0.0;
120  }
121  return indexCoordinatesAndDistanceValue;
122  }
123 
125  ToFScalarType focalLength, ToFScalarType interPixelDistanceX, ToFScalarType interPixelDistanceY,
126  ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance)
127  {
128  ToFPoint3D indexCoordinatesAndDistanceValue;
129 
130  ToFScalarType imageX = cartesianPointX*focalLength/cartesianPointZ;
131  ToFScalarType imageY = cartesianPointY*focalLength/cartesianPointZ;
132 
133  indexCoordinatesAndDistanceValue[0] = imageX/interPixelDistanceX + principalPointX;
134  indexCoordinatesAndDistanceValue[1] = imageY/interPixelDistanceY + principalPointY;
135 
136  ToFScalarType d = sqrt(imageX*imageX + imageY*imageY + focalLength*focalLength);
137 
138  if (calculateDistance)
139  {
140  indexCoordinatesAndDistanceValue[2] = d*(cartesianPointZ) / focalLength;
141  }
142  else
143  {
144  indexCoordinatesAndDistanceValue[2] = 0.0;
145  }
146  return indexCoordinatesAndDistanceValue;
147  }
148 
150  {
151  ToFPoint3D cartesianCoordinates;
152 
153  cartesianCoordinates[0] = distance * (continuousIndex[0] - principalPointX) / focalLengthX;
154  cartesianCoordinates[1] = distance * (continuousIndex[1] - principalPointY) / focalLengthY;
155  cartesianCoordinates[2] = distance;
156 
157  return cartesianCoordinates;
158  }
159 
160  ToFProcessingCommon::ToFScalarType ToFProcessingCommon::CalculateViewAngle( mitk::CameraIntrinsics::Pointer intrinsics, unsigned int dimX )
161  {
162  ToFScalarType viewAngle = 180*(atan2(intrinsics->GetPrincipalPointX(),intrinsics->GetFocalLengthX()) + atan2((dimX-intrinsics->GetPrincipalPointX()),intrinsics->GetFocalLengthX()))/vnl_math::pi;
163  return viewAngle;
164  }
165 }
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.
static ToFPoint3D CartesianToIndexCoordinatesWithInterpixdist(ToFScalarType cartesianPointX, ToFScalarType cartesianPointY, ToFScalarType cartesianPointZ, ToFScalarType focalLength, ToFScalarType interPixelDistanceX, ToFScalarType interPixelDistanceY, ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance=true)
Convert cartesian coordinates to index based distances.
static ToFProcessingCommon::ToFPoint3D ContinuousKinectIndexToCartesianCoordinates(mitk::Point2D continuousIndex, ToFScalarType distance, ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY)
ContinuousKinectIndexToCartesianCoordinates This method is escpially meant for reconstructing a Kinec...
DataCollection - Class to facilitate loading/accessing structured data.
static ToFScalarType CalculateViewAngle(mitk::CameraIntrinsics::Pointer intrinsics, unsigned int dimX)
Calculates the horizontal view angle of the camera with the given intrinsics.
static ToFPoint3D CartesianToIndexCoordinates(ToFScalarType cartesianPointX, ToFScalarType cartesianPointY, ToFScalarType cartesianPointZ, ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance=true)
Convert cartesian coordinates to index based distances.
static ToFProcessingCommon::ToFPoint3D KinectIndexToCartesianCoordinates(unsigned int i, unsigned int j, ToFScalarType distance, ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY)
KinectIndexToCartesianCoordinates Convert a pixel (i,j) with value d to a 3D world point...
itk::Point< ToFScalarType, 3 > ToFPoint3D
static ToFPoint3D CartesianToKinectIndexCoordinates(ToFScalarType cartesianPointX, ToFScalarType cartesianPointY, ToFScalarType cartesianPointZ, ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance=true)
CartesianCoordinatesToKinectIndexCoordinates Transform a 3D world point back to distance image pixel ...
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.