Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Mat::ones function

Tags:

c++

opencv

According to the docs, this function should return a Mat with all elements as ones.

Mat m = Mat::ones(2, 2, CV_8UC3);

I was expecting to get a 2x2 matrix of [1,1,1]. Instead, I got this:

[1, 0, 0] [1, 0, 0]
[1, 0, 0] [1, 0, 0]

Is this the expected behaviour?

like image 928
cfischer Avatar asked Aug 27 '13 15:08

cfischer


1 Answers

It looks like Mat::ones() works as expected only for single channel arrays. For matrices with multiple channels ones() sets only the first channel to ones while the remaining channels are set to zeros.

Use the following constructor instead:

Mat m = Mat(2, 2, CV_8UC3, Scalar(1,1,1));
std::cout << m;

Edit. Calling

Mat m = Mat::ones(2, 2, CV_8UC3); 

is the same as calling

Mat m = Mat(2, 2, CV_8UC3, 1); // OpenCV replaces `1` with `Scalar(1,0,0)`
like image 62
Alexey Avatar answered Oct 16 '22 21:10

Alexey