Check point isInside matrix

asked 2016-01-26 10:12:03 -0600

I have been working on a piece of code where I need to work with square ROI scattered throughout the image. Each time I need to check if the patch is really contained inside the image (as efficiently as possible), and I expected to find some kind of native method in cv::Mat which provides this functionality. However, I haven't found it. The closest solution I have found is that described in here.

So, I would like to known if anyone knows about some native (fast) solution. Otherwise, I wondered if it is worth adding it as a pull request. The fast solution I was thinking of is sth like this: x >= 0 && y >= 0 && x < mat.cols && y < mat.rows

I'm just willing to contribute, but as a newbie I wondered if such a simple method is not implemented yet because of some main reason I haven't taken into account.

edit retag flag offensive close merge delete

Comments

1

it's probably far too trivial, to have it in the library

berak gravatar imageberak ( 2016-01-26 10:25:54 -0600 )edit

Well, it's clear it's quite a straightforward operation. However, I thought it could be interesting from the point of view of readibility to avoid cluttering and unnecesarily verbose code in favor of more descriptive code. Anyway, if being far too trivial is one reason to keep it out of the library it's good to know. As I said, I'm also interested in better understanding the guidelines before contributing.

jesusbriales gravatar imagejesusbriales ( 2016-01-26 10:37:27 -0600 )edit
2
LBerger gravatar imageLBerger ( 2016-01-26 10:52:54 -0600 )edit

contains does that, but involves the creation of an intermediate object that I'm not interested in for the sake of performance. Furthermore, although valid, I expected the method to be available in the proper cv::Mat to keep the core idea of checking index accessibility rather than geometric containment (although they are equivalent in fact). In fact, since the case of use I was focusing on was that of checking (in advance) if a entire ROI can be accessed, I aimed at a slightly more general overloaded method that check if the point is inside with an extra boundary: x >= boundary && y >= boundary && x + boundary < mat.cols && y + boundary < mat.rows

And thanks again for the comments and opinions! :)

jesusbriales gravatar imagejesusbriales ( 2016-01-26 11:10:17 -0600 )edit

lets say you have a Mat m 640x480 and a Rect( 400,400,250,250) r

r = r & Rect( 0, 0, m.cols, m.rows ) gives you a Rect(400,400,240,80)

sturkmen gravatar imagesturkmen ( 2016-01-26 17:52:59 -0600 )edit