掩膜计算
步骤
1.获取原图像的高度和宽度以及通道数。
2.生成一个和原图同样大小的初始图。
3.遍历每个像素点。获取每个像素点的当前行指针,前一行指针和后一行指针
4.进行掩膜计算。
计算图解
![](https://img2020.cnblogs.com/blog/1486923/202009/1486923-20200927081241678-522859934.png)
代码
Mat inputImage=imread("d:/image/test01.jpg");
namedWindow("inputWindow",WINDOW_NORMAL);
resizeWindow("inputWindow",300,400);
imshow("inputWindow",inputImage);
Mat out=Mat::zeros(inputImage.size(),inputImage.type());
int rows=inputImage.rows;
int offset=inputImage.channels();
int cols=(inputImage.cols-1)*offset;
for (int row=1;row<rows-1;row++){
const uchar* previous=inputImage.ptr<uchar>(row-1);
const uchar* current=inputImage.ptr<uchar>(row);
const uchar* next=inputImage.ptr<uchar>(row+1);
uchar * output=out.ptr<uchar>(row);
for(int col=offset;col<cols-offset;col++){
output[col]=saturate_cast<uchar>(5*current[col]-current[col-offset]-current[col+offset]-previous[col]-next[col]);;
}
}
namedWindow("outputWindow",WINDOW_NORMAL);
resizeWindow("outputWindow",300,400);
imshow("outputWindow",out);
opencv自带函数Filter2D
void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel,Point anchor=Point(-1,-1),double delta=0,
int,borderType=BORDER_DEFAULT );