

// 使用math库里的宏常量
#define _USE_MATH_DEFINES
#include <iostream>
#include <filesystem>
#include <string>
#include <windows.h>
#include <opencv2/opencv.hpp>
namespace fs = std::filesystem;
using namespace cv;
using namespace std;
// 边缘检测 算子 过滤器
void test_filter2D()
{
Mat src;
src = imread("F:/opencv/console/x64/Debug/jzh.png");
imshow("input", src);
Mat dst1, dst2, dst3, dst4, dst5;
// Robert算子 主对角方向
Mat kernel1 = (Mat_<int>(2, 2) << 1, 0, 0, -1);
filter2D(src, dst1, -1, kernel1, Point(-1, -1));
imshow("dst1", dst1);
// Robert算子 次对角方向
Mat kernel2 = (Mat_<int>(2, 2) << 0, 1, -1, 0);
filter2D(src, dst2, -1, kernel2, Point(-1, -1));
imshow("dst2", dst2);
// Sobel算子 x方向
Mat kernel3 = (Mat_<int>(3, 3) << -1, 0, 1, -2, 0, 2, -1, 0, 1);
filter2D(src, dst3, -1, kernel3, Point(-1, -1));
imshow("dst3", dst3);
// Sobel算子 y方向
Mat kernel4 = (Mat_<int>(3, 3) << -1, -2, -1, 0, 0, 0, 1, 2, 1);
filter2D(src, dst4, -1, kernel4, Point(-1, -1));
imshow("dst4", dst4);
// 拉普拉斯算子
Mat kernel5 = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
filter2D(src, dst5, -1, kernel5, Point(-1, -1));
imshow("dst5", dst5);
}
int main()
{
// 边缘检测 算子 过滤器
test_filter2D();
waitKey(0);
destroyAllWindows();
return 0;
}