cv::max(a, b) behavior inconsistent with cv::max(b, a) (Bug #3696)


Added by Chen ZHANG almost 11 years ago. Updated almost 10 years ago.


Status:Done Start date:2014-05-15
Priority:Normal Due date:
Assignee:- % Done:

100%

Category:core
Target version:-
Affected version:2.4.0 - 2.4.8 Operating System:Windows
Difficulty: HW Platform:x64
Pull request:

Description

I'm using vs2012 with OpenCV2.4.8 x86 on win7 x64. When trying cv::max with the following code:

Mat a=Mat::ones(2,3, CV_32S);
Mat b;
max(a, b); // 1. OK
max(b, a); // 2. OpenCV Error

An error happened with the second call:

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'a rray op array' (where arrays have the same size and type), nor 'array op scalar' , nor 'scalar op array') in cv::binary_op, file C:\builds\2_4_PackSlave-win32-vc 11-shared\opencv\modules\core\src\arithm.cpp, line 1021

Should not the two calls be of the same effect theorectically?


Associated revisions

Revision edab3916
Added by Vadim Pisarevsky about 10 years ago

Merge pull request #3696 from Dmitry-Me:shouldPassByReference

History

Updated by Chen ZHANG almost 11 years ago

Chen ZHANG wrote:

I'm using vs2012 with OpenCV2.4.8 x86 on win7 x64. When trying cv::max with the following code:

Mat a=Mat::ones(2,3, CV_32S);
Mat b;
max(a, b); // 1. OK
max(b, a); // 2. OpenCV Error

An error happened with the second call:

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'a rray op array' (where arrays have the same size and type), nor 'array op scalar' , nor 'scalar op array') in cv::binary_op, file C:\builds\2_4_PackSlave-win32-vc 11-shared\opencv\modules\core\src\arithm.cpp, line 1021

Should not the two calls be of the same effect theorectically?

I've also posted a question on S.O. here:
http://stackoverflow.com/questions/23661235/opencv-cvmax-behavior-unexpected?noredirect=1#comment36362523_23661235

Updated by Steven Puttemans almost 11 years ago

Actually I think you are using the wrong calls to the function. Looking at the documentation here (http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=max#MatExpr%20max%28const%20Mat&%20a,%20const%20Mat&%20b%29) it seems that there is actually something wrong with the way you call the max function.

Max of two matrices calculates the per element maximum between matrices of same dimensions or they compare to a scalar.

SO I see a couple of problems in your code
  • Using an uninitialized matrix b is not a possible constructor of cv::max
  • Comparing max of a non existing matrix is quite weird

So it should be either

Mat a = Mat::ones(2,3, CV_32S);
Mat b = Mat::zeros(2,3, CV_32S);
Mat result = max(a,b);
Mat result2 = max(b,a);

In which both results will be identical or you can use it as a third matrix element

Mat a = Mat::ones(2,3, CV_32S);
Mat b = Mat::zeros(2,3, CV_32S);
Mat result, result2;
max(a, b, result);
max(b, a, result2)

Which works also.

I will close this down, do reopen if you think I am wrong here.

  • Status changed from Open to Cancelled

Updated by Steven Puttemans almost 11 years ago

And to add an extra remark, the fact that max(a,b) works is because max probably makes an internal copy of a to b if b is an empty matrix. However, this should not work according to the documents... we should treat that second part as a bug.

Updated by Chen ZHANG almost 11 years ago

Steven Puttemans wrote:

And to add an extra remark, the fact that max(a,b) works is because max probably makes an internal copy of a to b if b is an empty matrix. However, this should not work according to the documents... we should treat that second part as a bug.

So now you agree that it may be a bug? Why do you cancel it to hide the problem? I know it's OK to initialize matrices correctly before using max, but that's not the problem I think...

  • Status changed from Cancelled to Open

Updated by Alexander Karsakov over 10 years ago

  • Category set to core

Updated by Camille PHIQUEPAL over 10 years ago

Hi Chen, Alexander,

with this example, there are 2 strange things :
1) The first call doesn't raise any error
2) the max function is not "permutable" i.e max(a, b)!=max(b,a)

I found that, when b is not initialized, in the method MatOp_Bin::assign, the following function is called cv::max(e.a, e.s0, dst) with the second argument as a scalar.
The good function that should be called is cv::max(e.a, e.b, dst) because the two arguments given at the begining are matrix.

The same bug exists for the "min" function.

I've just submitted a pull request to solve this issue.
Best,
Camille

Updated by Philip L almost 10 years ago

fixed for 2.4 fixxing it now for version 3.0

  • Status changed from Open to Done
  • % Done changed from 0 to 100

Also available in: Atom PDF