Opencv入门——矩阵的掩膜操作
掩膜操作解释
掩膜操作就是实现图像对比度调整红色是中心像素,从上到下,从左到右对每个像素做同样的处理操作,得到最终结果就是对比度提高之后的输出图像Mat对象
代码实现
# Opencv中该代码对应的函数调用
#include<opencv2/opencv.hpp>
#include<highgui.h>
using namespace cv;
int main(int argc, char** argv)
{
//加载图像
Mat testimage = imread("D:/photo/02.png");
CV_Assert(testimage.depth() == CV_8U);
namedWindow("test_image", CV_WINDOW_AUTOSIZE);
imshow("test_image", testimage);
//使用Filter2D函数
Mat result;
Mat kern = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(testimage, result, testimage.depth(), kern);
//显示结果
namedWindow("result_image", CV_WINDOW_AUTOSIZE);
imshow("result_image", result);
waitKey(0);
return 0;
}
其中
定义掩膜:Mat kernel = (Mat_(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D( src, dst, src.depth(), kernel );其中src与dst是Mat类型变量、src.depth表示位图深度,有32、24、8等(实在不知道输入什么,就输入-1)。