【 C++ OpenCV画旋转矩形 并返回四个顶点 】

C++ OpenCV画旋转矩形 并返回四个顶点

  • 函数 rectangle2
    • 说明
    • 代码

函数 rectangle2

说明

通过矩形中心点和角度,在图像上找到旋转矩形的四个角点

int rectangle2(Mat& src,vector& out_points, Point2f center, double angle, double w_length,double h_length, Scalar color, int thickness)
参数:
Mat& src 输入并输出图片
vector& out_points 得到四个点
Point2f center 矩形中心点
double angle 矩形角度
double w_length 矩形宽的一半
double h_length 矩形长的一半
Scalar color 颜色
int thickness 线宽 -1为填充**

代码

int rectangle2(Mat& src,vector& out_points, Point2f center, double angle, double w_length,double h_length, Scalar color, int thickness)

    Mat& src  输入并输出图片
    vector& out_points    得到四个点
    Point2f center   矩形中心点
    double angle     矩形角度
    double w_length  矩形宽的一半
    double h_length  矩形长的一半
    Scalar color     颜色
    int thickness    线宽     -1为填充**
int rectangle2(Mat& src,vector<Point2f>& out_points, Point2f center, double angle, double w_length,double h_length, Scalar color, int thickness)
{
    if (src.empty())
        return -1;

    angle = -angle;
    vector<Point2f> before_points(4),later_points(4);
    double cosA = cos(angle / (180.0 / CV_PI));
    double sinA = sin(angle / (180.0 / CV_PI));
    //左上,右上,右下,左下
    before_points[0] = Point(center.x - w_length, center.y - h_length);
    before_points[1] = Point(center.x + w_length, center.y - h_length);
    before_points[2] = Point(center.x + w_length, center.y + h_length);
    before_points[3] = Point(center.x - w_length, center.y + h_length);
    
    //带角度的矩形四角点
    later_points[0].x = (before_points[0].x - center.x) * cosA - (before_points[0].y - center.y) * sinA + center.x;
    later_points[0].y = (before_points[0].x - center.x) * sinA + (before_points[0].y - center.y) * cosA + center.y;

    later_points[1].x = (before_points[1].x - center.x) * cosA - (before_points[1].y - center.y) * sinA + center.x;
    later_points[1].y = (before_points[1].x - center.x) * sinA + (before_points[1].y - center.y) * cosA + center.y;
    
    later_points[2].x = (before_points[2].x - center.x) * cosA - (before_points[2].y - center.y) * sinA + center.x;
    later_points[2].y = (before_points[2].x - center.x) * sinA + (before_points[2].y - center.y) * cosA + center.y;
    
    later_points[3].x = (before_points[3].x - center.x) * cosA - (before_points[3].y - center.y) * sinA + center.x;
    later_points[3].y = (before_points[3].x - center.x) * sinA + (before_points[3].y - center.y) * cosA + center.y;
    
    int thicknes = thickness;
    if (thickness == -1)
        thicknes = 1;
    line(src, later_points[0], later_points[1], color, thicknes);
    line(src, later_points[1], later_points[2], color, thicknes);
    line(src, later_points[2], later_points[3], color, thicknes);
    line(src, later_points[3], later_points[0], color, thicknes);

    out_points = later_points;

    if (thickness == -1)
    {
        Mat mask = Mat::zeros(src.size(), CV_8U);
        line(mask, later_points[0], later_points[1], Scalar(255), thicknes);
        line(mask, later_points[1], later_points[2], Scalar(255), thicknes);
        line(mask, later_points[2], later_points[3], Scalar(255), thicknes);
        line(mask, later_points[3], later_points[0], Scalar(255), thicknes);
        vector<vector<Point>> contours;
        vector<Vec4i> hierarchy;
        findContours(mask, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
        drawContours(src, contours, 0, color, -1);
    }

    return 0;
}

你可能感兴趣的:(opencv,c++,c++,opencv,开发语言)