OpenCV中两个旋转矩形RotatedRect的交集

来源:互联网 发布:淘宝水印怎么设置大小 编辑:程序博客网 时间:2024/05/22 12:13

OpenCV中两个旋转矩形RotatedRect的交集

#include <vector>#include <iostream>#include "opencv2/opencv.hpp"static void DrawRotatedRect(cv::Mat& img, cv::RotatedRect& rr, cv::Scalar color){    cv::Point2f pts[4] = { 0.0 };    rr.points(pts);    for (int i = 0; i < 4; i++)    {        cv::line(img, pts[i], pts[(i + 1) % 4], color, 1);    }}static void GetRegion(cv::Mat& img, cv::Mat& dst, int lineValue, int fillValue){    CV_Assert(img.type() == CV_8UC1);    dst = cv::Mat::zeros(img.size(), img.type());    for (int y = 0; y < img.rows; y++)    {        cv::Point left = cv::Point(-1, -1);        cv::Point right = cv::Point(-1, -1);        // 从左到右扫描        for (int x = 0; x < img.cols; x++)        {            int value = img.at<uchar>(y, x);            if (value == lineValue)            {                left.x = x;                left.y = y;                break;            }        }        // 从右边到左边扫描        for (int x = img.cols - 1; x >= 0; x--)        {            int value = img.at<uchar>(y, x);            if (value == lineValue)            {                right.x = x;                right.y = y;                break;            }        }        // 表明没有检测到,直接就是下一行        if ((left.x < 0) && (left.y < 0) && (right.x < 0) && (right.y < 0))        {            continue;        }        if ((left.x >= 0) && (left.y >= 0) && (right.x >= 0) && (right.y >= 0) && (left!=right))        {            cv::line(dst, left, right, fillValue, 1/*thickness*/);        }        else if ((left == right) && (left.x >= 0) && (left.y >= 0))        {            // 只有一个点            dst.at<uchar>(left.y, left.x) = fillValue;        }        else        {            // TODO        }    } // Row}static bool RotatedRectIntersection(cv::RotatedRect r1, cv::RotatedRect r2, cv::Mat& dst, int w, int h){    dst = cv::Mat::zeros(h, w, CV_8UC1);    const int LINE_VALUE = 255;    const int REGION_VALUE = 255;    const int FILL_VALUE = 20;    cv::Mat rrImg1 = cv::Mat::zeros(h, w, CV_8UC1);    cv::Mat rrImg2 = cv::Mat::zeros(h, w, CV_8UC1);    DrawRotatedRect(rrImg1, r1, LINE_VALUE);    DrawRotatedRect(rrImg2, r2, LINE_VALUE);    cv::Mat region1;    cv::Mat region2;    GetRegion(rrImg1, region1, LINE_VALUE, FILL_VALUE);    GetRegion(rrImg2, region2, LINE_VALUE, FILL_VALUE);    int cnt = 0;    // 统计交点数目    for (int y = 0; y < region1.rows; y++)    {        for (int x = 0; x < region1.cols; x++)        {            int v1 = region1.at<uchar>(y, x);            int v2 = region2.at<uchar>(y, x);            if ((v1 == FILL_VALUE) && (v2 == FILL_VALUE))            {                cnt++;                dst.at<uchar>(y, x) = 255;            }        }    }    // 返回是否有交点    if (cnt > 0)    return true;    return false;}void test_rotated_rect_intersection(){    int w = 500;    int h = 300;    cv::RotatedRect rr1 = cv::RotatedRect(cv::Point2f(30, 40), cv::Size2f(20, 30), 45);    cv::RotatedRect rr2 = cv::RotatedRect(cv::Point2f(25, 30), cv::Size2f(25, 40), 120);    cv::Mat dst;    bool flag = RotatedRectIntersection(rr1, rr2, dst, w, h);    DrawRotatedRect(dst, rr1, 255);    DrawRotatedRect(dst, rr2, 255);    cv::imshow("dst", dst);    std::cout << flag << std::endl;    cv::waitKey(0);}int main(int argc, char** argv){    void test_rotated_rect_intersection();    test_rotated_rect_intersection();    cv::waitKey(0);    return 0;}

结果图如下所示:
代码运行结果

原创粉丝点击