KinectAndCameraCalibration.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package VisionX::Component
17 * @author David Schiebener <schiebener at kit dot edu>
18 * @copyright 2014 Humanoids Group, HIS, KIT
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
24 
25 // VisionX
28 
29 // IVT
30 #include "Image/ByteImage.h"
31 #include "Image/ImageProcessor.h"
32 #include "Image/IplImageAdaptor.h"
33 #include "Calibration/StereoCalibration.h"
34 #include "Calibration/Calibration.h"
35 
37 #include <opencv2/opencv.hpp>
38 #include <sys/time.h>
39 
40 
41 namespace visionx
42 {
43 
45  {
46  // set desired image provider
47  cameraImageProviderName = getProperty<std::string>("CameraImageProviderAdapterName").getValue();
48  usingImageProvider(cameraImageProviderName);
49 
50  kinectImageProviderName = getProperty<std::string>("KinectImageProviderAdapterName").getValue();
51  usingImageProvider(kinectImageProviderName);
52 
53  const int cornersPerRow = getProperty<int>("NumberOfRows").getValue() - 1;
54  const int cornersPerColumn = getProperty<int>("NumberOfColumns").getValue() - 1;
55  const double squareSize = getProperty<double>("PatternSquareSize").getValue();
56 
57  waitingIntervalBetweenImages = getProperty<int>("WaitingIntervalBetweenImages").getValue();
58  m_sCameraParameterFileName = getProperty<std::string>("OutputFileName").getValue();
59  desiredNumberOfImages = getProperty<int>("NumberOfImages").getValue();
60  numberOfCapturedImages = 0;
61 
62  // initialize calibration filter
63  m_pCalibFilter = new CvCalibFilter();
64  ARMARX_INFO << "Calibration pattern with " << cornersPerColumn + 1 << " x " << cornersPerRow + 1 << " squares of size " << squareSize;
65  etalonParams[0] = double(cornersPerColumn + 1);
66  etalonParams[1] = double(cornersPerRow + 1);
67  etalonParams[2] = squareSize;
68  m_pCalibFilter->SetCameraCount(2);
69  m_pCalibFilter->SetFrames(desiredNumberOfImages);
70  m_pCalibFilter->SetEtalon(CV_CALIB_ETALON_CHESSBOARD, etalonParams);
71 
72  offeringTopic("KinectAndCameraCalibration");
73  }
74 
76  {
77  // connect to image provider
78  ARMARX_INFO << getName() << " connecting to " << cameraImageProviderName;
79  visionx::ImageProviderInfo imageProviderInfo = getImageProvider(cameraImageProviderName);
80  cameraImageProviderPrx = getProxy<ImageProviderInterfacePrx>(cameraImageProviderName);
81  listenerPrx = getTopic<KinectAndCameraCalibrationListenerPrx>("KinectAndCameraCalibration");
82 
83  cameraImages = new CByteImage*[2];
84  cameraImages[0] = tools::createByteImage(imageProviderInfo);
85  cameraImages[1] = tools::createByteImage(imageProviderInfo);
86 
87  ARMARX_INFO << getName() << " connecting to " << kinectImageProviderName;
88  getImageProvider(kinectImageProviderName);
89  kinectImageProviderPrx = getProxy<ImageProviderInterfacePrx>(kinectImageProviderName);
90  kinectImages = new CByteImage*[kinectImageProviderPrx->getNumberImages()];
91 
92  for (int i = 0; i < kinectImageProviderPrx->getNumberImages(); i++)
93  {
94  kinectImages[i] = tools::createByteImage(kinectImageProviderPrx->getImageFormat(), kinectImageProviderPrx->getImageFormat().type);
95  }
96 
97  startingTime = IceUtil::Time::now();
98  timeOfLastCapture = IceUtil::Time::now();
99 
100  finished = false;
101  requested = false;
102  result.result = false;
103  }
104 
106  {
107  finished = true;
108  requested = false;
109  result.result = false;
110 
111  usleep(100000);
112  delete cameraImages[0];
113  delete cameraImages[1];
114  delete[] cameraImages;
115  delete m_pCalibFilter;
116  }
117 
119  {
120  if (requested)
121  {
122  long timeSinceStart = (IceUtil::Time::now() - startingTime).toMilliSeconds();
123 
124  if (timeSinceStart < 10000)
125  {
126  ARMARX_VERBOSE << "Time until start of capture: " << timeSinceStart - 10000 << " ms";
127  usleep(100000);
128  }
129  else if (!finished)
130  {
131  // get images
132  armarx::MetaInfoSizeBasePtr info;
133  int nNumberCameraImages = getImages(cameraImageProviderName, cameraImages, info);
134  ARMARX_VERBOSE << getName() << " got " << nNumberCameraImages << " camera images";
135 
136  armarx::MetaInfoSizeBasePtr info2;
137  int nNumberKinectImages = getImages(kinectImageProviderName, kinectImages, info2);
138  ARMARX_VERBOSE << getName() << " got " << nNumberKinectImages << " kinect images";
139 
140  if (nNumberCameraImages > 0 && nNumberKinectImages > 0)
141  {
142  if ((IceUtil::Time::now() - timeOfLastCapture).toMilliSeconds() > waitingIntervalBetweenImages)
143  {
144  ARMARX_INFO << "Capturing image " << numberOfCapturedImages + 1 << " of " << desiredNumberOfImages;
145  IplImage* ppIplImages[2] = { IplImageAdaptor::Adapt(cameraImages[0]), IplImageAdaptor::Adapt(kinectImages[0]) };
146 
147  if (m_pCalibFilter->FindEtalon(ppIplImages))
148  {
149  ARMARX_INFO << "Found calibration pattern";
150  numberOfCapturedImages++;
151  timeOfLastCapture = IceUtil::Time::now();
152  m_pCalibFilter->DrawPoints(ppIplImages);
153  m_pCalibFilter->Push();
154 
155  if (numberOfCapturedImages == desiredNumberOfImages)
156  {
157  ARMARX_IMPORTANT << "Calculating calibration";
158  usleep(10000);
159 
160  if (m_pCalibFilter->IsCalibrated())
161  {
162  ARMARX_INFO << "Saving camera calibration to file " << m_sCameraParameterFileName;
163  m_pCalibFilter->SaveCameraParams(m_sCameraParameterFileName.c_str());
164  CStereoCalibration calibration;
165  calibration.LoadCameraParameters(m_sCameraParameterFileName.c_str());
166  CCalibration cRight = *calibration.GetRightCalibration();
167  Vec3d translation = cRight.GetCameraParameters().translation;
168  ARMARX_INFO << "RightCalibration traslation; " << translation.x << " " << translation.y << " " << translation.z;
169  Mat3d rotation = cRight.GetCameraParameters().rotation;
170  ARMARX_INFO << "RightCalibration rotation; "
171  << rotation.r1 << " " << rotation.r2 << " " << rotation.r3 << " \n"
172  << rotation.r4 << " " << rotation.r5 << " " << rotation.r6 << " \n"
173  << rotation.r7 << " " << rotation.r8 << " " << rotation.r9 << " \n";
174 
175 
176  CCalibration cLeft = *calibration.GetLeftCalibration();
177  Vec3d translationLeft = cLeft.GetCameraParameters().translation;
178  ARMARX_INFO << "LeftCalibration traslation; " << translationLeft.x << " " << translationLeft.y << " " << translationLeft.z;
179  Mat3d rotationLeft = cLeft.GetCameraParameters().rotation;
180  ARMARX_INFO << "LeftCalibration rotation; "
181  << rotationLeft.r1 << " " << rotationLeft.r2 << " " << rotationLeft.r3 << " \n"
182  << rotationLeft.r4 << " " << rotationLeft.r5 << " " << rotationLeft.r6 << " \n"
183  << rotationLeft.r7 << " " << rotationLeft.r8 << " " << rotationLeft.r9 << " \n";
184 
185  finished = true;
186  result.result = finished;
187  result.transformation = new armarx::Pose(tools::convertIVTtoEigen(rotation), tools::convertIVTtoEigen(translation));
188 
189  listenerPrx->reportCalibrationFinished(result.result);
190  ARMARX_IMPORTANT << "Calibration finished";
191  }
192  }
193  }
194  }
195  else
196  {
197  usleep(10000);
198  }
199  }
200  else
201  {
202  usleep(100000);
203  }
204  }
205  }
206  else
207  {
208  usleep(100000);
209  startingTime = IceUtil::Time::now();
210  }
211  }
212 
213 
215  {
216  requested = true;
217  startingTime = IceUtil::Time::now();
218  }
219 
220  void KinectAndCameraCalibration::stopCalibration(const ::Ice::Current& c)
221  {
222  requested = false;
223  usleep(10000);
224 
225  numberOfCapturedImages = 0;
226  delete m_pCalibFilter;
227  m_pCalibFilter = new CvCalibFilter();
228  m_pCalibFilter->SetCameraCount(2);
229  m_pCalibFilter->SetFrames(desiredNumberOfImages);
230  m_pCalibFilter->SetEtalon(CV_CALIB_ETALON_CHESSBOARD, etalonParams);
231 
232  finished = false;
233  result.result = false;
234 
235  startingTime = IceUtil::Time::now();
236  }
237 
238  KinectPoseCalibration KinectAndCameraCalibration::getCalibrationParameters(const ::Ice::Current& c)
239  {
240  return result;
241  }
242 }
243 
visionx::tools::convertIVTtoEigen
Eigen::Matrix3f convertIVTtoEigen(const Mat3d m)
Definition: TypeMapping.cpp:632
CvCalibFilter::SetFrames
virtual bool SetFrames(int totalFrames)
Definition: calibfilter.cpp:258
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
visionx::KinectAndCameraCalibration::startCalibration
void startCalibration(const ::Ice::Current &c=Ice::emptyCurrent) override
ice related functions (todo: put comments)
Definition: KinectAndCameraCalibration.cpp:214
visionx::KinectAndCameraCalibration::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: KinectAndCameraCalibration.cpp:105
CvCalibFilter::Push
virtual bool Push(const CvPoint2D32f **points=0)
Definition: calibfilter.cpp:3933
Pose.h
visionx::KinectAndCameraCalibration::onConnectImageProcessor
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
Definition: KinectAndCameraCalibration.cpp:75
visionx::ImageProcessor::getImageProvider
ImageProviderInfo getImageProvider(std::string name, ImageType destinationImageType=eRgb, bool waitForProxy=false)
Select an ImageProvider.
Definition: ImageProcessor.cpp:152
visionx::KinectAndCameraCalibration::stopCalibration
void stopCalibration(const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: KinectAndCameraCalibration.cpp:220
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
CvCalibFilter::FindEtalon
virtual bool FindEtalon(IplImage **imgs)
Definition: calibfilter.cpp:3826
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::ImageProviderInfo
Definition: ImageProcessor.h:466
visionx::KinectAndCameraCalibration::process
void process() override
Process the vision component.
Definition: KinectAndCameraCalibration.cpp:118
CvCalibFilter::DrawPoints
virtual void DrawPoints(IplImage **dst)
Definition: calibfilter.cpp:4007
CvCalibFilter::IsCalibrated
bool IsCalibrated() const
Definition: calibfilter.h:119
GfxTL::Vec3d
VectorXD< 3, double > Vec3d
Definition: VectorXD.h:695
visionx::ImageProcessor::usingImageProvider
void usingImageProvider(std::string name)
Registers a delayed topic subscription and a delayed provider proxy retrieval which all will be avail...
Definition: ImageProcessor.cpp:117
visionx::ImageProcessor::getImages
int getImages(CByteImage **ppImages)
Poll images from provider.
Definition: ImageProcessor.cpp:351
CV_CALIB_ETALON_CHESSBOARD
@ CV_CALIB_ETALON_CHESSBOARD
Definition: calibfilter.h:73
CvCalibFilter::SetEtalon
virtual bool SetEtalon(CvCalibEtalonType etalonType, double *etalonParams, int pointCount=0, CvPoint2D32f *points=0)
Definition: calibfilter.cpp:97
visionx::KinectAndCameraCalibration::getCalibrationParameters
visionx::KinectPoseCalibration getCalibrationParameters(const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: KinectAndCameraCalibration.cpp:238
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
visionx::KinectAndCameraCalibration::onInitImageProcessor
void onInitImageProcessor() override
Setup the vision component.
Definition: KinectAndCameraCalibration.cpp:44
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:290
ImageUtil.h
CvCalibFilter::SaveCameraParams
virtual bool SaveCameraParams(const char *filename)
Definition: calibfilter.cpp:4157
KinectAndCameraCalibration.h
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
TypeMapping.h
CvCalibFilter::SetCameraCount
virtual void SetCameraCount(int cameraCount)
Definition: calibfilter.cpp:235
CvCalibFilter
Definition: calibfilter.h:78