c++opencvmask

Using setTo in OpenCV with CV_32F images


I have two CV_32F images V1 and V2, of same size 512x512

I am trying to do:

float epsilon = 0.05f;
cv::Mat V = cv::abs(V1-V2);
cv::Mat mask = V < epsilon;
V1.setTo(V2, mask);

I checked that mask is a 512x512 CV_8U image.

setTo triggers a "checkScalar" assertion fail.

When I read the documentation of setTo, it says:

mask
Operation mask of the same size as *this. Its non-zero elements indicate which matrix elements need to be copied. The mask has to be of type CV_8U and can have 1 or multiple channels

Is the documentation wrong? Does mask have to be another type than CV_8U when masking a CV_32F image?


Solution

  • setTo() is only for spreading a scalar across the Mat subject.

    For array data, you want copyTo(). Note that this pushes data into the argument Mat, doesn't pull it into the subject Mat. The m argument receives those values. Flip subject and argument, relative to your previous setTo() attempt.

    The docs specify input arguments to be a generic InputArray very often but the actual type is restricted at runtime (checkScalar and such). The docs for setTo() say that it's expecting a scalar value(), which means no array types.


    I don't know why you'd question the mask argument or the Mat element type. That's all fine.