# Fill a matrix

What is the easiest way to fill a cv::Mat object with scalars of a single value?

Fill a matrix

What is the easiest way to fill a cv::Mat object with scalars of a single value?

add a comment

1

You can use matrix multiplication for single channel, or setTo function for multiple channels:

```
cv::Mat mat = cv::Mat::ones(rows,cols,CV_8UC1);
mat *= 12; // to set all values to 12
std::cout << mat << std::endl;
// To set a multi dimensional matrix
cv::mat mat2 = cv::Mat(rows,cols,CV_8UC2);
mat2.setTo(cv::Scalar(12,12)); // to set all values to 12
std::cout << mat2 << std::endl;
```

Asked: **
2013-02-26 09:06:09 -0500
**

Seen: **3,395 times**

Last updated: **Feb 26 '13**

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.