卷积作用:提取边缘,图像增强图像锐化,模糊图像
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
int main()
{Mat src = imread("E:\\vs2015\\opencvstudy\\1.jpg", 1);if (src.empty()){cout << "could not load the src image!" << endl;return -1;}char *input_title = "input Image";imshow(input_title, src);Mat robertx_image, roberty_image;Mat robert_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);filter2D(src, robertx_image, -1, robert_x, Point(-1, -1), 0.0);imshow("robertx", robertx_image);Mat robert_y = (Mat_<int>(2, 2) << 0, 1, -1, 0);filter2D(src, roberty_image, -1, robert_y, Point(-1, -1), 0.0);imshow("roberty", roberty_image);Mat sobelx_image, sobely_image;Mat sobel_x = (Mat_<int>(3, 3) << -1,0,1,-2,0,2,-1,0,1);filter2D(src, sobelx_image, -1, sobel_x, Point(-1, -1), 0.0);imshow("sobelx", sobelx_image);Mat sobel_y = (Mat_<int>(3, 3) << -1,-2,-1,0,0,0,1,2,1);filter2D(src, sobely_image, -1, sobel_y, Point(-1, -1), 0.0);imshow("sobely", sobely_image);Mat lapulasi_image; //边缘检测算子Mat lapulasi = (Mat_<int>(3, 3) << 0,-1,0,-1,4,-1,0,-1,0);filter2D(src, lapulasi_image, -1, lapulasi, Point(-1, -1), 0.0);imshow("lapulasi", lapulasi_image);//利用filter2D自定义卷积核int c = 0;int index = 0;int ksize = 3;Mat filter_image;while (true){c = waitKey(1000);if ((char)c == 27) //ESC{break;}ksize = (index % 5) * 2 + 1;Mat kernal = Mat::ones(Size(ksize,ksize),CV_32F)/(float)(ksize*ksize);filter2D(src, filter_image, -1, kernal, Point(-1, -1));index++;imshow("filter2D", filter_image);}waitKey(0);return 0;
}