Ask Your Question
0

how to return array of minimum values between two arrays

asked 2017-02-13 17:09:19 -0600

jp2112 gravatar image

According to the help docs, there is a function min() that says it computes an output array that is the per element minimum of two input arrays or array and scalar, but when I use this in my code it's not recognized as valid OpenCV (no overload given). What is the function to do this minimum? I'm using 3.2.0 in Win7 on VSE2015 C/C++. Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-02-13 18:15:22 -0600

Tetragramm gravatar image

Many C++ files either include or define a min and max function that often overlaps definition with the OpenCV one.

Try specifying cv::min(), even if you have "using namespace cv;" specified.

You can also use the arguments different ways that make it less likely to have problems.

Normally you would do

result = cv::min(a, b);

but you can also do

cv::min(a, b, result);

You'll note that this does not overlap with any macros that take (x,y).

edit flag offensive delete link more

Comments

yea, I tried both of those ways but it still doesn't like the min part.

    Mat norm(data.size(), CV_32F);
   (norm gets computed here)
      float minnormval = some_threshold;
      Mat result;
      result = cv::min(norm, minnormval); // doesn't work
      cv::min(norm, minnormval, result); // doesn't work

When I hover over the "min" word I get the message

#define min(a,b) (((a) < (b)) ? (a) : (b))
expected an identifier
jp2112 gravatar imagejp2112 ( 2017-02-13 20:31:43 -0600 )edit

Hmm. At the top of your file, before any includes, try putting #define NOMINMAX before all of the includes. If that doesn't work, put parentheses around the function name ex:

(cv::min)(norm, monnormval)
Tetragramm gravatar imageTetragramm ( 2017-02-13 22:27:32 -0600 )edit

The (cv::min) solution worked. The #define may have also worked but elsewhere in my code I was using the standard C overloads for max() and min(), so those lines were now getting flagged.

Perhaps this is a bug in OpenCV, that it doesn't recognize array operators max() or min() unless specified as was done here? Or maybe it's because I'm using VSE?

Many thanks!

jp2112 gravatar imagejp2112 ( 2017-02-13 23:43:21 -0600 )edit

It's a macro with the name min. No idea where it's being defined, but that's the cause.

Putting the () around the method name does nothing, but it stops macros from parsing. Since it can't be a macro with the (), it then parses correctly as a function name.

Tetragramm gravatar imageTetragramm ( 2017-02-14 17:14:35 -0600 )edit

ok. Just a note that (min) also works. And I see that the include file <windows.h>, which I am using in order to get my display size, is where min(a,b) and max(a,b) are defined as macros, so this is why I am having this problem. I found this link:

[http://stackoverflow.com/questions/1632145/use-of-min-and-max-functions-in-c (http://stackoverflow.com/questions/16...)]

jp2112 gravatar imagejp2112 ( 2017-02-14 18:39:25 -0600 )edit

Yep. That ought to have been fixed with the #define NOMINMAX. Not sure why.

Tetragramm gravatar imageTetragramm ( 2017-02-14 19:34:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-13 17:09:19 -0600

Seen: 1,700 times

Last updated: Feb 13 '17