2
votes

When I use openCV void Mat::copyTo(OutputArray m, InputArray mask) function, newly allocated matrix is initialized with all zeros before copying the data. Is there any way to initialize with 255 instead of zeros ?

2

2 Answers

5
votes

You simply need to allocate the output matrix before copying data to it. You can create a matrix initialized with a constant as follows Mat A(3,3,CV_32F, Scalar(255)). Alternatively, if you have a pre-declared matrix A, you can re-allocate it with A.create(3,3,CV_32F), and then initialize it with a constant using A = Scalar(255).

So in your case you can do the following:

// Create output matrix initialized with a constant
Mat output(rows, cols, CV_8UC3, Scalar(255,255,255)); 

// Copy your `input` matrix into `output` through your `mask`
input.copyTo(output, mask);
6
votes

See OpenCV Doc about Mat

You can create Mat with contant like

Three channel

   Mat M(cols,rows, CV_8UC3, Scalar::all(255));

Single channel

  Mat M(cols,rows, CV_8UC1, Scalar(255));