一、概述
案例:
1.加载一张彩色图片,并将彩色图像转换为灰度图像。
2.获取灰度图像和彩色图像的像素值
二、示例图
三、示例代码
//操作像素点 #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char const *argv[]) { /*读取一个灰度图的像素值*/ Mat src = imread("girl.jpg"); imshow("src",src); Mat gray; //色彩转换,将彩色图转为灰度图 cvtColor(src,gray,COLOR_BGR2GRAY); imshow("gray",gray); //读取灰度图的像素点并输出 // cout << gray<<endl; Scalar simpler = gray.at<uchar>(1,1); Scalar simpler2 = gray.at<uchar>(Point(1,1)); cout << simpler<<endl; cout << simpler2<<endl; //读取彩色图像的像素点并输出 Vec3f vec = src.at<Vec3f>(1,1); float blue = vec.val[0]; float green = vec.val[1]; float red = vec.val[2]; cout << "红:"<<red<<" 绿:"<<green<<" 蓝:"<<blue<<endl; waitKey(0); return 0; }