如何访问RGB图像的每个像素值?

时间:2014-09-30 06:22:48

标签: opencv rgb at-command mat

我正在尝试读取RGB图像。但是,我只能访问Vec3b类型,而不是每个频道。 我确定是什么问题。想帮助我摆脱痛苦吗?

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;
    }
}

这是RGB图像的像素值的结果。

[153, 88, 81]          X     Q
[161, 94, 85]    。    ^     T
...

1 个答案:

答案 0 :(得分:3)

您的访问权限很好 []运算符返回的类型为char,因此值将打印为char - 文本字符。只需将其转换为int即可将灰度值视为整数:

cout << int(imgMod.at<cv::Vec3b>(iter_y, iter_x)[0]) << "\t";

更可读和更明确的C ++方法是:

static_cast<int>(imgMod.at<cv::Vec3b>(iter_y, iter_x)[0]) << "\t";

更为酷的是(obscure?) little trick - 请注意+

cout << +imgMod.at<cv::Vec3b>(iter_y, iter_x)[0] << "\t";
//      ^
相关问题