edu.51cto.comedu.51cto.comOpenCV3.1.0–图像处理教程edu.51cto.com讲师:贾志刚E-Mail:bfnh1998@hotmail.com微博:流浪的鱼-GloomyFishedu.51cto.com图像操作读写图像读写像素修改像素值edu.51cto.com读写图像imread可以指定加载为灰度或者RGB图像Imwrite保存图像文件,类型由扩展名决定edu.51cto.com读写像素读一个GRAY像素点的像素值(CV_8UC1)Scalarintensity=img.at(y,x);或者Scalarintensity=img.at(Point(x,y));读一个RGB像素点的像素值Vec3fintensity=img.at(y,x);floatblue=intensity.val[0];floatgreen=intensity.val[1];floatred=intensity.val[2];edu.51cto.com修改像素值灰度图像img.at(y,x)=128;RGB三通道图像img.at(y,x)[0]=128;//blueimg.at(y,x)[1]=128;//greenimg.at(y,x)[2]=128;//red空白图像赋值img=Scalar(0);ROI选择Rectr(10,10,100,100);MatsmallImg=img(r);edu.51cto.comVec3b与Vec3FVec3b对应三通道的顺序是blue、green、red的uchar类型数据。Vec3f对应三通道的float类型数据把CV_8UC1转换到CV32F1实现如下:src.convertTo(dst,CV_32F);edu.51cto.com#include#include#include#includeusingnamespacecv;usingnamespacestd;intmain(intargc,char**args){Matimage=imread("D:/test.jpg",IMREAD_COLOR);if(image.empty()){cout<<"couldnotfindtheimageresource..."<(row,col)[0]=0;//blueimage.at(row,col)[1]=0;//green}}}namedWindow("MyImage",CV_WINDOW_AUTOSIZE);imshow("MyImage",image);waitKey(0);return0;}edu.51cto.comThankYou!edu.51cto.com