Draw crosshair with rotation angle using OpenCV

The source code is as follows:

void pointRotate(const cv::Point2d &src, const cv::Point2d &base, double angle, cv::Point2d &dst)
{
    //旋转角度参数angle,单位是角度,非弧度
    //点A(x1, y1)基于点O(x0, y0)逆时针旋转angle角度后,得到点B(x2,y2),求解点B坐标
    double x1 = src.x; //点A
    double y1 = src.y;
    double x0 = base.x;
    double y0 = base.y; //点O

    double theta = angle * CV_PI / 180.0;                             //角度转弧度
    double x2 = (x1 - x0) * cos(theta) - (y1 - y0) * sin(theta) + x0; //点B
    double y2 = (x1 - x0) * sin(theta) + (y1 - y0) * cos(theta) + y0;

    dst.x = x2;
    dst.y = y2;
}

void drawCross(cv::Mat &image, cv::Point center, int len, cv::Scalar color, int thickness, double angle)
{
    //不旋转
    if (fabs(angle) < 0.001)
    {
        //绘制横线
        cv::line(image, cv::Point(center.x - len / 2, center.y), cv::Point(center.x + len / 2, center.y), color, thickness);

        //绘制竖线
        cv::line(image, cv::Point(center.x, center.y - len / 2), cv::Point(center.x, center.y + len / 2), color, thickness);
        return;
    }

    //旋转角度angle,单位是角度,非弧度
    cv::Point2d p1;
    cv::Point2d p2;

    //绘制x线
    double x1 = center.x - len / 2;
    double x2 = center.x + len / 2;
    double y = center.y;
    pointRotate(cv::Point2d(x1, y), center, angle, p1);
    pointRotate(cv::Point2d(x2, y), center, angle, p2);
    cv::line(image, p1, p2, color, thickness);

    //绘制y线
    double y1 = center.y - len / 2;
    double y2 = center.y + len / 2;
    double x = center.x;
    pointRotate(cv::Point2d(x, y1), center, angle, p1);
    pointRotate(cv::Point2d(x, y2), center, angle, p2);
    cv::line(image, p1, p2, color, thickness);
}

test:

cv::circle(imageMatch, cv::Point(col, row), 10, cv::Scalar(0, 0, 255), 1);

drawCross(imageMatch, cv::Point(col, row), 10, cv::Scalar(0, 0, 255), 1, angle);

---

Extended reading, rotated rectangle

Basic usage and angle exploration of RotatedRect in OpenCV

Guess you like

Origin blog.csdn.net/libaineu2004/article/details/123066057