dkb
dkb

Reputation: 561

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.

Upvotes: 8

Views: 2707

Answers (2)

Andres Hurtis
Andres Hurtis

Reputation: 275

You can easily convert the Qt libraries code for that in order to be used with OpenCV.

Look for this function:

QRect QRect::operator&(const QRect &r) const 

In qrect.cpp.

Upvotes: 1

NPE
NPE

Reputation: 500157

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.

Upvotes: 2

Related Questions