I am trying to read RGB image. However, I can only access with Vec3b type, not each channel. I am sure what is the problem. Would like to help me out of misery?
imgMod = imread("rgb.png");
for (int iter_x = 0; iter_x < imgMod.cols; ++iter_x)
{
for (int iter_y = 0; iter_y < imgMod.rows; ++iter_y)
{
cout << imgMod.at<cv::Vec3b>(iter_y, iter_x) << "\t";
cout << imgMod.at<cv::Vec3b>(iter_y, iter_x)[0] << "\t";
cout << imgMod.at<cv::Vec3b>(iter_y, iter_x)[1] << "\t";
cout << imgMod.at<cv::Vec3b>(iter_y, iter_x)[2] << endl;
}
}
Here is a result for pixel value of RGB image.
[153, 88, 81] X Q
[161, 94, 85] 。 ^ T
...
Your access is fine.
The type returned by the []
operator is char
so the value gets printed as a char
- a text character. Just cast it to int
to see the grey value as an integer:
cout << int(imgMod.at<cv::Vec3b>(iter_y, iter_x)[0]) << "\t";
A (more readable and explicit) C++ way to do it would be this:
static_cast<int>(imgMod.at<cv::Vec3b>(iter_y, iter_x)[0]) << "\t";
Even more cool is this (obscure?) little trick - note the +
:
cout << +imgMod.at<cv::Vec3b>(iter_y, iter_x)[0] << "\t";
// ^