Ask Your Question
0

Why at() return 3 values for a grayscale image

asked 2019-04-26 12:35:22 -0500

Huayra gravatar image

Why the grayscale image has 1 channel but still need 'Vec3b' to access the pixel value? And why there should be 3 different values.

char* imageName = argv[1];

Mat image;
image = imread(imageName, 1);

if( argc != 2 || !image.data )
{
    cout << " No image data \n ";
    return -1;
}
cout << "original: " << image.at<Vec3b>(10,10) << endl;
cout << image.size() << " channels: " << image.channels() << endl;
Mat gray_image;
cvtColor( image, gray_image, COLOR_BGR2GRAY );

cout << "gray: " << gray_image.at<Vec3b>(10,10) << endl;
cout << gray_image.size() << " channels: " << gray_image.channels() << endl;
imwrite( "Gray_Image.jpg", gray_image );

The output looks as follows.

original: [110, 130, 225]
[220 x 220] channels: 3
gray: [94, 97, 98]
[220 x 220] channels: 1
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-04-27 15:23:55 -0500

Tetragramm gravatar image

Probably because you're asking for 3 uchar values. Look at the lines with .at<Vec3b>. The type between the < and > is the type it returns. In as grayscale image, the type is uchar, but you're asking for a vector of 3 uchar. Try replacing the Vec3b with uchar.

https://docs.opencv.org/4.1.0/d3/d63/...

edit flag offensive delete link more

Comments

Thank you! This is quite confusing, because I tried uchar, when I print it I got Garbled character. Finally I realized that I need to convert it to int and then print it.

Huayra gravatar imageHuayra ( 2019-04-27 16:28:57 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-04-26 12:35:22 -0500

Seen: 239 times

Last updated: Apr 27 '19