IVT
ExtrinsicParameterCalculatorCV.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: ExtrinsicParameterCalculatorCV.cpp
37 // Author: Pedram Azad
38 // Date: 27.03.2007
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
50 #include "Image/PrimitivesDrawer.h"
51 #include "Image/ImageProcessor.h"
52 #include "Image/IplImageAdaptor.h"
53 #include "Image/ByteImage.h"
55 #include "Math/Math3d.h"
56 #include "Math/Math2d.h"
57 #include "Helpers/helpers.h"
58 
59 #include <stdio.h>
60 #include <cv.h>
61 
62 
63 
64 // ****************************************************************************
65 // Functions
66 // ****************************************************************************
67 
69  const CByteImage *pImage, int nColumns, int nRows, float fSquareSize, // input
70  Vec2d *pPoints, Mat3d &rotation, Vec3d &translation) // output
71 {
72  CByteImage gray_image(pImage->width, pImage->height, CByteImage::eGrayScale);
73  CByteImage temp_image(&gray_image);
74 
75  ImageProcessor::ConvertImage(pImage, &gray_image);
76 
77  IplImage *pIplImage = IplImageAdaptor::Adapt(&gray_image);
78  IplImage *pIplTempImage = IplImageAdaptor::Adapt(&temp_image);
79  CvMemStorage *pStorage = cvCreateMemStorage();
80 
81  CvPoint2D32f *pIplPoints = new CvPoint2D32f[nColumns * nRows];
82 
83  int nPoints = 0;
84 
85  if (cvFindChessBoardCornerGuesses(pIplImage, pIplTempImage, pStorage,
86  cvSize(nColumns - 1, nRows - 1), pIplPoints, &nPoints) == 0 ||
87  nPoints != (nRows - 1) * (nColumns - 1))
88  {
89  // free memory
90  cvReleaseImageHeader(&pIplImage);
91  cvReleaseImageHeader(&pIplTempImage);
92  cvReleaseMemStorage(&pStorage);
93  delete [] pIplPoints;
94  return false;
95  }
96 
97  cvFindCornerSubPix(pIplImage, pIplPoints, nPoints,
98  cvSize(5, 5), cvSize(-1, -1),
99  cvTermCriteria(CV_TERMCRIT_ITER |CV_TERMCRIT_EPS, 10, 0.1));
100 
101  const CCalibration::CCameraParameters &cameraParameters = pCalibration->GetCameraParameters();
102 
103  float distortion[] = { (float) cameraParameters.distortion[0], (float) cameraParameters.distortion[1], (float) cameraParameters.distortion[2], (float) cameraParameters.distortion[3] };
104  float focalLength[] = { (float) cameraParameters.focalLength.x, (float) cameraParameters.focalLength.y };
105  CvPoint2D32f principalPoint;
106  float rotationVector[3];
107  float translationVector[3];
108 
109  principalPoint.x = (float) cameraParameters.principalPoint.x;
110  principalPoint.y = (float) cameraParameters.principalPoint.y;
111 
112  CvPoint3D32f *pIplObjectPoints = (CvPoint3D32f *) malloc(nPoints * sizeof(CvPoint3D32f));
113 
114  int i = 0;
115  for (int y = 0; y < nRows - 1; y++)
116  for (int x = 0; x < nColumns - 1; x++, i++)
117  {
118  pIplObjectPoints[i].x = fSquareSize * x;
119  pIplObjectPoints[i].y = fSquareSize * y;
120  pIplObjectPoints[i].z = 0;
121  }
122 
123  cvFindExtrinsicCameraParams(nPoints,
124  cvSize(pImage->width, pImage->height),
125  pIplPoints,
126  pIplObjectPoints,
127  focalLength,
128  principalPoint,
129  distortion,
130  rotationVector,
131  translationVector);
132 
133  float rotationMatrix[9];
134 
135  CvMat rmat = cvMat(3, 3, CV_32FC1, rotationMatrix);
136  CvMat rvec = cvMat(3, 1, CV_32FC1, rotationVector);
137  cvRodrigues(&rmat, &rvec, 0, CV_RODRIGUES_V2M);
138 
139  // result:
140 
141  // translation
142  Math3d::SetVec(translation, translationVector[0], translationVector[1], translationVector[2]);
143 
144  // rotation
145  rotation.r1 = rotationMatrix[0];
146  rotation.r2 = rotationMatrix[1];
147  rotation.r3 = rotationMatrix[2];
148  rotation.r4 = rotationMatrix[3];
149  rotation.r5 = rotationMatrix[4];
150  rotation.r6 = rotationMatrix[5];
151  rotation.r7 = rotationMatrix[6];
152  rotation.r8 = rotationMatrix[7];
153  rotation.r9 = rotationMatrix[8];
154 
155  // corner points
156  for (i = 0; i < nPoints; i++)
157  Math2d::SetVec(pPoints[i], pIplPoints[i].x, pIplPoints[i].y);
158 
159  // free memory
160  cvReleaseImageHeader(&pIplImage);
161  cvReleaseImageHeader(&pIplTempImage);
162  cvReleaseMemStorage(&pStorage);
163  free(pIplObjectPoints);
164  delete [] pIplPoints;
165 
166  return true;
167 }
168 
169 
170 void ExtrinsicParameterCalculatorCV::DrawExtrinsic(CByteImage *pResultImage, const CCalibration *pCalibration, const Vec2d *pPoints, int nPoints, float fSquareSize)
171 {
172  Vec3d worldPoint0 = { 0, 0, 0 };
173  Vec3d worldPointX = { 2 * fSquareSize, 0, 0 };
174  Vec3d worldPointY = { 0, 2 * fSquareSize, 0 };
175  Vec2d imagePoint0, imagePointX, imagePointY;
176  pCalibration->WorldToImageCoordinates(worldPoint0, imagePoint0);
177  pCalibration->WorldToImageCoordinates(worldPointX, imagePointX);
178  pCalibration->WorldToImageCoordinates(worldPointY, imagePointY);
179 
180  PrimitivesDrawer::DrawLine(pResultImage, imagePoint0, imagePointX, 0, 0, 0, 2);
181  PrimitivesDrawer::DrawLine(pResultImage, imagePoint0, imagePointY, 0, 0, 0, 2);
182  PrimitivesDrawer::DrawCircle(pResultImage, imagePoint0, 5, 0, 0, 0, -1);
183  PrimitivesDrawerCV::PutText(pResultImage, "x", imagePointX.x, imagePointX.y, 0.75, 0.75, 0, 0, 0, 2);
184  PrimitivesDrawerCV::PutText(pResultImage, "y", imagePointY.x, imagePointY.y, 0.75, 0.75, 0, 0, 0, 2);
185 
186  if (pPoints && nPoints > 0)
187  {
188  Vec2d last_point;
189  Math2d::SetVec(last_point, pPoints[0]);
190 
191  for (int i = 0; i < nPoints; i++)
192  {
193  int r, g, b;
194  hsv2rgb(int(float(i) / (nPoints - 1) * 240), 255, 255, r, g, b);
195  PrimitivesDrawer::DrawCircle(pResultImage, pPoints[i], 3, r, g, b, -1);
196  PrimitivesDrawer::DrawLine(pResultImage, last_point, pPoints[i], r, g, b, 1);
197  Math2d::SetVec(last_point, pPoints[i]);
198  }
199  }
200 }
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
float r4
Definition: Math3d.h:95
float r3
Definition: Math3d.h:95
void hsv2rgb(int h, int s, int v, int &r, int &g, int &b)
Definition: helpers.cpp:348
float x
Definition: Math2d.h:84
float r1
Definition: Math3d.h:95
float r7
Definition: Math3d.h:95
float r2
Definition: Math3d.h:95
void WorldToImageCoordinates(const Vec3d &worldPoint, Vec2d &imagePoint, bool bUseDistortionParameters=true) const
Transforms 3D world coordinates to 2D image coordinates.
bool ConvertImage(const CByteImage *pInputImage, CByteImage *pOutputImage, bool bFast=false, const MyRegion *pROI=0)
Converts a grayscale CByteImage to an RGB CByteImage image and vice versa.
IplImage * Adapt(const CByteImage *pImage, bool bAllocateMemory=false)
Converts a CByteImage to an IplImage.
const CCameraParameters & GetCameraParameters() const
Gives access to the camera parameters.
Definition: Calibration.h:268
float r8
Definition: Math3d.h:95
bool GetPointsAndTranslationAndRotation(const CCalibration *pCalibration, const CByteImage *pImage, int nColumns, int nRows, float fSquareSize, Vec2d *pPoints, Mat3d &rotation, Vec3d &translation)
void PutText(CByteImage *pImage, const char *pText, double x, double y, double scale_x, double scale_y, int r=255, int g=255, int b=255, int thickness=1)
Draws text into a CByteImage.
float y
Definition: Math2d.h:84
int width
The width of the image in pixels.
Definition: ByteImage.h:257
int height
The height of the image in pixels.
Definition: ByteImage.h:264
Data structure for the representation of a 3D vector.
Definition: Math3d.h:73
Camera model parameters and functions for a single camera.
Definition: Calibration.h:125
float r6
Definition: Math3d.h:95
void DrawLine(CByteImage *pImage, const PointPair2d &line, int r=255, int g=255, int b=255, int thickness=1)
Draws a line segment into a CByteImage, given its two end points.
void DrawExtrinsic(CByteImage *pResultImage, const CCalibration *pCalibration, const Vec2d *pPoints, int nPoints, float fSquareSize)
Data structure for the representation of a 3x3 matrix.
Definition: Math3d.h:93
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
void SetVec(Vec2d &vec, float x, float y)
Definition: Math2d.cpp:68
float r9
Definition: Math3d.h:95
float r5
Definition: Math3d.h:95
void SetVec(Vec3d &vec, float x, float y, float z)
Definition: Math3d.cpp:243
void DrawCircle(CByteImage *pImage, float mx, float my, float radius, int r=255, int g=255, int b=255, int thickness=1, bool bAntiAlias=false)
Draws a circle into a CByteImage.
Struct containing all parameters of the camera model.
Definition: Calibration.h:133