简体   繁体   中英

Intersection area of 2 polygons in openCV

I have the contours of 2 polygons (as vector of cv::Point2d).

I would like to calculate the area of intersection between them

What is the easiest way to get it?

Thank you very much!

Ron

Draw the shapes with CV_FILLED in two images and AND them. Area is: CountNonZero(bitwise_and(ShapeAImage,ShapeBImage)) .

The easiest method to code goes like this:

cv::Rect BoundingBox;
int IntersectionArea = 0;
//insert Min-Max X,Y to create the BoundingBox

for (every y inside boundingbox)
     for (every x inside boundingbox)
         if (PointPolygonTest(x,y,Contour1) && PointPolygonTest(x,y,Contour2))
             IntersectionArea++;

You can find intersection polygon wth Clipper library

//create clipper polygons from your points
c.AddPolygons(subj, ptSubject);
c.AddPolygons(clip, ptClip);
c.Execute(ctIntersection, solution, pftNonZero, pftNonZero);

then calc area of this polygon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM