各种算子
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <opencv2\imgproc\types_c.h>
using namespace cv;
int main(int argc, char** argv)
{
Mat src,dst1,dst2,dst3,dst4,dst5;
src = imread("2.jpg");
if (!src.data)
{
printf("无\n");
return -1;
}
char INPUT_WIN[] = "原图";
char OUTPUT_WIN[] = "Robert X";
char OUTPUT_DC[] = "Robert Y";
namedWindow(INPUT_WIN, WINDOW_FREERATIO);
namedWindow(OUTPUT_WIN, WINDOW_FREERATIO);
namedWindow(OUTPUT_DC, WINDOW_FREERATIO);
namedWindow("Sobel_X", WINDOW_FREERATIO);
namedWindow("Sobel_Y", WINDOW_FREERATIO);
namedWindow("拉普拉斯算子", WINDOW_FREERATIO);
imshow(INPUT_WIN, src);
// Robert X 方向 (逆时针45)
Mat kernel = (Mat_<int>(2, 2) << 1, 0, 0, -1);
filter2D(src, dst1, -1, kernel, Point(-1, -1), 0.0);
imshow(OUTPUT_WIN, dst1);
// Robert Y 方向( 顺时针45)
Mat kernel2 = (Mat_<int>(2, 2) << 0, 1, -1, 0);
filter2D(src, dst2, -1, kernel2, Point(-1, -1), 0.0);
imshow(OUTPUT_DC, dst2);
//Sobel算子 X
Mat 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("Sobel_X", dst3);
//Sobel算子 Y
Mat 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("Sobel_Y", dst4);
//拉普拉斯算子
Mat kernel5= (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
filter2D(src, dst5, -1, kernel5, Point(-1, -1), 0.0);
imshow("拉普拉斯算子", dst5);
waitKey(0);
return 0;
}
效果图
?
?
?自定义卷积
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <opencv2\imgproc\types_c.h>
using namespace cv;
int main(int argc, char** argv)
{
Mat src,dst;
src = imread("2.jpg");
if (!src.data)
{
printf("无\n");
return -1;
}
char INPUT_WIN[] = "原图";
namedWindow(INPUT_WIN, WINDOW_FREERATIO);
namedWindow("自定义卷积", WINDOW_FREERATIO);
imshow(INPUT_WIN, src);
int c = 0;
int index = 0;
int ksize = 3;
while (true)
{
c = waitKey(500);
if ((char)c == 27) //ESC退出
{
break;
}
ksize = 4 + (index % 5) * 2 + 1;
Mat kernel = Mat::ones(Size(ksize, ksize), CV_32F) / (float)(ksize * ksize);
filter2D(src, dst, -1, kernel, Point(-1, -1));
index++;
imshow("自定义卷积", dst);
}
return 0;
}
|