//像素矩阵的指针,获取当前像素矩阵的行指针。从0开始。
//Mat.ptr<uchar>(int row)
const uchar* curent=myimage.ptr<uchar>(row);
//获取当前像素点P(row,col)的像素值
p(row,col)=current[col]
//像素范围处理,将像素值的范围约束在0-255之间
saturate_cast(-100),返回0
saturate_cast(288),返回255
//本次图像掩模使用的矩阵是3*3,掩膜功能是提高图像对比度
掩膜矩阵:
//滤波器原理,使用这个掩膜矩阵与图像对应像素做乘积,乘积得到的值赋给中心像素。掩膜矩阵从上到下,从左到右扫图像。
//实现代码如下:
1.图像的行值比真实的索引大一个维度,src.rows;src.cols.循环时要减一处理。
2.为了使掩膜贴合图像,需要从第一行开始扫,而不是第0行。
3.由于图像的列是三个通道,每次对图像的列像素进行操作,要移动三个像素的位置。
int rows = src.rows;
int offsetx = src.channels();//BGR offsetx=3
int cols = (src.cols - 1)*src.channels();//每列三个通道,因为像素是从0开始记
dst = Mat::zeros(src.size(), src.type());//初始化一个新图像,全黑255
for (int row = 1; row < rows-1; row++)
{
//得到0,1,2行像素的地址
const uchar* previous = src.ptr<uchar>(row - 1);
const uchar* current = src.ptr<uchar>(row);
const uchar* next = src.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]));
}
}
//第二种方式,直接使用API filter
//参数1.原图像
2.输出图像
3.位深度,可以写“-1”,表示按照原图的位深度,
4.掩膜
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst, src.depth(), kernel);
//完整代码
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
int main(int argc, char** argv)
{
Mat src, dst;
src = imread("C:\\Users\\Administrator\\Pictures\\Saved Pictures\\timg6ZAGKYHR.jpg");
if (!src.data)
{
printf("could not find..");
return -1;
}
namedWindow("src demo", WINDOW_AUTOSIZE);
imshow("src demo", src);
int rows = src.rows;
int offsetx = src.channels();//BGR offsetx=3
int cols = (src.cols - 1)*src.channels();//每列三个通道,因为像素是从0开始记
dst = Mat::zeros(src.size(), src.type());//初始化一个新图像,全黑255
for (int row = 1; row < rows-1; row++)
{
//得到0,1,2行像素的地址
const uchar* previous = src.ptr<uchar>(row - 1);
const uchar* current = src.ptr<uchar>(row);
const uchar* next = src.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]));
}
}
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst, src.depth(), kernel);
namedWindow("dst demo", WINDOW_AUTOSIZE);
imshow("dst demo", dst);
waitKey(0);
}