Mat.copyTo with mask broken (Bug #1918)


Added by Andrei Zaharescu about 1 year ago. Updated about 1 year ago.


Status:Done Start date:2012-05-10
Priority:High Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:core
Target version:2.4.1
Affected version: Pull request:

Description

there is a bug in copyTo, when using a mask - an assertion fails.

Here is the sample code that fails:

#include <opencv2/core/core.hpp>
void main() {
cv::Mat_<unsigned char> tmpSrc(100,100);
tmpSrc = 124;
cv::Mat_<unsigned char> tmpMask(100,100);
tmpMask = 255;
cv::Mat_<unsigned char> tmpDst(100,100);
tmpDst = 2;
tmpSrc.copyTo(tmpDst,tmpMask);
}

I've tried initializing everything, but still no dice.

One one end, I've found a problem in
void _OutputArray::create(int dims, const int* size, int type, int i, bool allowTransposed, int fixedDepthMask) const

see the code below

if(fixedType())
{
if(CV_MAT_CN(type) == m.channels() && ((1 << CV_MAT_TYPE(flags)) & fixedDepthMask) != 0 )
type = m.type();
else
CV_Assert(!fixedType() || (CV_MAT_CN(type) == m.channels() && ((1 << CV_MAT_TYPE(flags)) & fixedDepthMask) != 0));
}

Basically if it will ever go into the else, it will always fail, because you have something like

if(A) {
if (B)
bla();
else{
// it got here, so we have A && !B
assert(!A || B); // this will fail
}
}

I looked in how copyTo works if there is no mask, and there is a special case for creating the destination OutputArray if the dimension is 2, so I've copied that here as well..see in the proposed patch. I might not be the best solution, but it's got me over the problem.


copy.cpp.patch (505 Bytes) Andrei Zaharescu, 2012-05-10 07:59 pm


Related issues

duplicated by Bug #1944: bug in matrix.cpp / _OutputArray::create OpenCV 2.4.0 Cancelled 2012-05-16
duplicated by Bug #1997: Assertion in cv::OutputArray::create fails if used on sub... Cancelled 2012-05-29

Associated revisions

Revision 7a7dae36
Added by Vadim Pisarevsky about 1 year ago

fixed problem with Mat::copyTo() with Mat_<> arguments (ticket #1918)

History

Updated by Vadim Pisarevsky about 1 year ago

fixed in r8321

  • Status changed from Open to Done

Also available in: Atom PDF