Medical Imaging Interaction Toolkit  2016.11.0
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,
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 ===================================================================*/
17 
18 namespace mitk
19 {
21  ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY)
22  {
23  ToFPoint3D cartesianCoordinates;
24 
25  // calculate image coordinates in pixel units;
26  // Note: pixel unit (pX) in x direction does normally not equal pixel unit (pY) in y direction.
27  // Therefore, a transformation in one of the pixel units is necessary
28  // Here, pX as image coordinate unit is chosen
29  // pY = (focalLengthX / focalLengthY) * pX
30  ToFScalarType imageX = i - principalPointX;
31  ToFScalarType imageY = j - principalPointY;
32  ToFScalarType imageY_in_pX = imageY * (focalLengthX / focalLengthY);
33 
34  //distance from pinhole to pixel (i,j) in pX units (pixel unit in x direction)
35  ToFScalarType d_in_pX = sqrt(imageX*imageX + imageY_in_pX*imageY_in_pX + focalLengthX*focalLengthX);
36 
37  cartesianCoordinates[0] = distance * imageX / d_in_pX; //Strahlensatz: x / imageX = distance / d
38  cartesianCoordinates[1] = distance * imageY_in_pX / d_in_pX; //Strahlensatz: y / imageY = distances / d
39  cartesianCoordinates[2] = distance * focalLengthX / d_in_pX; //Strahlensatz: z / f = distance / d.
40 
41  return cartesianCoordinates;
42  }
43 
44  //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.
46  ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY)
47  {
48  ToFPoint3D cartesianCoordinates;
49 
50  cartesianCoordinates[0] = distance * (i - principalPointX) / focalLengthX;
51  cartesianCoordinates[1] = distance * (j - principalPointY) / focalLengthY;
52  cartesianCoordinates[2] = distance;
53 
54  return cartesianCoordinates;
55  }
56 
57  ToFProcessingCommon::ToFPoint3D ToFProcessingCommon::CartesianToKinectIndexCoordinates(ToFScalarType cartesianPointX, ToFScalarType cartesianPointY, ToFScalarType cartesianPointZ, ToFScalarType focalLengthX, ToFScalarType focalLengthY, ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance)
58  {
59  ToFPoint3D indexCoordinatesAndDistanceValue;
60  indexCoordinatesAndDistanceValue[0] = ((cartesianPointX*focalLengthX)/cartesianPointZ) + principalPointX;
61  indexCoordinatesAndDistanceValue[1] = ((cartesianPointY*focalLengthY)/cartesianPointZ) + principalPointY;
62 
63  if (calculateDistance)
64  { //There is no computation for kinect. See KinectIndexToCartesianCoordinates.
65  indexCoordinatesAndDistanceValue[2] = cartesianPointZ;
66  }
67  else
68  {
69  indexCoordinatesAndDistanceValue[2] = 0.0;
70  }
71  return indexCoordinatesAndDistanceValue;
72  }
73 
75  ToFScalarType interPixelDistanceX, ToFScalarType interPixelDistanceY,
76  ToFScalarType principalPointX, ToFScalarType principalPointY)
77  {
78  ToFPoint3D cartesianCoordinates;
79 
80  // calculate image coordinates in mm;
81  ToFScalarType imageX = (( i - principalPointX ) * interPixelDistanceX);
82  ToFScalarType imageY = (( j - principalPointY ) * interPixelDistanceY);
83 
84  //distance from pinhole to pixel
85  ToFScalarType d = sqrt(imageX*imageX + imageY*imageY + focalLength*focalLength);
86 
87  cartesianCoordinates[0] = (distance)*imageX / d; //Strahlensatz: x / imageX = (distance) / d
88  cartesianCoordinates[1] = (distance)*imageY / d; //Strahlensatz: y / imageY = (distance) / d
89  cartesianCoordinates[2] = ((distance*focalLength) / d); //Strahlensatz: z / f = distance / d.
90 
91  return cartesianCoordinates;
92  }
93 
94 
95 
97  ToFScalarType focalLengthX, ToFScalarType focalLengthY,
98  ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance)
99  {
100  ToFPoint3D indexCoordinatesAndDistanceValue;
101 
102  ToFScalarType imageX = cartesianPointX*focalLengthX/cartesianPointZ; //Strahlensatz: cartesianPointX / imageX = cartesianPointZ / focalLengthX
103  ToFScalarType imageY = cartesianPointY*focalLengthY/cartesianPointZ; //Strahlensatz: cartesianPointY / imageY = cartesianPointZ / focalLengthY
104 
105  indexCoordinatesAndDistanceValue[0] = imageX + principalPointX;
106  indexCoordinatesAndDistanceValue[1] = imageY + principalPointY;
107 
108  // Note: pixel unit (pX) in x direction does normally not equal pixel unit (pY) in y direction.
109  // Therefore, a transformation in one of the pixel units is necessary (for calculation of the distance value only)
110  // Here, pX as image coordinate unit is chosen
111  // pY = (focalLengthX / focalLengthY) * pX
112  ToFScalarType imageY_in_pX = imageY * focalLengthX/focalLengthY;
113 
114  //distance from pinhole to pixel
115  ToFScalarType d_in_pX = sqrt(imageX*imageX + imageY_in_pX*imageY_in_pX + focalLengthX*focalLengthX);
116 
117  if (calculateDistance)
118  {
119  indexCoordinatesAndDistanceValue[2] = d_in_pX*(cartesianPointZ) / focalLengthX;
120  }
121  else
122  {
123  indexCoordinatesAndDistanceValue[2] = 0.0;
124  }
125  return indexCoordinatesAndDistanceValue;
126  }
127 
129  ToFScalarType focalLength, ToFScalarType interPixelDistanceX, ToFScalarType interPixelDistanceY,
130  ToFScalarType principalPointX, ToFScalarType principalPointY, bool calculateDistance)
131  {
132  ToFPoint3D indexCoordinatesAndDistanceValue;
133 
134  ToFScalarType imageX = cartesianPointX*focalLength/cartesianPointZ;
135  ToFScalarType imageY = cartesianPointY*focalLength/cartesianPointZ;
136 
137  indexCoordinatesAndDistanceValue[0] = imageX/interPixelDistanceX + principalPointX;
138  indexCoordinatesAndDistanceValue[1] = imageY/interPixelDistanceY + principalPointY;
139 
140  ToFScalarType d = sqrt(imageX*imageX + imageY*imageY + focalLength*focalLength);
141 
142  if (calculateDistance)
143  {
144  indexCoordinatesAndDistanceValue[2] = d*(cartesianPointZ) / focalLength;
145  }
146  else
147  {
148  indexCoordinatesAndDistanceValue[2] = 0.0;
149  }
150  return indexCoordinatesAndDistanceValue;
151  }
152 
154  {
155  ToFPoint3D cartesianCoordinates;
156 
157  cartesianCoordinates[0] = distance * (continuousIndex[0] - principalPointX) / focalLengthX;
158  cartesianCoordinates[1] = distance * (continuousIndex[1] - principalPointY) / focalLengthY;
159  cartesianCoordinates[2] = distance;
160 
161  return cartesianCoordinates;
162  }
163 
165  {
166  ToFScalarType viewAngle = 180*(atan2(intrinsics->GetPrincipalPointX(),intrinsics->GetFocalLengthX()) + atan2((dimX-intrinsics->GetPrincipalPointX()),intrinsics->GetFocalLengthX()))/vnl_math::pi;
167  return viewAngle;
168  }
169 }
itk::SmartPointer< Self > Pointer
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.