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


     

    示例代码:

    1. /*
    2. * Author: Steve Nicholson
    3. *
    4. * A program that illustrates intersectConvexConvex in various scenarios
    5. */
    6. #include "opencv2/imgproc.hpp"
    7. #include "opencv2/highgui.hpp"
    8. using namespace cv;
    9. using namespace std;
    10. // Create a vector of points describing a rectangle with the given corners
    11. //矩形:四点
    12. static vector<Point> makeRectangle(Point topLeft, Point bottomRight)
    13. {
    14. vector<Point> rectangle;
    15. rectangle.push_back(topLeft);
    16. rectangle.push_back(Point(bottomRight.x, topLeft.y));
    17. rectangle.push_back(bottomRight);
    18. rectangle.push_back(Point(topLeft.x, bottomRight.y));
    19. return rectangle;
    20. }
    21. //三角形:三点
    22. static vector<Point> makeTriangle(Point point1, Point point2, Point point3)
    23. {
    24. vector<Point> triangle;
    25. triangle.push_back(point1);
    26. triangle.push_back(point2);
    27. triangle.push_back(point3);
    28. return triangle;
    29. }
    30. // Run intersectConvexConvex on two polygons then draw the polygons and their intersection (if there is one)
    31. // Return the area of the intersection
    32. // 在两个多边形上运行 intersectConvexConvex 然后绘制多边形和它们的交点(如果有的话)
    33. // 返回交点的面积
    34. static float drawIntersection(Mat &image, vector<Point> polygon1, vector<Point> polygon2, bool handleNested = true)
    35. {
    36. vector<Point> intersectionPolygon;//交点向量
    37. vector<vector<Point> > polygons;//多个多边形
    38. polygons.push_back(polygon1);
    39. polygons.push_back(polygon2);
    40. //凸凸多边形求交
    41. float intersectArea = intersectConvexConvex(polygon1, polygon2, intersectionPolygon, handleNested);
    42. if (intersectArea > 0)
    43. {
    44. Scalar fillColor(200, 200, 200);//浅灰色
    45. // 如果输入无效,用红色绘制交点 If the input is invalid, draw the intersection in red
    46. if (!isContourConvex(polygon1) || !isContourConvex(polygon2))//有一个多边形不是凸的
    47. {
    48. fillColor = Scalar(0, 0, 255);//红色
    49. }
    50. fillPoly(image, intersectionPolygon, fillColor);//填充重叠区域多边形
    51. }
    52. polylines(image, polygons, true, Scalar(0, 0, 0));//绘制多边形
    53. return intersectArea;//面积
    54. }
    55. //绘制文字
    56. static void drawDescription(Mat &image, int intersectionArea, string description, Point origin)
    57. {
    58. const size_t bufSize=1024;
    59. char caption[bufSize];//标题
    60. snprintf(caption, bufSize, "Intersection area: %d%s", intersectionArea, description.c_str());
    61. putText(image, caption, origin, FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0, 0, 0));//显示文本:黑色
    62. }
    63. //求交示例
    64. static void intersectConvexExample()
    65. {
    66. Mat image(610, 550, CV_8UC3, Scalar(255, 255, 255));
    67. float intersectionArea;
    68. //矩形与矩形求交
    69. intersectionArea = drawIntersection(image,
    70. makeRectangle(Point(10, 10), Point(50, 50)),
    71. makeRectangle(Point(20, 20), Point(60, 60)));
    72. drawDescription(image, (int)intersectionArea, "", Point(70, 40));//
    73. //矩形-矩形 无交集
    74. intersectionArea = drawIntersection(image,
    75. makeRectangle(Point(10, 70), Point(35, 95)),
    76. makeRectangle(Point(35, 95), Point(60, 120)));
    77. drawDescription(image, (int)intersectionArea, "", Point(70, 100));
    78. //矩形A包含矩形B 处理嵌套
    79. intersectionArea = drawIntersection(image,
    80. makeRectangle(Point(10, 130), Point(60, 180)),
    81. makeRectangle(Point(20, 140), Point(50, 170)),
    82. true);
    83. drawDescription(image, (int)intersectionArea, " (handleNested true)", Point(70, 160));
    84. //矩形A包含矩形B 不处理嵌套
    85. intersectionArea = drawIntersection(image,
    86. makeRectangle(Point(10, 190), Point(60, 240)),
    87. makeRectangle(Point(20, 200), Point(50, 230)),
    88. false);
    89. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 220));
    90. //矩形A包含矩形B,共享边 处理嵌套
    91. intersectionArea = drawIntersection(image,
    92. makeRectangle(Point(10, 250), Point(60, 300)),
    93. makeRectangle(Point(20, 250), Point(50, 290)),
    94. true);
    95. drawDescription(image, (int)intersectionArea, " (handleNested true)", Point(70, 280));
    96. // These rectangles share an edge so handleNested can be false and an intersection is still found
    97. //这些矩形共享一条边,因此 handleNested 可以为false,并且仍然可以找到交叉点
    98. intersectionArea = drawIntersection(image,
    99. makeRectangle(Point(10, 310), Point(60, 360)),
    100. makeRectangle(Point(20, 310), Point(50, 350)),
    101. false);
    102. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 340));
    103. //不共享边,不处理嵌套
    104. intersectionArea = drawIntersection(image,
    105. makeRectangle(Point(10, 370), Point(60, 420)),
    106. makeRectangle(Point(20, 371), Point(50, 410)),
    107. false);
    108. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 400));
    109. // A vertex of the triangle lies on an edge of the rectangle so handleNested can be false and an intersection is still found
    110. //三角形的一个顶点位于矩形的边上,因此 handleNested 可以为false,并且仍然可以找到交点
    111. intersectionArea = drawIntersection(image,
    112. makeRectangle(Point(10, 430), Point(60, 480)),
    113. makeTriangle(Point(35, 430), Point(20, 470), Point(50, 470)),
    114. false);
    115. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 460));
    116. // 显示重叠矩形和三角形的交点Show intersection of overlapping rectangle and triangle
    117. intersectionArea = drawIntersection(image,
    118. makeRectangle(Point(10, 490), Point(40, 540)),
    119. makeTriangle(Point(25, 500), Point(25, 530), Point(60, 515)),
    120. false);
    121. drawDescription(image, (int)intersectionArea, "", Point(70, 520));
    122. // This concave polygon is invalid input to intersectConvexConvex so it returns an invalid intersection
    123. //这个凹多边形是 intersectConvexConvex 的无效输入,因此它返回一个无效的交集
    124. vector<Point> notConvex;
    125. notConvex.push_back(Point(25, 560));
    126. notConvex.push_back(Point(25, 590));
    127. notConvex.push_back(Point(45, 580));
    128. notConvex.push_back(Point(60, 600));
    129. notConvex.push_back(Point(60, 550));
    130. notConvex.push_back(Point(45, 570));
    131. intersectionArea = drawIntersection(image,
    132. makeRectangle(Point(10, 550), Point(50, 600)),
    133. notConvex,
    134. false);
    135. drawDescription(image, (int)intersectionArea, " (invalid input: not convex)", Point(70, 580));
    136. imshow("Intersections", image);
    137. waitKey(0);
    138. }
    139. int main()
    140. {
    141. intersectConvexExample();
    142. }

  • 相关阅读:
    2023系统架构师---论软件系统架构评估(范文)
    改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP
    第一章 Python的基础语法
    SpringBoot面经总结
    【springboot】12、内容协商
    41_ue4进阶末日生存游戏开发[判断左右键点击]
    速腾聚创发布全固态补盲激光雷达E1,成立合资公司,备战百万产能
    HTML5期末大作业:基于HTML+CSS+JavaScript茶文化中国水墨风格绿色茶叶销售(5页) 学生网页设计作业源码
    中国大学科技园联盟携优积科技走进晋江 探索校地双向赋能新路径
    2-反对称矩阵及其指数函数
  • 原文地址:https://blog.csdn.net/cxyhjl/article/details/125560276