Fill a matrix
What is the easiest way to fill a cv::Mat object with scalars of a single value?
What is the easiest way to fill a cv::Mat object with scalars of a single value?
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 -0600
Seen: 3,991 times
Last updated: Feb 26 '13