Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

blending 3-channel images with setTo and mask: error: checkScalar?

Hi, I am trying to blend background image (3-channel) with another image, which was draw with some geometric primitives. I want to keep background intact where there is nothing in the foreground, and blend the rest.

This is the minimal example, which compiles but fails at runtime:

#include<opencv2/core.hpp>
int main(void){
    cv::Mat back(10,10,CV_8UC3);
    cv::Mat fore(10,10,CV_8UC3);
    // mask is nonzero where there is something in fore - summed over all 3 channels
    cv::Mat mask; 
    cv::transform(fore,mask,cv::Matx13f(1,1,1));
    float alpha=.3;
    // blend pixels which have non-zero mask, keep the rest of back intact
    back.setTo((1-alpha)*back+alpha*fore,mask>0);
}

This is the error I get:

OpenCV Error: Assertion failed (checkScalar(value, type(), _value.kind(), _InputArray::MAT )) in setTo, file /build/opencv-AuXD2R/opencv-3.3.1/modules/core/src/copy.cpp, line 514
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv-AuXD2R/opencv-3.3.1/modules/core/src/copy.cpp:514: error: (-215) checkScalar(value, type(), _value.kind(), _InputArray::MAT ) in function setTo

Can I have more explanation of that? Both matrices are 3-channel, mask is 1-channel of type CV_8UC1. Is there any more straightforward way how to do some kind of alpha-blending?

Thanks!