Ask Your Question
0

rectA & rectB not working

asked 2016-12-23 01:36:30 -0600

Nbb gravatar image

updated 2016-12-23 02:37:13 -0600

I am trying to get the intersection of 2 rectangles. What happens if the 2 rectangles do not intersect at all ? Will it give me an error ? Because the output below is what I am getting. The limits are (0,0,1242,375). I don't know the rectangles I am passing in. I just want to keep those that intersect and to retain the intersection.

[1170 x 232 from (72, 0)]
[1242 x 375 from (0, 0)]
[1242 x 375 from (0, 0)]
[967 x 211 from (275, 0)]
[1242 x 375 from (0, 0)]
[1242 x 375 from (0, 0)]
[1242 x 375 from (0, 0)]
[2147449647 x 375 from (2147483647, 0)]
[1242 x 375 from (0, 0)]

I am using this temporary fix after the intersection operator to remove all erroneous rectangles

if (rect.tl().x < 0 || rect.tl().y < 0 || rect.br().x >= image.cols || rect.br().y >= image.rows 
                                || rect.area() >= image.cols * image.rows || rect.area() == 0)
edit retag flag offensive close merge delete

Comments

1

2147483647 == 0x7fffffff == INT_MAX.

looks like you have a bug elsewhere ...

berak gravatar imageberak ( 2016-12-23 01:47:06 -0600 )edit

thanks ! but wouldn't int_max be reduced to 1242 due to the intersection ? should I introduce another check for INT_MAX ? I am projecting 3D bounding boxes onto a 2D image so I guess if the box lies outside of the image then something weird might happen. I had hoped that the intersection would be enough

Nbb gravatar imageNbb ( 2016-12-23 01:55:49 -0600 )edit
2

"should I introduce another check for INT_MAX" -- no, no, rather be paranoid about the rest of your program(linked wrong libs again ? buffer overflow somewhere ? uninitialized meory ?)

berak gravatar imageberak ( 2016-12-23 02:00:09 -0600 )edit

I think the error just lies at the projection function, if a corner of the 3D bounding box is "on the image plane" then its projection will be at infinity

Nbb gravatar imageNbb ( 2016-12-23 02:05:27 -0600 )edit
2

oooh, that again sounds pretty likely ! (in that case, forget all remarks above.)

berak gravatar imageberak ( 2016-12-23 02:07:39 -0600 )edit
1

is there a solution ? Why doesn't it work for INT_MAX ? isn't that still a number ? Im currently using the area to filter out such rectangles rect.area() < image.rows*image.cols but im afraid if there are other rectangles that just lie out of bounds but still remain below the maximum area since it looks like the intersection operator doesn't work as I had hoped it would

Nbb gravatar imageNbb ( 2016-12-23 02:16:37 -0600 )edit
1

code source for rect intersection is here. INT_MAX+width is not possible

LBerger gravatar imageLBerger ( 2016-12-23 02:57:25 -0600 )edit
1

you can't use area() to filter those out, since INT_MAX * something is already over the top. (or say INT_MAX + anything)

berak gravatar imageberak ( 2016-12-23 03:09:01 -0600 )edit

if a corner of the 3D bounding box is "on the image plane" I understand a field of view problem using a stenope model...

LBerger gravatar imageLBerger ( 2016-12-23 03:14:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-25 07:53:54 -0600

essamzaky gravatar image

updated 2016-12-26 08:26:38 -0600

you can do the following checking to detect if there is intersection between the two rectangles or not

BOOL IsOverLap(RECT A, RECT B)
{
    //return true if there is overlapping between the two rectangles
    return ! (A.top > B.bottom || A.bottom < B.top || A.left > B.right || A.right < B.left);
}

Edit 26/12/2016

And here it is the implemetation by using cv::Rect

BOOL IsOverLapCV(Rect A, Rect B)
{
    //return true if there is overlapping between the two rectangles
    return ! (A.y > (B.y + B.height) || (A.y + A.height) < B.y || A.x > (B.x + B.width) || (A.x + A.width) < B.x);
}
edit flag offensive delete link more

Comments

i like your idea, but you're confusing cv::Rect and the RECT from win32 api above.

berak gravatar imageberak ( 2016-12-25 08:11:22 -0600 )edit

Yes ,it's confusing , he can use OpenCV cv::Rect , i just copied function from my code

essamzaky gravatar imageessamzaky ( 2016-12-25 09:38:49 -0600 )edit

The original issue was that one of the dimension of the rectangle was INT_MAX and thus there is an overflow in the check.

There are two solutions in my opinion:

  • solve / check the issue with the projection on the image plane
  • promote the int type to a bigger type (float or double)
Eduardo gravatar imageEduardo ( 2016-12-26 15:55:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-12-23 01:36:30 -0600

Seen: 246 times

Last updated: Dec 26 '16