【opencv450-samples】intersectExample.cpp凸凸多边形求交:得到重叠区域及面积

46 篇文章 4 订阅

 

示例代码:

/*
 * Author: Steve Nicholson
 *
 * A program that illustrates intersectConvexConvex in various scenarios
 */

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

// Create a vector of points describing a rectangle with the given corners
//矩形:四点
static vector<Point> makeRectangle(Point topLeft, Point bottomRight)
{
    vector<Point> rectangle;
    rectangle.push_back(topLeft);
    rectangle.push_back(Point(bottomRight.x, topLeft.y));
    rectangle.push_back(bottomRight);
    rectangle.push_back(Point(topLeft.x, bottomRight.y));
    return rectangle;
}
//三角形:三点
static vector<Point> makeTriangle(Point point1, Point point2, Point point3)
{
    vector<Point> triangle;
    triangle.push_back(point1);
    triangle.push_back(point2);
    triangle.push_back(point3);
    return triangle;
}

// Run intersectConvexConvex on two polygons then draw the polygons and their intersection (if there is one)
// Return the area of the intersection
// 在两个多边形上运行 intersectConvexConvex 然后绘制多边形和它们的交点(如果有的话)
// 返回交点的面积
static float drawIntersection(Mat &image, vector<Point> polygon1, vector<Point> polygon2, bool handleNested = true)
{
    vector<Point> intersectionPolygon;//交点向量

    vector<vector<Point> > polygons;//多个多边形
    polygons.push_back(polygon1);
    polygons.push_back(polygon2);
    //凸凸多边形求交
    float intersectArea = intersectConvexConvex(polygon1, polygon2, intersectionPolygon, handleNested);

    if (intersectArea > 0)
    {
        Scalar fillColor(200, 200, 200);//浅灰色
        // 如果输入无效,用红色绘制交点 If the input is invalid, draw the intersection in red
        if (!isContourConvex(polygon1) || !isContourConvex(polygon2))//有一个多边形不是凸的
        {
            fillColor = Scalar(0, 0, 255);//红色
        }
        fillPoly(image, intersectionPolygon, fillColor);//填充重叠区域多边形
    }
    polylines(image, polygons, true, Scalar(0, 0, 0));//绘制多边形

    return intersectArea;//面积
}
//绘制文字
static void drawDescription(Mat &image, int intersectionArea, string description, Point origin)
{
    const size_t bufSize=1024;
    char caption[bufSize];//标题
    snprintf(caption, bufSize, "Intersection area: %d%s", intersectionArea, description.c_str());
    putText(image, caption, origin, FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0, 0, 0));//显示文本:黑色
}
//求交示例
static void intersectConvexExample()
{
    Mat image(610, 550, CV_8UC3, Scalar(255, 255, 255));
    float intersectionArea;
    //矩形与矩形求交
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 10), Point(50, 50)),
        makeRectangle(Point(20, 20), Point(60, 60)));

    drawDescription(image, (int)intersectionArea, "", Point(70, 40));//
    //矩形-矩形  无交集
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 70), Point(35, 95)),
        makeRectangle(Point(35, 95), Point(60, 120)));

    drawDescription(image, (int)intersectionArea, "", Point(70, 100));
    //矩形A包含矩形B  处理嵌套
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 130), Point(60, 180)),
        makeRectangle(Point(20, 140), Point(50, 170)),
        true);

    drawDescription(image, (int)intersectionArea, " (handleNested true)", Point(70, 160));
    //矩形A包含矩形B  不处理嵌套
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 190), Point(60, 240)),
        makeRectangle(Point(20, 200), Point(50, 230)),
        false);

    drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 220));
    //矩形A包含矩形B,共享边  处理嵌套
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 250), Point(60, 300)),
        makeRectangle(Point(20, 250), Point(50, 290)),
        true);

    drawDescription(image, (int)intersectionArea, " (handleNested true)", Point(70, 280));

    // These rectangles share an edge so handleNested can be false and an intersection is still found
    //这些矩形共享一条边,因此 handleNested 可以为false,并且仍然可以找到交叉点
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 310), Point(60, 360)),
        makeRectangle(Point(20, 310), Point(50, 350)),
        false);

    drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 340));
    //不共享边,不处理嵌套
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 370), Point(60, 420)),
        makeRectangle(Point(20, 371), Point(50, 410)),
        false);

    drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 400));

    // A vertex of the triangle lies on an edge of the rectangle so handleNested can be false and an intersection is still found
    //三角形的一个顶点位于矩形的边上,因此 handleNested 可以为false,并且仍然可以找到交点
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 430), Point(60, 480)),
        makeTriangle(Point(35, 430), Point(20, 470), Point(50, 470)),
        false);

    drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 460));

    // 显示重叠矩形和三角形的交点Show intersection of overlapping rectangle and triangle
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 490), Point(40, 540)),
        makeTriangle(Point(25, 500), Point(25, 530), Point(60, 515)),
        false);

    drawDescription(image, (int)intersectionArea, "", Point(70, 520));

    // This concave polygon is invalid input to intersectConvexConvex so it returns an invalid intersection
    //这个凹多边形是 intersectConvexConvex 的无效输入,因此它返回一个无效的交集
    vector<Point> notConvex;
    notConvex.push_back(Point(25, 560));
    notConvex.push_back(Point(25, 590));
    notConvex.push_back(Point(45, 580));
    notConvex.push_back(Point(60, 600));
    notConvex.push_back(Point(60, 550));
    notConvex.push_back(Point(45, 570));
    intersectionArea = drawIntersection(image,
        makeRectangle(Point(10, 550), Point(50, 600)),
        notConvex,
        false);

    drawDescription(image, (int)intersectionArea, " (invalid input: not convex)", Point(70, 580));

    imshow("Intersections", image);
    waitKey(0);
}

int main()
{
    intersectConvexExample();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值