25 #include <opencv2/calib3d.hpp>
37 #include <Calibration/Calibration.h>
38 #include <Image/IplImageAdaptor.h>
52 ArMarkerLocalizerPropertyDefinitions::ArMarkerLocalizerPropertyDefinitions(std::string prefix) :
55 defineOptionalProperty<float>(
"MarkerSize", 40.0,
"The side length of the marker(s)");
56 defineOptionalProperty<std::string>(
"ReferenceFrameName",
"DepthCamera",
"Name of the ReferenceFrameName");
57 defineOptionalProperty<std::string>(
"AgentName",
"Armar6",
"name of the agent");
58 defineOptionalProperty<std::string>(
"ImageProviderName",
"ImageProvider",
"name of the image provider to use");
65 defs->optional(visuEnabled,
"visu.enabled",
"If true, visualize marker position.");
72 return "ArMarkerLocalizer";
79 imageProviderName = getProperty<std::string>(
"ImageProviderName").getValue();
85 markerSize = getProperty<float>(
"MarkerSize").getValue();
89 StereoCalibrationInterfacePrx calibrationProvider = StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.
proxy);
91 std::unique_ptr<CStereoCalibration> stereoCalibration(
visionx::tools::convert(calibrationProvider->getStereoCalibration()));
92 CCalibration::CCameraParameters ivtCameraParameters = stereoCalibration->GetLeftCalibration()->GetCameraParameters();
93 cv::Mat cameraMatrix(3, 3, cv::DataType<float>::type);
94 cameraMatrix.at<
float>(0, 0) = ivtCameraParameters.focalLength.x;
95 cameraMatrix.at<
float>(0, 1) = 0;
96 cameraMatrix.at<
float>(0, 2) = ivtCameraParameters.principalPoint.x;
97 cameraMatrix.at<
float>(1, 0) = 0;
98 cameraMatrix.at<
float>(1, 1) = ivtCameraParameters.focalLength.y;
99 cameraMatrix.at<
float>(1, 2) = ivtCameraParameters.principalPoint.y;
100 cameraMatrix.at<
float>(2, 0) = 0;
101 cameraMatrix.at<
float>(2, 1) = 0;
102 cameraMatrix.at<
float>(2, 2) = 1;
104 cv::Mat distortionParameters(4, 1, cv::DataType<float>::type);
106 if (!calibrationProvider->getImagesAreUndistorted())
108 distortionParameters.at<
float>(0, 0) = ivtCameraParameters.distortion[0];
109 distortionParameters.at<
float>(1, 0) = ivtCameraParameters.distortion[1];
110 distortionParameters.at<
float>(2, 0) = ivtCameraParameters.distortion[2];
111 distortionParameters.at<
float>(3, 0) = ivtCameraParameters.distortion[3];
115 distortionParameters.at<
float>(0, 0) = distortionParameters.at<
float>(1, 0) = distortionParameters.at<
float>(2, 0) = distortionParameters.at<
float>(3, 0) = 0;
119 imageSize.width = ivtCameraParameters.width;
120 imageSize.height = ivtCameraParameters.height;
121 arucoCameraParameters.setParams(cameraMatrix, distortionParameters, imageSize);
124 startingTime = IceUtil::Time::now();
126 cameraImages =
new CByteImage*[2];
144 ArMarkerLocalizationResultList result = localizeAllMarkersInternal();
146 std::unique_lock lock(resultMutex);
147 lastLocalizationResult = result;
153 for (
const auto& r : result)
156 .
pose(FramedPosePtr::dynamicCast(r.pose)->toEigen()));
165 ArMarkerLocalizationResultList ArMarkerLocalizer::localizeAllMarkersInternal()
167 IplImage* imageIpl = IplImageAdaptor::Adapt(cameraImages[0]);
168 cv::Mat imageOpenCV = cv::cvarrToMat(imageIpl);
171 std::vector<aruco::Marker> markers;
172 markerDetector.detect(imageOpenCV, markers, arucoCameraParameters, markerSize);
177 for (
const auto& m : markers)
184 std::string refFrame = getProperty<std::string>(
"ReferenceFrameName").getValue();
185 const auto agentName = getProperty<std::string>(
"AgentName").getValue();
187 visionx::ArMarkerLocalizationResultList resultList;
189 for (aruco::Marker marker : markers)
192 visionx::ArMarkerLocalizationResult result;
195 marker.calculateExtrinsics(markerSize, arucoCameraParameters.CameraMatrix, arucoCameraParameters.Distorsion);
196 Eigen::Vector3f position(marker.Tvec.at<
float>(0, 0), marker.Tvec.at<
float>(1, 0), marker.Tvec.at<
float>(2, 0));
200 cv::Matx33f cvOrientation;
202 cv::Rodrigues(marker.Rvec, cvOrientation);
205 orientation << cvOrientation(0, 2), cvOrientation(0, 0), cvOrientation(0, 1),
206 cvOrientation(1, 2), cvOrientation(1, 0), cvOrientation(1, 1),
207 cvOrientation(2, 2), cvOrientation(2, 0), cvOrientation(2, 1);
213 result.id = marker.id;
225 resultList.push_back(result);
235 if (!waitForImages(500))
238 return visionx::ArMarkerLocalizationResultList();
240 getImages(cameraImages);
241 return localizeAllMarkersInternal();
246 std::unique_lock lock(resultMutex);
247 return lastLocalizationResult;
261 tab.markerSize.setRange(1.0, 1000.0);
262 tab.markerSize.setSteps(1000);
263 tab.markerSize.setDecimals(1);
264 tab.markerSize.setValue(this->markerSize);
266 grid.
add(
Label(
"Marker size:"), {row, 0}).add(tab.markerSize, {row, 1});
275 if (tab.markerSize.hasValueChanged())
277 this->markerSize = tab.markerSize.getValue();