sheldon90
sheldon90

Reputation: 115

can't understand following c++ code line

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
{
.......
}

Upvotes: 1

Views: 1874

Answers (2)

TaZ
TaZ

Reputation: 743

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
    ...
}

Upvotes: 8

Ed Heal
Ed Heal

Reputation: 59997

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.

Upvotes: 1

Related Questions