smttsp
smttsp

Reputation: 4191

zeros function in opencv is not working

I've been trying to learn and use opencv Matrices and I'm stucked on a point.

Here is my code,

Mat my_img, im_rgb;

im_rgb = imread(imageName, 1);
my_img.create(im_rgb.size(), im_rgb.type()); 

cout << (int)my_img.at<Vec3b>(20,20).val[0] << " first\n";

my_img.zeros(im_rgb.size(), im_rgb.type()); 
cout << (int)my_img.at<Vec3b>(20,20).val[0] << " second \n" ;

my_img.at<Vec3b>(20,20).val[0] = 0;
cout << (int)my_img.at<Vec3b>(20,20).val[0] << third;

I read image and I create a Mat, my_img which is same type with im_rgb, CV_8UC3.

The output is

205 first
205 second
0 third

After many trial on output, my interpretation is when the my_img created inside of the Matrix is full of 205 and when I call zeros function, it is not doing anything. But I can change value of elements of the matrix.

When I try this in matlab, it fulfills the matrix with 0.

I wonder, what I'm doing wrong? Thanks,

Upvotes: 1

Views: 351

Answers (1)

ChronoTrigger
ChronoTrigger

Reputation: 8607

If i remember correctly, zeros is a static function of cv::Mat, so it does not affect the object. It is used this way:

my_img = cv::Mat::zeros(im_rgb.size(), im_rgb.type());

Upvotes: 3

Related Questions