掩膜就是一个矩阵,根据矩阵重新计算图片像素值。
作用:
1.提取感兴趣区
2.屏蔽作用
3.结构特征提取
4.特殊形状图像的制作
借鉴网上有人整理的材料,非常详细https://blog.youkuaiyun.com/qq_42887760/article/details/85730412
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
int main(int argc, char** argv) {
Mat src, dst;
src = imread("D:/vcprojects/images/test.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
/*//掩膜操作
int cols = (src.cols-1) * src.channels();//列数*通道数
int offsetx = src.channels();//图像的通道数
int rows = src.rows;//行数
dst = Mat::zeros(src.size(), src.type());//初始化 dst
for (int row = 1; row < (rows - 1); row++) {
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++) {
//掩膜操作:I(i,j) = 5*I(i,j) - [I(i-1,j)+I(i+1,j)+I(i,j-1)+I(i,j+1)]
output[col] = saturate_cast<uchar>(5 * current[col] - (current[col- offsetx] + current[col+ offsetx] + previous[col] + next[col]));
}
}
*/
double t = getTickCount();
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst, src.depth(), kernel);
//filter2D(src,dst,-1,kernel);
double timeconsume = (getTickCount() - t) / getTickFrequency();
printf("tim consume %.2f\n", timeconsume);
namedWindow("contrast image demo", CV_WINDOW_AUTOSIZE);
imshow("contrast image demo", dst);
waitKey(0);
return 0;
}