intersectExample.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. static vector<Point> makeRectangle(Point topLeft, Point bottomRight)
  12. {
  13. vector<Point> rectangle;
  14. rectangle.push_back(topLeft);
  15. rectangle.push_back(Point(bottomRight.x, topLeft.y));
  16. rectangle.push_back(bottomRight);
  17. rectangle.push_back(Point(topLeft.x, bottomRight.y));
  18. return rectangle;
  19. }
  20. static vector<Point> makeTriangle(Point point1, Point point2, Point point3)
  21. {
  22. vector<Point> triangle;
  23. triangle.push_back(point1);
  24. triangle.push_back(point2);
  25. triangle.push_back(point3);
  26. return triangle;
  27. }
  28. // Run intersectConvexConvex on two polygons then draw the polygons and their intersection (if there is one)
  29. // Return the area of the intersection
  30. static float drawIntersection(Mat &image, vector<Point> polygon1, vector<Point> polygon2, bool handleNested = true)
  31. {
  32. vector<Point> intersectionPolygon;
  33. vector<vector<Point> > polygons;
  34. polygons.push_back(polygon1);
  35. polygons.push_back(polygon2);
  36. float intersectArea = intersectConvexConvex(polygon1, polygon2, intersectionPolygon, handleNested);
  37. if (intersectArea > 0)
  38. {
  39. Scalar fillColor(200, 200, 200);
  40. // If the input is invalid, draw the intersection in red
  41. if (!isContourConvex(polygon1) || !isContourConvex(polygon2))
  42. {
  43. fillColor = Scalar(0, 0, 255);
  44. }
  45. fillPoly(image, intersectionPolygon, fillColor);
  46. }
  47. polylines(image, polygons, true, Scalar(0, 0, 0));
  48. return intersectArea;
  49. }
  50. static void drawDescription(Mat &image, int intersectionArea, string description, Point origin)
  51. {
  52. const size_t bufSize=1024;
  53. char caption[bufSize];
  54. snprintf(caption, bufSize, "Intersection area: %d%s", intersectionArea, description.c_str());
  55. putText(image, caption, origin, FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0, 0, 0));
  56. }
  57. static void intersectConvexExample()
  58. {
  59. Mat image(610, 550, CV_8UC3, Scalar(255, 255, 255));
  60. float intersectionArea;
  61. intersectionArea = drawIntersection(image,
  62. makeRectangle(Point(10, 10), Point(50, 50)),
  63. makeRectangle(Point(20, 20), Point(60, 60)));
  64. drawDescription(image, (int)intersectionArea, "", Point(70, 40));
  65. intersectionArea = drawIntersection(image,
  66. makeRectangle(Point(10, 70), Point(35, 95)),
  67. makeRectangle(Point(35, 95), Point(60, 120)));
  68. drawDescription(image, (int)intersectionArea, "", Point(70, 100));
  69. intersectionArea = drawIntersection(image,
  70. makeRectangle(Point(10, 130), Point(60, 180)),
  71. makeRectangle(Point(20, 140), Point(50, 170)),
  72. true);
  73. drawDescription(image, (int)intersectionArea, " (handleNested true)", Point(70, 160));
  74. intersectionArea = drawIntersection(image,
  75. makeRectangle(Point(10, 190), Point(60, 240)),
  76. makeRectangle(Point(20, 200), Point(50, 230)),
  77. false);
  78. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 220));
  79. intersectionArea = drawIntersection(image,
  80. makeRectangle(Point(10, 250), Point(60, 300)),
  81. makeRectangle(Point(20, 250), Point(50, 290)),
  82. true);
  83. drawDescription(image, (int)intersectionArea, " (handleNested true)", Point(70, 280));
  84. // These rectangles share an edge so handleNested can be false and an intersection is still found
  85. intersectionArea = drawIntersection(image,
  86. makeRectangle(Point(10, 310), Point(60, 360)),
  87. makeRectangle(Point(20, 310), Point(50, 350)),
  88. false);
  89. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 340));
  90. intersectionArea = drawIntersection(image,
  91. makeRectangle(Point(10, 370), Point(60, 420)),
  92. makeRectangle(Point(20, 371), Point(50, 410)),
  93. false);
  94. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 400));
  95. // A vertex of the triangle lies on an edge of the rectangle so handleNested can be false and an intersection is still found
  96. intersectionArea = drawIntersection(image,
  97. makeRectangle(Point(10, 430), Point(60, 480)),
  98. makeTriangle(Point(35, 430), Point(20, 470), Point(50, 470)),
  99. false);
  100. drawDescription(image, (int)intersectionArea, " (handleNested false)", Point(70, 460));
  101. // Show intersection of overlapping rectangle and triangle
  102. intersectionArea = drawIntersection(image,
  103. makeRectangle(Point(10, 490), Point(40, 540)),
  104. makeTriangle(Point(25, 500), Point(25, 530), Point(60, 515)),
  105. false);
  106. drawDescription(image, (int)intersectionArea, "", Point(70, 520));
  107. // This concave polygon is invalid input to intersectConvexConvex so it returns an invalid intersection
  108. vector<Point> notConvex;
  109. notConvex.push_back(Point(25, 560));
  110. notConvex.push_back(Point(25, 590));
  111. notConvex.push_back(Point(45, 580));
  112. notConvex.push_back(Point(60, 600));
  113. notConvex.push_back(Point(60, 550));
  114. notConvex.push_back(Point(45, 570));
  115. intersectionArea = drawIntersection(image,
  116. makeRectangle(Point(10, 550), Point(50, 600)),
  117. notConvex,
  118. false);
  119. drawDescription(image, (int)intersectionArea, " (invalid input: not convex)", Point(70, 580));
  120. imshow("Intersections", image);
  121. waitKey(0);
  122. }
  123. int main()
  124. {
  125. intersectConvexExample();
  126. }