I'm using ArUco markers and I'd like to get the coordinates of the center of a marquer. This method detectMarkers() followed by the drawDetectedMarkers() draws the Id of the marker. It looks like the bottom left pixel of the word "Id" is placed in the center. How can I get the coordinates so I can print them on my frame without doing pose estimation (if possible)?
Asked
Active
Viewed 2,086 times
0
-
Do you need the center as pixels or as meters? – Kani Jun 03 '20 at 22:21
-
i need the pixel coordinates – Username101 Jun 04 '20 at 02:42
1 Answers
2
Since you already have the 4 vertices, the center is simply their mean (as you can see also in the OpenCV implementation for drawDetectedMarkers
)
Point2f cent(0, 0);
for(int p = 0; p < 4; p++)
cent += currentMarker.ptr< Point2f >(0)[p];
cent = cent / 4.;
So your code should look like (see also the tutorial):
cv::Mat inputImage;
...
std::vector<int> markerIds;
std::vector<std::vector<cv::Point2f>> markerCorners, rejectedCandidates;
cv::Ptr<cv::aruco::DetectorParameters> parameters = cv::aruco::DetectorParameters::create();
cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
cv::aruco::detectMarkers(inputImage, dictionary, markerCorners, markerIds, parameters, rejectedCandidates);
// Draw marker centers
cv::Mat outputImage = inputImage.clone();
for(const auto& corners : markerCorners)
{
cv::Point2f center(0.f, 0.f);
// corners.size() == 4
for(const auto& corner : corners) {
center += corner;
}
center /= 4.f;
cv::circle(outputImage, center, 3, cv::Scalar(255,0,0));
}
cv::imshow("centers", outputImage);
cv::waitKey();

Miki
- 40,887
- 13
- 123
- 202
-
It works thank you, I had the idea to see what drawDetectedMarkers() did but for some reason I only checked the documentation and not the code itself. – Username101 Jun 04 '20 at 13:47
-
Hey again, why did you make "center" be composed of float values? I tried just normal integers and nothing changes even the calculated center displays a float value (i don't know why since i removed the appended ".f". – Username101 Jun 12 '20 at 10:31
-
@Username101 the mean of integer values is not necessarily an integer (mean of [1,2] is 1.5). "center" is a point with float coordinates, so if you pass integers (0,0) or float (0.f, 0.f) or double (0.0, 0.0) the coordinates will always be float. "cv::circle" will draw at integer coordinates, so if you pass a "Point2f" it will be casted to a "Point1i / Point" with integer coordinates. – Miki Jun 12 '20 at 10:44