Search code examples
opencvmathcomputer-visionaugmented-realityaruco

Getting points in 2D plane under perspective by real distance using Aruco marker


I have ruler which is included on the picture below. The dimensions of the marker, color checker and space between the marker and the color checker are known.

enter image description here

The problem is how to compute position of the color checker's corners (C1-C4) from the coordinates of the aruco marker (M1-M4) when the image is taken under perspective so just multiplying vectors (M2-M1) and (M3-M2) by scalar computed from marker dimensions and color checker dimensions is not possible.

The implementation is in C++ using OpenCV but code is not necessary.

Thanks in advance

Edit: I tried to use perspective transform

const std::vector<cv::Point2f> ruler_inner_rect_real = {
    left_marker.at(0),
    left_marker.at(1),
    left_marker.at(2),
    left_marker.at(3)
};
const std::vector<cv::Point2f> RULER_INNER_RECT_IDEAL = {
    cv::Point2f(0, 0),
    cv::Point2f(this->marker_size, 0),
    cv::Point2f(this->marker_size, this->marker_size),
    cv::Point2f(0, this->marker_size),
};
cv::Mat tmat = cv::getPerspectiveTransform(ruler_inner_rect_real, RULER_INNER_RECT_IDEAL);

But this thing doesn't work because the RULER_INNER_RECT_IDEAL is in the zero position of the image/Mat. What are the correct coordinates to be used with the perspective transform?

Edit: So with the help in comments I got the solution to get points C1-C4:

const std::vector<cv::Point2f> in = {
    cv::Point2f(this->marker_size+this->color_checker_position_mm.width, this->color_checker_position_mm.height),
    cv::Point2f(this->marker_size+this->color_checker_position_mm.width+this->color_checker_width, this->color_checker_position_mm.height),
    cv::Point2f(this->marker_size+this->color_checker_position_mm.width+this->color_checker_width, this->marker_size+this->color_checker_position_mm.height),
    cv::Point2f(this->marker_size+this->color_checker_position_mm.width, this->marker_size+this->color_checker_position_mm.height),
};
std::vector<cv::Point2f> out;
cv::perspectiveTransform(in, out, tmat.inv());

Solution

  • You know "paper" coordinates of all rectangle corners, coordinates of black rect corners at the image and want to find color rect image coordinates?

    In this case you can use OpenCV getPerspectiveTransform function to calculate perspective transformation matrix for black rect (and for whole image), then apply perspectiveTransform to transform color rect corners.