Loading...
Searching...
No Matches
samples/cpp/intersectExample.cpp
Examples of how intersectConvexConvex works
/*
* 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
{
vector<Point> rectangle;
rectangle.push_back(topLeft);
rectangle.push_back(bottomRight);
return rectangle;
}
{
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
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)
{
// If the input is invalid, draw the intersection in red
{
}
fillPoly(image, intersectionPolygon, fillColor);
}
return intersectArea;
}
{
const size_t bufSize=1024;
char caption[bufSize];
snprintf(caption, bufSize, "Intersection area: %d%s", intersectionArea, description.c_str());
}
static void intersectConvexExample()
{
float intersectionArea;
intersectionArea = drawIntersection(image,
intersectionArea = drawIntersection(image,
intersectionArea = drawIntersection(image,
true);
intersectionArea = drawIntersection(image,
false);
intersectionArea = drawIntersection(image,
true);
// These rectangles share an edge so handleNested can be false and an intersection is still found
intersectionArea = drawIntersection(image,
false);
intersectionArea = drawIntersection(image,
false);
// A vertex of the triangle lies on an edge of the rectangle so handleNested can be false and an intersection is still found
intersectionArea = drawIntersection(image,
false);
// Show intersection of overlapping rectangle and triangle
intersectionArea = drawIntersection(image,
false);
// This concave polygon is invalid input to intersectConvexConvex so it returns an invalid intersection
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,
notConvex,
false);
imshow("Intersections", image);
waitKey(0);
}
int main()
{
intersectConvexExample();
}
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
void fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar &color, int lineType=LINE_8, int shift=0, Point offset=Point())
Fills the area bounded by one or more polygons.
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws several polygonal curves.
float intersectConvexConvex(InputArray p1, InputArray p2, OutputArray p12, bool handleNested=true)
Finds intersection of two convex polygons.
void fillColor(const cv::Scalar &color)
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106
STL namespace.