Ron Gross
Ron Gross

Reputation: 1624

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

Upvotes: 12

Views: 18004

Answers (3)

LovaBill
LovaBill

Reputation: 5139

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

Upvotes: 12

MBo
MBo

Reputation: 80197

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

Upvotes: 4

Boyko Perfanov
Boyko Perfanov

Reputation: 3047

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++;

Upvotes: 3

Related Questions