本篇博文作为系列博文的第一篇,主要讲解一些opencv的基本操作,包括读取、写回、访问像素、修改像素、显示照片。
#include <cv.h>
#include <iostream>
#include <highgui.h>
using namespace cv;
using namespace std;
int main()
{
//读取默认通道(3个通道)照片
Mat src = imread("fruits.jpg");
namedWindow("src", CV_WINDOW_AUTOSIZE);
//获取照片中心像素值,包括三个分量
Vec3b intensity = src.at<Vec3b>(src.cols/2, src.rows/2);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
cout<<"The blue value is "<<(int)blue<<endl;
cout<<"The green value is "<<(int)green<<endl;
cout<<"The red value is "<<(int)red<<endl;
imshow("src",src);
//读取灰度图,参数0代表单通道(灰度)
Mat grey = imread("fruits.jpg",0);
namedWindow("grey",CV_WINDOW_AUTOSIZE);
imshow("grey",grey);
//访问中心的像素值,获得一个范围在0-255之间的整数
Scalar intensity_grey = grey.at<uchar>(grey.cols/2, grey.rows/2);
cout<<"The grey value is "<<intensity_grey.val[0]<<endl;
//设置中心的像素值
grey.at<uchar>(grey.cols/2,grey.rows/2) = 255 ;
imshow("test",grey);
imwrite("grey.jpg",grey);
waitKey(0);
return 0;
}原文地址:http://blog.csdn.net/wxb1553725576/article/details/31034471