Ask Your Question
1

Fastest way to check if a value exist using standard C++

asked 2015-02-10 05:22:53 -0500

LivingSparks gravatar image

I often have to test whether a class "exists"

Do something IF cv::Rect is not empty/nil?
Do something IF cv::Mat is not empty/nil?

In Objective-C is easy:

If(someObject){
// the object exist so do what you want
}

is there any possible way to test simply these classes?

Today i do this:

If(myMat.rows != 0){
// My Mat is not empty
}

OR

If(myRect.width != 0){
// My Rect is not empty
}

is there another way to do this?

edit retag flag offensive close merge delete

Comments

5

in general, with classes, checking their mere existance is probably not helpful, you want to know, if they are in a valid state.

Mat and Rect, and many other classes (esp. cv::Ptr) have an emtpy() method.

other classes, that load resources, like VideoCapture or CascadeClassifier, have an isOpened() method for that.

berak gravatar imageberak ( 2015-02-10 05:31:12 -0500 )edit

I cannot see the the empty() method on the cv::Rect variable, but I can see an area() method, that may be used for verification (> 0). Anyway, what does "Rect is not empty" means? It contains 4 ints... You can draw a Rect(5, 5, -1, -2). I think you shall do a method that says if your rectangle is valid or not, based on its members, or maybe based on the image size and its size and position (eg: roi & imgRect == roi)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-10 06:36:28 -0500 )edit

^^ thanks, i was wrong.

berak gravatar imageberak ( 2015-02-10 06:41:04 -0500 )edit

Thanks for the help, however "I" consider a Rect empty when its size width and height are null, anyway it would have been interesting to have a bool connected to the object.

LivingSparks gravatar imageLivingSparks ( 2015-02-10 08:40:46 -0500 )edit

Rect I do not think it can have null values, it contains int members

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-10 09:40:45 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-02-10 08:51:01 -0500

berak gravatar image

if you really need that, just write one:

bool operator ! (const Mat&m) { return m.empty(); }

Mat m1;
Mat m2(1,1,0);
cerr << !m1 << endl;
cerr << !m2 << endl;

1
0
edit flag offensive delete link more

Comments

Thank you! :D

LivingSparks gravatar imageLivingSparks ( 2015-02-10 09:11:01 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-02-10 05:22:53 -0500

Seen: 9,117 times

Last updated: Feb 10 '15