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