Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I think what you want is probably the setTo function. You create a 3 channel image, then call setTo on that object, passing the scalar and the mask. This is assuming your mask image is one channel and you want a three channel image, then this would be the fastest way.

If your binary mask image is already three channel, then the multiply(src1, src2, dst) function will take a scalar in src1 or src2.

I think what you want is probably the setTo function. You create a 3 channel image, then call setTo on that object, passing the scalar and the mask. This is assuming your mask image is one channel and you want a three channel image, then this would be the fastest way.

If your binary mask image is already three channel, then the multiply(src1, src2, dst) function will take a scalar in src1 or src2.

sample code:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv){

    Mat M=Mat::eye(3,3,CV_8UC1);
    Mat M2 = Mat(3,3,CV_8UC3,Scalar(0,0,0));
    cout << "M:\n" << M << endl;

    M2.setTo(Scalar(127,128,255),M);
    cout << "\nM2:\n" << M2 << endl;
    imshow("img", M);
    waitKey(0);
}

output:

M:
[  1,   0,   0;
   0,   1,   0;
   0,   0,   1]

M2:
[127, 128, 255,   0,   0,   0,   0,   0,   0;
   0,   0,   0, 127, 128, 255,   0,   0,   0;
   0,   0,   0,   0,   0,   0, 127, 128, 255]