自定义线性滤波

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值