CalibrationCreator2.h
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::ArmarXObjects::CalibrationCreator2
17  * @author Christian R. G. Dreher <c.dreher@kit.edu>
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #pragma once
25 
26 
27 #include <string>
28 
29 // VisionX
31 
32 // IVT
33 #include <opencv2/calib3d.hpp>
34 #include <opencv2/core.hpp>
35 #include <opencv2/core/utility.hpp>
36 #include <opencv2/highgui.hpp>
37 #include <opencv2/imgcodecs.hpp>
38 #include <opencv2/imgproc.hpp>
39 #include <opencv2/videoio.hpp>
40 
41 #include <Math/Math2d.h>
42 #include <Math/Math3d.h>
43 
44 
45 class CByteImage;
46 
47 namespace visionx
48 {
49 
50  class Settings
51  {
52  public:
53  Settings() : goodInput(false)
54  {
55  }
56 
57  enum Pattern
58  {
63  };
64 
65  void
67  {
68  goodInput = true;
69  if (boardSize.width <= 0 || boardSize.height <= 0)
70  {
71  ARMARX_ERROR << "Invalid Board size: " << boardSize.width << " "
72  << boardSize.height;
73  goodInput = false;
74  }
75  if (squareSize <= 10e-6)
76  {
77  ARMARX_ERROR << "Invalid square size " << squareSize;
78  goodInput = false;
79  }
80  if (nrFrames <= 0)
81  {
82  ARMARX_ERROR << "Invalid number of frames " << nrFrames;
83  goodInput = false;
84  }
85 
86  flag = 0;
87 
88  if (useK3ToK6)
89  {
90  flag |= cv::CALIB_RATIONAL_MODEL;
91  }
92  else
93  {
94  flag |= cv::CALIB_FIX_K3;
95  flag |= cv::CALIB_FIX_K4;
96  flag |= cv::CALIB_FIX_K5;
97  flag |= cv::CALIB_FIX_K6;
98  }
99 
100  //flag |= cv::CALIB_FIX_PRINCIPAL_POINT;
101  //flag |= cv::CALIB_ZERO_TANGENT_DIST;
102  if (aspectRatio)
103  {
104  flag |= cv::CALIB_FIX_ASPECT_RATIO;
105  }
106  //flag |= cv::CALIB_FIX_K1;
107  //flag |= cv::CALIB_FIX_K2;
108 
110  {
111  ARMARX_ERROR << " Camera calibration mode does not exist. ";
112  goodInput = false;
113  }
114  }
115 
116  public:
117  cv::Size boardSize; // The size of the board -> Number of items by width and height
118  Pattern calibrationPattern; // One of the Chessboard, circles, or asymmetric circle pattern
119  double squareSize; // The size of a square in your defined unit (point, millimeter,etc).
120  int nrFrames; // The number of frames to use from the input for calibration
121  float aspectRatio; // The aspect ratio
122  bool writePoints; // Write detected feature points
123  bool writeExtrinsics; // Write extrinsic parameters
124  bool flipVertical; // Flip the captured images around the horizontal axis
125  std::string outputFileName; // The name of the file where to write
126  bool useK3ToK6;
127 
128  bool goodInput;
129  int flag;
130  };
131 
133  {
134  public:
135  static const std::string default_name;
136 
137  std::string
138  getDefaultName() const override
139  {
140  return default_name;
141  }
142 
143  protected:
144  // inherited from VisionComponent
145  void onInitImageProcessor() override;
146  void onConnectImageProcessor() override;
147  void onDisconnectImageProcessor() override;
148  void onExitImageProcessor() override;
149 
150  void process() override;
151 
152  /**
153  * @see PropertyUser::createPropertyDefinitions()
154  */
157  {
160 
161  def->defineOptionalProperty<std::string>("ImageProviderAdapterName",
162  "ImageProvider",
163  "Ice Adapter name of the image provider");
164  def->defineOptionalProperty<int>(
165  "NumberOfImages", 20, "Number of images used for the calibration");
166  def->defineOptionalProperty<int>(
167  "WaitingIntervalBetweenImages", 500, "Waiting time between captured images, in ms");
168  def->defineOptionalProperty<int>("NumberOfRows",
169  5,
170  "Number of corner rows on the calibration pattern "
171  "(height; chessboard squares - 1)");
172  def->defineOptionalProperty<int>("NumberOfColumns",
173  5,
174  "Number of corner columns on the calibration pattern "
175  "(width; chessboard squares - 1)");
176  def->defineOptionalProperty<double>(
177  "PatternSquareSize", 30.0, "Size of the squares on the calibration pattern");
178  def->defineOptionalProperty<std::string>(
179  "OutputFileName", "cameras.txt", "Path to the file for saving the calibration");
180  def->defineOptionalProperty<bool>(
181  "UseAdditionalParams", false, "Also use K3-K6 parameters");
182  def->defineOptionalProperty<bool>(
183  "EnableFixAspectRatio", false, "Fix/Calibrate focal length aspect ratio");
184  def->defineOptionalProperty<std::string>(
185  "ImageToUse", "left", "Use left or rigth image for calibration")
186  .map("left", "left")
187  .map("right", "right");
188  return def;
189  }
190 
191  private:
192  enum
193  {
194  CAPTURING,
195  CALIBRATED
196  };
197 
198  int m_mode = CAPTURING;
199 
200  Settings m_settings;
201 
202  std::vector<std::vector<cv::Point2f>> imagePoints;
203  cv::Mat cameraMatrix, distCoeffs;
204  cv::Size imageSize;
205 
206  std::string providerName;
207  ImageProviderInterfacePrx imageProviderPrx;
208 
209  // CvCalibFilter* m_pCalibFilter;
210  // CvPoint2D32f* m_pCorners2DFloat;
211  // CvPoint2D64f* m_pCorners2D;
212  // CvPoint3D64f* m_pCorners3D;
213 
214  CByteImage** cameraImages;
215  //int waitingIntervalBetweenImages, desiredNumberOfImages, numberOfCapturedImages;
216  std::string m_outputFileName;
217 
218  IceUtil::Time waitingTimeBetweenCaptures;
219  IceUtil::Time timeOfLastCapture;
220  unsigned int imageID;
221  bool finished;
222  };
223 
224 } // namespace visionx
visionx::Settings::Pattern
Pattern
Definition: CalibrationCreator2.h:57
visionx::Settings::validate
void validate()
Definition: CalibrationCreator2.h:66
visionx::Settings::flag
int flag
Definition: CalibrationCreator2.h:129
visionx::Settings::calibrationPattern
Pattern calibrationPattern
Definition: CalibrationCreator2.h:118
visionx::Settings::writePoints
bool writePoints
Definition: CalibrationCreator2.h:122
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ImageProcessor
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
Definition: ImageProcessor.h:98
visionx::Settings::useK3ToK6
bool useK3ToK6
Definition: CalibrationCreator2.h:126
visionx::Settings::flipVertical
bool flipVertical
Definition: CalibrationCreator2.h:124
visionx::Settings::CIRCLES_GRID
@ CIRCLES_GRID
Definition: CalibrationCreator2.h:61
visionx::Settings::goodInput
bool goodInput
Definition: CalibrationCreator2.h:128
visionx::Settings::squareSize
double squareSize
Definition: CalibrationCreator2.h:119
visionx::Settings
Definition: CalibrationCreator2.h:50
visionx::Settings::NOT_EXISTING
@ NOT_EXISTING
Definition: CalibrationCreator2.h:59
visionx::CalibrationCreator2::onConnectImageProcessor
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
Definition: CalibrationCreator2.cpp:358
visionx::Settings::ASYMMETRIC_CIRCLES_GRID
@ ASYMMETRIC_CIRCLES_GRID
Definition: CalibrationCreator2.h:62
visionx::CalibrationCreator2::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: CalibrationCreator2.h:156
ImageProcessor.h
visionx::Settings::nrFrames
int nrFrames
Definition: CalibrationCreator2.h:120
visionx::Settings::aspectRatio
float aspectRatio
Definition: CalibrationCreator2.h:121
visionx::CalibrationCreator2::process
void process() override
Process the vision component.
Definition: CalibrationCreator2.cpp:426
visionx::Settings::outputFileName
std::string outputFileName
Definition: CalibrationCreator2.h:125
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
visionx::Settings::CHESSBOARD
@ CHESSBOARD
Definition: CalibrationCreator2.h:60
visionx::Settings::Settings
Settings()
Definition: CalibrationCreator2.h:53
visionx::CalibrationCreator2::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: CalibrationCreator2.h:138
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
visionx::CalibrationCreator2::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: CalibrationCreator2.cpp:421
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
visionx::CalibrationCreator2::default_name
static const std::string default_name
Definition: CalibrationCreator2.h:135
visionx::Settings::boardSize
cv::Size boardSize
Definition: CalibrationCreator2.h:117
visionx::CalibrationCreator2
Definition: CalibrationCreator2.h:132
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::Settings::writeExtrinsics
bool writeExtrinsics
Definition: CalibrationCreator2.h:123
visionx::CalibrationCreator2::onDisconnectImageProcessor
void onDisconnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component looses network ...
Definition: CalibrationCreator2.cpp:413
visionx::CalibrationCreator2::onInitImageProcessor
void onInitImageProcessor() override
Setup the vision component.
Definition: CalibrationCreator2.cpp:334