VS2017&OPENCV4.0
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
int main(int argc, char** argv) {
Mat src, dst1, dst2, dst3, dst4, dst5, dst6;
int ksize = 0;
src = imread("D:\\test.jpg");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
namedWindow(INPUT_WIN, WINDOW_AUTOSIZE);
imshow(INPUT_WIN, src);
//Robert X 方向
Mat kernel_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);
filter2D(src, dst1, -1, kernel_x, Point(-1, -1), 0.0);
imshow("xRobert", dst1);
//Robert Y 方向
Mat kernel_y = (Mat_<int>(2, 2) << 0, 1, -1, 0);
filter2D(src, dst2, -1, kernel_x, Point(-1, -1), 0.0);
imshow("yRobert", dst2);
//Sobel X 方向
kernel_x = (Mat_<int>(3, 3) << -1, 0, 1, -2,0,2,-1,0,1);
filter2D(src, dst3, -1, kernel_x, Point(-1, -1), 0.0);
imshow("xSobel", dst3);
//Sobel Y 方向
kernel_y = (Mat_<int>(3, 3) << -1, -2, -1, 0,0,0, 1,2,1);
filter2D(src, dst4, -1, kernel_y, Point(-1, -1), 0.0);
imshow("ySobel", dst4);
// 拉普拉斯算子
kernel_y = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
filter2D(src, dst5, -1, kernel_y, Point(-1, -1), 0.0);
imshow("Lap", dst5);
int c = 0;
int index = 0;
while (true) {
c = waitKey(500);//每隔500毫秒模糊一次
if ((char)c == 27) {// ESC
break;
}
ksize = 5 + (index % 8) * 2;
Mat kernel = Mat::ones(Size(ksize, ksize), CV_32F) / (float)(ksize * ksize);
filter2D(src, dst6, -1, kernel, Point(-1, -1));
index++;//递进模糊
imshow("Sobel", dst6);
}
waitKey(0);
return 0;
}