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

2023-12-20 15:38:26

源码如下:

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

文章来源:https://blog.csdn.net/EveryDayOneHour/article/details/135107593
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。