一、获取图像像素指针
Mat.ptr<uchar>(int i=0) 获取像素矩阵的指针,索引i表示第几行,从0开始计行数。
例如:我们通过imread获取了一张图片的Mat对象myImage,
获得当前行指针const uchar* current= myImage.ptr<uchar>(row);
获取当前像素点P(row, col)的像素值 p(row, col) =current[col]
二、像素范围处理saturate_cast<uchar>
saturate_cast<uchar>(-1),返回 0
saturate_cast<uchar>(300),返回 255
saturate_cast<uchar>(100),返回 100
这个函数的功能是确保RGB值得范围在0~255(uchar)之间
三、读写像素
1.读一个GRAY像素点的像素值(CV_8UC1)
Scalar intensity = img.at<uchar>(y, x); 或者 Scalar intensity = img.at<uchar>(Point(x, y));
2.读一个RGB像素点的像素值
Vec3f intensity = img.at<Vec3f>(y, x);
float blue = intensity.val[0]; /*通道0*/
float green = intensity.val[1]; /*通道1*/
float red = intensity.val[2];/*通道2*/
3.修改灰度图像像素
img.at<uchar>(y, x) = 128; /*将点(x,y)的像素值更改为128*/
4.修改RGB三通道图像像素
img.at<Vec3b>(y,x)[0]=128; // blue
img.at<Vec3b>(y,x)[1]=128; // green
img.at<Vec3b>(y,x)[2]=128; // red
补充:Vec3b对应三通道的顺序是blue、green、red的uchar类型数据,而Vec3f则是float类型数据。