1

I'm new with C++, and try to figuring out what this line of code means:

cur_rect = cv::Rect(cur_rect) & cv::Rect(0, 0, mat->cols, mat->rows); // here
if( cv::Rect(cur_rect) == cv::Rect() )  //here
{
.......
}
John Dibling
  • 99,718
  • 31
  • 186
  • 324
sheldon90
  • 115
  • 1
  • 3
  • 16

2 Answers2

8

The Rect & Rect part intersects two rectangles and gives a non-empty rectangle back when the two inputs overlap.

So you can compare the result to Rect() to see whether there was an intersection. Your code crops cur_rect to (0, 0, mat->cols, mat->rows) and then checks whether it is empty or not.

Sources:

http://opencv.willowgarage.com/documentation/cpp/core_basic_structures.html?highlight=rect

How can one easily detect whether 2 ROIs intersects in OpenCv?

Edit

An alternative implementation, a bit cleaner:

// crop cur_rect to rectangle with matrix 'mat' size:
cur_rect &= cv::Rect(0, 0, mat->cols, mat->rows);
if (cur_rect.area() == 0) {
    // result is empty
    ...
}
Community
  • 1
  • 1
TaZ
  • 743
  • 7
  • 15
  • Why does it construct a new `cv::Rect(cur_rect)` for the comparison, though - wouldn't `cur_rect` on its own do? And if `cv::Rect()` is an empty rect, isn't the `if` test inverted - it's testing whether the intersection is empty? – Rup May 10 '12 at 09:56
  • I think the `cv::Rect(cur_rect)` part is indeed extraneous, but it will work. Edited the response a bit, I think this is a sort of cropping-functionality. – TaZ May 10 '12 at 09:57
  • 1
    I think I would implement the functionality as follows: `cur_rect &= cv::Rect(0, 0, mat->cols, mat->rows); if (cur_rect.area() == 0) { ... }` – TaZ May 10 '12 at 10:06
  • Thanks for replying @TaZ!! It helps me a lot.. =) – sheldon90 May 12 '12 at 01:16
1

I am assuming that cv::Rect(...) methods (or family of them) returns a rectangle object. The line that you do not understand, I assume is an overloaded operator (==) that compares rectangles.

But I am making a lot of assumptions here as I do not have the code for cv class.

As to the & overloaded operator - one assumes that this is doing an intersection or union. Once again without the code it is hard to say.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • 1
    See the "in addition to" paragraph in [the documentation](http://opencv.willowgarage.com/documentation/cpp/basic_structures.html#rect) – Rup May 10 '12 at 09:49