CalibrationCreator.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 
23 #include "CalibrationCreator.h"
24 
25 // VisionX
26 //#include <VisionX/tools/TypeMapping.h>
28 
29 // IVT
30 #include "Image/ByteImage.h"
31 #include "Image/ImageProcessor.h"
32 #include "Image/IplImageAdaptor.h"
33 
34 #include <opencv2/opencv.hpp>
35 
36 #include <sys/time.h>
37 
38 
39 namespace visionx
40 {
41 
43  {
44  // set desired image provider
45  providerName = getProperty<std::string>("ImageProviderAdapterName").getValue();
46  usingImageProvider(providerName);
47 
48  const int cornersPerRow = getProperty<int>("NumberOfRows").getValue() - 1;
49  const int cornersPerColumn = getProperty<int>("NumberOfColumns").getValue() - 1;
50  const int cornersPerImage = cornersPerColumn * cornersPerRow;
51  const double squareSize = getProperty<double>("PatternSquareSize").getValue();
52 
53  m_pCorners2DFloat = new CvPoint2D32f[cornersPerImage];
54  m_pCorners2D = new CvPoint2D64f[cornersPerImage];
55  m_pCorners3D = new CvPoint3D64f[cornersPerImage];
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  double etalonParams[3] = { double(cornersPerColumn + 1), double(cornersPerRow + 1), squareSize };
66  m_pCalibFilter->SetCameraCount(2);
67  m_pCalibFilter->SetFrames(desiredNumberOfImages);
68  m_pCalibFilter->SetEtalon(CV_CALIB_ETALON_CHESSBOARD, etalonParams);
69 
70 
71 
72  }
73 
74 
75 
76 
78  {
79  // connect to image provider
80  ARMARX_INFO << getName() << " connecting to " << providerName;
81  visionx::ImageProviderInfo imageProviderInfo = getImageProvider(providerName);
82  imageProviderPrx = getProxy<ImageProviderInterfacePrx>(providerName);
83 
84  cameraImages = new CByteImage*[2];
85  cameraImages[0] = tools::createByteImage(imageProviderInfo);
86  cameraImages[1] = tools::createByteImage(imageProviderInfo);
87 
88  startingTime = IceUtil::Time::now();
89  timeOfLastCapture = IceUtil::Time::now();
90  finished = false;
91 
92 
93 
94  enableResultImages(2, imageProviderInfo.imageFormat.dimension, imageProviderInfo.imageFormat.type);
95  }
96 
97 
99  {
100  finished = true;
101  usleep(100000);
102  delete cameraImages[0];
103  delete cameraImages[1];
104  delete[] cameraImages;
105  delete m_pCalibFilter;
106  delete[] m_pCorners3D;
107  delete[] m_pCorners2D;
108  delete[] m_pCorners2DFloat;
109  }
110 
111 
112 
113 
114 
116  {
117  long timeSinceStart = (IceUtil::Time::now() - startingTime).toMilliSeconds();
118 
119  if (timeSinceStart < 10000)
120  {
121  ARMARX_VERBOSE << "Time until start of capture: " << timeSinceStart - 10000 << " ms";
122  usleep(100000);
123  }
124  else if (!finished)
125  {
126  if (!waitForImages(8000))
127  {
128  ARMARX_WARNING << "Timeout or error in wait for images";
129  }
130  else
131  {
132  // get images
133  int nNumberImages = getImages(cameraImages);
134  ARMARX_VERBOSE << getName() << " got " << nNumberImages << " images";
135 
136  if ((IceUtil::Time::now() - timeOfLastCapture).toMilliSeconds() > waitingIntervalBetweenImages)
137  {
138  ARMARX_INFO << "Capturing image " << numberOfCapturedImages + 1 << " of " << desiredNumberOfImages;
139  IplImage* ppIplImages[2] = { IplImageAdaptor::Adapt(cameraImages[0]), IplImageAdaptor::Adapt(cameraImages[1]) };
140 
141  if (m_pCalibFilter->FindEtalon(ppIplImages))
142  {
143  ARMARX_INFO << "Found calibration pattern";
144  numberOfCapturedImages++;
145  m_pCalibFilter->DrawPoints(ppIplImages);
146  m_pCalibFilter->Push();
147  timeOfLastCapture = IceUtil::Time::now();
148 
149  if (numberOfCapturedImages == desiredNumberOfImages)
150  {
151  ARMARX_IMPORTANT << "Calculating calibration";
152 
153  if (m_pCalibFilter->IsCalibrated())
154  {
155  ARMARX_INFO << "Saving camera calibration to file " << m_sCameraParameterFileName;
156  m_pCalibFilter->SaveCameraParams(m_sCameraParameterFileName.c_str());
157  finished = true;
158  ARMARX_IMPORTANT << "Calibration finished";
159  }
160  }
161  }
162  provideResultImages(cameraImages);
163  }
164  else
165  {
166  usleep(10000);
167  }
168  }
169  }
170  }
171 }
CvCalibFilter::SetFrames
virtual bool SetFrames(int totalFrames)
Definition: calibfilter.cpp:258
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
CalibrationCreator.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::CalibrationCreator::onConnectImageProcessor
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
Definition: CalibrationCreator.cpp:77
CvCalibFilter::Push
virtual bool Push(const CvPoint2D32f **points=0)
Definition: calibfilter.cpp:3933
visionx::CalibrationCreator::process
void process() override
Process the vision component.
Definition: CalibrationCreator.cpp:115
visionx::ImageProcessor::getImageProvider
ImageProviderInfo getImageProvider(std::string name, ImageType destinationImageType=eRgb, bool waitForProxy=false)
Select an ImageProvider.
Definition: ImageProcessor.cpp:152
visionx::ImageProviderInfo::imageFormat
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
Definition: ImageProcessor.h:496
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
CvCalibFilter::DrawPoints
virtual void DrawPoints(IplImage **dst)
Definition: calibfilter.cpp:4007
CvCalibFilter::IsCalibrated
bool IsCalibrated() const
Definition: calibfilter.h:119
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::CalibrationCreator::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: CalibrationCreator.cpp:98
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
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
visionx::ImageProcessor::enableResultImages
void enableResultImages(int numberImages, ImageDimension imageDimension, ImageType imageType, const std::string &name="")
Enables visualization.
Definition: ImageProcessor.cpp:227
visionx::CalibrationCreator::onInitImageProcessor
void onInitImageProcessor() override
Setup the vision component.
Definition: CalibrationCreator.cpp:42
ImageUtil.h
CvCalibFilter::SaveCameraParams
virtual bool SaveCameraParams(const char *filename)
Definition: calibfilter.cpp:4157
visionx::ImageProcessor::provideResultImages
void provideResultImages(CByteImage **images, armarx::MetaInfoSizeBasePtr info=nullptr)
sends result images for visualization
Definition: ImageProcessor.cpp:245
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
CvCalibFilter::SetCameraCount
virtual void SetCameraCount(int cameraCount)
Definition: calibfilter.cpp:235
visionx::ImageProcessor::waitForImages
bool waitForImages(int milliseconds=1000)
Wait for new images.
Definition: ImageProcessor.cpp:275
CvCalibFilter
Definition: calibfilter.h:78