获取图像像素指针
函数说明:
Mat.ptr<uchar>(int i=0) 获取图像像素矩阵指针,i表示从第几行开始,从0开始计行数
saturate_cast<uchar>(-100) 返回0
saturate_cast<uchar>(288),返回255
saturate_cast<uchar>(150),返回150
这个函数saturate_cast<uchar>确保了RGB的值在0~255之间
例如我要进行3*3的淹模运算,[0,-1,0,-1,5,-1,0,-1,0]的运算
例子:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
int main(int argc, char** argv) {
Mat dst, srcImage;
srcImage = imread("E:/image/36.jpg");
if (!srcImage.data) {
printf("没有找到图片");
return -1;
}
namedWindow("meinv");
imshow("meinv", srcImage);
int cols = (srcImage.cols - 1)*srcImage.channels(); //获得图像的宽度
int offsetx = srcImage.channels();//开始的位置
int rows = srcImage.rows; //获取图像的高度(矩阵行数)
dst = Mat::zeros(srcImage.size(), srcImage.type());
//遍历图像矩阵的行数,或者每个图像指针(row,col)
for (int row = 1; row < rows - 1; row++) {
const uchar* previous = srcImage.ptr<uchar>(row - 1); //图像指针ptr
const uchar* current = srcImage.ptr<uchar>(row);
const uchar* next = srcImage.ptr<uchar>(row + 1);
uchar* output = dst.ptr<uchar>(row);
//遍历一行中的每个像素
for (int col = offsetx; col < cols; col++) {
//进行淹模操作
output[col] = saturate_cast<uchar>(5 * current[col] - (current[col-offsetx]+current[col+offsetx]+previous[col]+next[col]));
}
}
namedWindow("输出");
imshow("输出", dst);
waitKey(0);
return 0;
}
这个是图像增强淹没模