Ask Your Question

Revision history [back]

Why at() return 3 values for a grayscale 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