Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the area of the intersection of two rectangles

I have two rectangles, each identified by a set of four coordinates. I've read up on how to see whether they intersect, but how can I calculate the area of the intersection? The rectangles are not axis-aligned.

Is there an OpenCV function for this? I was told there was, but I fail to find it.

like image 965
dkb Avatar asked Dec 06 '10 10:12

dkb


People also ask

How do you find the overlapping area of two rectangles?

We basically add areas of two rectangles. This includes the intersecting part twice, so we subtract the area of intersecting part. Similarly, we can compute area of 2nd rectangle. If the x_distance or y_distance is negative, then the two rectangles do not intersect.

How do you check if two rectangles intersect in Python?

Practical Data Science using PythonTwo rectangles overlap when the area of their intersection is positive. So, two rectangles that only touch at the corner or edges do not overlap. So, if the input is like R1 = [0,0,2,2], R2 = [1,1,3,3], then the output will be True.


1 Answers

Treat your rectangles as general polygons, and decompose the problem into two steps:

  • compute the intersection of the two polygons, which itself is a polygon (or is empty);
  • compute the area of the resulting polygon.

There's plenty of literature on the Web for both problems.

I don't know anything about OpenCV so can't give any advice there.

like image 85
NPE Avatar answered Oct 24 '22 17:10

NPE