"Assertion failed" with cv::merge, in release mode but not in debug (Win/x64)

asked 2016-03-08 17:45:20 -0500

Benoit_rosa gravatar image

updated 2016-03-12 16:59:19 -0500

Hi,

On Visual studio, I am getting an Assertion failed error linked to the use of the cv::merge function. The error is as following: OpenCV Error : Assertion failed <mv && n > 0> in cv::merge

This is a runtime error that it is happening only in release mode, with the code running nicely in debug mode. I am compiling this using Visual Studio 2012, with OpenCV 3.1.0 that I compiled myself from source.

The error is happening when trying to merge 3 channels back into a RGB image, let's say something like:

(get B,G,R images from code. Matrices are valid and image same type and size)

std::vector<cv::Mat> array_to_merge ;

array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);

cv::merge(array_to_merge,result);

Looking at the OpenCV source code, I actually found that the function that is called is in modules/core/src/convert.cpp, line 341:

void cv::merge(InputArrayOfArrays _mv, OutputArray _dst)
{
    CV_OCL_RUN(_mv.isUMatVector() && _dst.isUMat(),
               ocl_merge(_mv, _dst))

    std::vector<Mat> mv;
    _mv.getMatVector(mv);
    merge(!mv.empty() ? &mv[0] : 0, mv.size(), _dst);
}

It looks to me like the line _mv.getMatVector(mv); does not copy the contents of _mv into mv, leaving an empty MatVector to be called in the last line, then raising the exception. Why it happens only in release mode is beyond my understanding. This is however confirmed by the fact that this code is running both in debug and release modes :

(get B,G,R images from code. Matrices are valid and image same type and size)

std::vector<cv::Mat> array_to_merge ;

array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);

cv::merge(&array_to_merge[0], array_to_merge.size(), result);

(then do something or display the image)

Looking at this problem, what I see is a rather unusual problem but triggering a bug in a consistent manner. I have found and posted a workaround here, but should I make a ticket on GitHub? Or is there some problem in my simple code that I don't understand and is causing the bug?

FYI, a full code example is here: http://pastebin.com/CFLWrwfM

edit retag flag offensive close merge delete

Comments

OpenCV 3.0.1 is 3.1.0 ? are you using opencl ? can you take another look at your linker settings, are you linking debug opencv libs to release builds by accident ?

can't reproduce your problem on win/x86/ocv3.1/no ocl

berak gravatar imageberak ( 2016-03-09 02:03:03 -0500 )edit

Yes, sorry, it is 3.1.0 and not 3.0.1.

I checked the build and no, I'm not linking debug opencv to release build. That was indeed my first thought. My config is win/x64/ocv3.1/no ocl.

Benoit_rosa gravatar imageBenoit_rosa ( 2016-03-09 05:50:03 -0500 )edit

I've got win/x64/3.1.0/no ocl and I'm not seeing any problem.

Tetragramm gravatar imageTetragramm ( 2016-03-12 17:11:05 -0500 )edit

Interesting. Are you compiling in release mode this code ? http://pastebin.com/CFLWrwfM

Note that the part making the bug on my computer is commented out. You would have to uncomment line 30 and comment out line 33.

Benoit_rosa gravatar imageBenoit_rosa ( 2016-03-12 17:18:56 -0500 )edit

Yes, I did both debug and release, with both versions of merge.

Tetragramm gravatar imageTetragramm ( 2016-03-12 18:04:06 -0500 )edit

Ok, there might be some problem on my config then. I'll double check.

If I remember well, the bug was not appearing if I was linking my release build against libopencvworld310d.lib, which is the debug one ...

Benoit_rosa gravatar imageBenoit_rosa ( 2016-03-12 18:12:06 -0500 )edit