27 #include <opencv2/calib3d.hpp>
41 #include <Calibration/Calibration.h>
42 #include <Image/IplImageAdaptor.h>
49 ArMarkerLocalizerPropertyDefinitions::ArMarkerLocalizerPropertyDefinitions(std::string prefix) :
52 defineOptionalProperty<float>(
"MarkerSize", 40.0,
"The side length of the marker(s)");
53 defineOptionalProperty<std::string>(
54 "ReferenceFrameName",
"DepthCamera",
"Name of the ReferenceFrameName");
55 defineOptionalProperty<std::string>(
"AgentName",
"Armar6",
"name of the agent");
56 defineOptionalProperty<std::string>(
57 "ImageProviderName",
"ImageProvider",
"name of the image provider to use");
66 defs->optional(visuEnabled,
"visu.enabled",
"If true, visualize marker position.");
74 return "ArMarkerLocalizer";
80 imageProviderName = getProperty<std::string>(
"ImageProviderName").getValue();
86 markerSize = getProperty<float>(
"MarkerSize").getValue();
90 StereoCalibrationInterfacePrx calibrationProvider =
91 StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.
proxy);
93 std::unique_ptr<CStereoCalibration> stereoCalibration(
95 CCalibration::CCameraParameters ivtCameraParameters =
96 stereoCalibration->GetLeftCalibration()->GetCameraParameters();
97 cv::Mat cameraMatrix(3, 3, cv::DataType<float>::type);
98 cameraMatrix.at<
float>(0, 0) = ivtCameraParameters.focalLength.x;
99 cameraMatrix.at<
float>(0, 1) = 0;
100 cameraMatrix.at<
float>(0, 2) = ivtCameraParameters.principalPoint.x;
101 cameraMatrix.at<
float>(1, 0) = 0;
102 cameraMatrix.at<
float>(1, 1) = ivtCameraParameters.focalLength.y;
103 cameraMatrix.at<
float>(1, 2) = ivtCameraParameters.principalPoint.y;
104 cameraMatrix.at<
float>(2, 0) = 0;
105 cameraMatrix.at<
float>(2, 1) = 0;
106 cameraMatrix.at<
float>(2, 2) = 1;
108 cv::Mat distortionParameters(4, 1, cv::DataType<float>::type);
110 if (!calibrationProvider->getImagesAreUndistorted())
112 distortionParameters.at<
float>(0, 0) = ivtCameraParameters.distortion[0];
113 distortionParameters.at<
float>(1, 0) = ivtCameraParameters.distortion[1];
114 distortionParameters.at<
float>(2, 0) = ivtCameraParameters.distortion[2];
115 distortionParameters.at<
float>(3, 0) = ivtCameraParameters.distortion[3];
119 distortionParameters.at<
float>(0, 0) = distortionParameters.at<
float>(1, 0) =
120 distortionParameters.at<
float>(2, 0) = distortionParameters.at<
float>(3, 0) = 0;
124 imageSize.width = ivtCameraParameters.width;
125 imageSize.height = ivtCameraParameters.height;
126 arucoCameraParameters.setParams(cameraMatrix, distortionParameters, imageSize);
129 startingTime = IceUtil::Time::now();
131 cameraImages =
new CByteImage*[2];
150 ArMarkerLocalizationResultList result = localizeAllMarkersInternal();
152 std::unique_lock lock(resultMutex);
153 lastLocalizationResult = result;
159 for (
const auto& r : result)
162 .
pose(FramedPosePtr::dynamicCast(r.pose)->toEigen()));
169 ArMarkerLocalizationResultList
170 ArMarkerLocalizer::localizeAllMarkersInternal()
172 IplImage* imageIpl = IplImageAdaptor::Adapt(cameraImages[0]);
173 cv::Mat imageOpenCV = cv::cvarrToMat(imageIpl);
176 std::vector<aruco::Marker> markers;
177 markerDetector.detect(imageOpenCV, markers, arucoCameraParameters, markerSize);
182 for (
const auto& m : markers)
189 std::string refFrame = getProperty<std::string>(
"ReferenceFrameName").getValue();
190 const auto agentName = getProperty<std::string>(
"AgentName").getValue();
192 visionx::ArMarkerLocalizationResultList resultList;
194 for (aruco::Marker marker : markers)
197 visionx::ArMarkerLocalizationResult result;
200 marker.calculateExtrinsics(
201 markerSize, arucoCameraParameters.CameraMatrix, arucoCameraParameters.Distorsion);
202 Eigen::Vector3f position(
203 marker.Tvec.at<
float>(0, 0), marker.Tvec.at<
float>(1, 0), marker.Tvec.at<
float>(2, 0));
207 cv::Matx33f cvOrientation;
209 cv::Rodrigues(marker.Rvec, cvOrientation);
212 orientation << cvOrientation(0, 2), cvOrientation(0, 0), cvOrientation(0, 1),
213 cvOrientation(1, 2), cvOrientation(1, 0), cvOrientation(1, 1), cvOrientation(2, 2),
214 cvOrientation(2, 0), cvOrientation(2, 1);
220 result.id = marker.id;
232 resultList.push_back(result);
238 visionx::ArMarkerLocalizationResultList
241 if (!waitForImages(500))
244 return visionx::ArMarkerLocalizationResultList();
246 getImages(cameraImages);
247 return localizeAllMarkersInternal();
250 ArMarkerLocalizationResultList
253 std::unique_lock lock(resultMutex);
254 return lastLocalizationResult;
268 tab.markerSize.setRange(1.0, 1000.0);
269 tab.markerSize.setSteps(1000);
270 tab.markerSize.setDecimals(1);
271 tab.markerSize.setValue(this->markerSize);
273 grid.
add(
Label(
"Marker size:"), {row, 0}).add(tab.markerSize, {row, 1});
283 if (tab.markerSize.hasValueChanged())
285 this->markerSize = tab.markerSize.getValue();