Ask Your Question

Finding overlap of two objects in an image using OpenCV

asked 2018-08-18 04:33:43 -0600

infoclogged gravatar image

updated 2018-08-18 04:34:31 -0600

I have this image and would like to find the intersection points of the two objects ( blue vector and red vector ). Unfortunately, the find_intersection method between the vectors from the c++ standard library returns a size 0 because ofcourse there in no overlap of the edges where the two objects meet.

So, is there an elegant way to find the intersecting part of the blue and the red part using OpenCV? Maybe using findContours or CannyEdge or any convolving filter? I am not sure, how I should proceed.image description

edit retag flag offensive close merge delete


WHY is there a border between the regions ?

(there is no real overlap here)

berak gravatar imageberak ( 2018-08-18 06:20:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-08-18 07:09:14 -0600

berak gravatar image

here's an idea:

  • make a mask for the blue and the red part using inRange
  • dilate both masks (make the blobs larger), until they start to overlap (they don't currently, that's the problem)
  • AND the masks to find the overlap
  • (optionally: erode the result again, or apply thinning)
  • use findNonZero() to retrieve the locations for the white overlap pixels

image description

and some code:

Mat mask_red, mask_blue;

inRange(ocv, Scalar(0,0,250), Scalar(0,0,255), mask_red);
inRange(ocv, Scalar(250,250,0), Scalar(255,255,0), mask_blue);
for (int i=0; i<5; i++) {
    dilate(mask_blue, mask_blue, Mat());
    dilate(mask_red, mask_red, Mat());
Mat final = mask_blue & mask_red;
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-08-18 04:33:43 -0600

Seen: 2,030 times

Last updated: Aug 18 '18