Username101
Username101

Reputation: 5

OpenCV ArUco markers, how to get the center of them?

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)?

Upvotes: 0

Views: 2438

Answers (1)

Miki
Miki

Reputation: 41775

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();

Upvotes: 2

Related Questions