Opencv入门五 (显示图片灰度值)

源码如下:

#include <opencv2/opencv.hpp>
int main(int argc, char** argv)
{
    cv::Mat img_rgb, img_gry, img_cny;
    cv::namedWindow("Example Gray",cv::WINDOW_AUTOSIZE);
    cv::namedWindow("Example Canny", cv::WINDOW_AUTOSIZE);
    img_rgb = cv::imread(argv[1]);
    cv::cvtColor(img_rgb, img_gry, cv::COLOR_BGR2GRAY);  //生成一个和原图一样大小,但只有一个通道的图像,从而将BGR图像转换为灰度值
    cv::imshow("Example Gray", img_gry);
    cv::Canny(img_gry, img_cny, 10, 100, 3, true);
    cv::imshow("Example Canny", img_cny);
    cv::waitKey(0);

    int x = 16, y = 32;
    cv::Vec3b intensity = img_rgb.at<cv::Vec3b>(y, x);
    uchar blue = intensity[0];     //读写像素值 blue
    uchar green = intensity[1];   //读写像素值 green
    uchar red = intensity[2];       //读写像素值 red
    std::cout << "At(x,y)=(" << x << "," << y << "):(blue,green,red)=(" << (unsigned int)blue <<
        "," << (unsigned int)green << "," << (unsigned int)red << ")" << std::endl;
    std::cout <<"Gray pixel there is:" << (unsigned int)img_gry.at<uchar>(y, x) << std::endl;
    x /= 4; y /= 4;
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值