#include <iostream>
#include <opencv2/opencv.hpp>using namespace std;
using namespace cv;//Robert算子
int Demo_Robert(){ char win1[] = "window1"; char win2[] = "window2"; char win3[] = "window3";Mat img1, img2, img3, kernel_x, kernel_y;
img1 = imread("D://images//box//0019-00.jpg"); if (img1.empty()) { cout << "could not load image..."<< endl; return 0; } imshow(win1,img1);//X方向—Robert算子
kernel_x = (Mat_<int>(2,2)<<1,0,0,-1); filter2D(img1,img2,-1,kernel_x,Point(-1,-1),0,0); //Y方向—Robert算子 kernel_y = (Mat_<int>(2, 2) << 0, 1, -1, 0); filter2D(img1, img3, -1, kernel_y, Point(-1, -1), 0, 0);imshow(win2, img2);
imshow(win3, img3); return 0;}//Sobel算子
int Demo_Sobel(){ char win1[] = "window1"; char win2[] = "window2"; char win3[] = "window3";Mat img1, img2, img3, kernel_x, kernel_y;
img1 = imread("D://images//box//0019-00.jpg"); if (img1.empty()) { cout << "could not load image..." << endl; return 0; } imshow(win1, img1);//X方向—Sobel算子
kernel_x = (Mat_<int>(3, 3) << -1,0,1,-2,0,2,-1,0,1); filter2D(img1, img2, -1, kernel_x, Point(-1, -1), 0, 0); //Y方向—Sobel算子 kernel_y = (Mat_<int>(3, 3) << -1,-2,-1,0,0,0,1,2,1); filter2D(img1, img3, -1, kernel_y, Point(-1, -1), 0, 0);imshow(win2, img2);
imshow(win3, img3); return 0;}
//拉普拉斯算子
int Demo_Laplace(){ char win1[] = "window1"; char win2[] = "window2"; char win3[] = "window3";Mat img1, img2, img3, kernel_x, kernel_y;
img1 = imread("D://images//box//0019-00.jpg"); if (img1.empty()) { cout << "could not load image..." << endl; return 0; } imshow(win1, img1);//Laplace算子
kernel_x = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0); filter2D(img1, img2, -1, kernel_x, Point(-1, -1), 0, 0); imshow(win2, img2); return 0;}//自定义卷积核——实现渐进模糊
int Demo_Kernel(){ char win1[] = "window1"; char win2[] = "window2"; char win3[] = "window3";Mat img1, img2, img3, kernel_x, kernel_y;
img1 = imread("D://images//box//0019-00.jpg"); if (img1.empty()) { cout << "could not load image..." << endl; return 0; } imshow(win1, img1);int c = 0;
int index = 0; int ksize = 3; while (true) { c = waitKey(600); if ((char)c==27) { break; } ksize = 4 + (index % 5) * 2 + 1; Mat kernel1 = Mat::ones(Size(ksize,ksize),CV_32F)/(float)(ksize*ksize); filter2D(img1,img2,-1,kernel1,Point(-1,-1)); index++; imshow(win2,img2); }imshow(win2, img2);
return 0;}int main()
{ //Demo_Robert(); //Demo_Sobel(); //Demo_Laplace(); Demo_Kernel();waitKey(0);
return 0;}