OpenCV学习:自定义线性滤波和卷积边缘处理

第十三课 自定义线性滤波
1.卷积概念
卷积是图像处理中的一个操作,是kernel在每个像素上的操作,kernel是一个固定大小的矩阵数组,中心为锚点
把kernel放到像素数组之上,求锚点周围覆盖的像素乘积之和(包括锚点),用来体会换锚点覆盖下的像素值称
为卷积处理。
2.常见算子
Robert算子、Sobel算子、拉普拉斯算子
3.自定义卷积模糊
//输入图像
//输出图像
//图像深度32/8,未知写-1
//卷积核/模板,可以自定义
//锚点位置,Point(-1,-1)
//计算出的像素加delta

	filter2D(Mat src,Mat dst,int depth,Mat kernel,Point anchor,double delta);

4.代码演示

	#include <opencv2/opencv.hpp>
	#include <iostream>
	#include <math.h>

	using namespace cv;
	int main(int argc, char** argv) {
		Mat src, dst;
		int ksize = 0;

		src = imread("D:/vcprojects/images/test1.png");
		if (!src.data) {
			printf("could not load image...\n");
			return -1;
		}

		char INPUT_WIN[] = "input image";
		char OUTPUT_WIN[] = "Custom Blur Filter Result";
		namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
		namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE);

		imshow(INPUT_WIN, src);
		
		// Sobel X 方向
		// Mat kernel_x = (Mat_<int>(3, 3) << -1, 0, 1, -2,0,2,-1,0,1);
		// filter2D(src, dst, -1, kernel_x, Point(-1, -1), 0.0);

		// Sobel Y 方向
		// Mat yimg;
		// Mat kernel_y = (Mat_<int>(3, 3) << -1, -2, -1, 0,0,0, 1,2,1);
		// filter2D(src, yimg, -1, kernel_y, Point(-1, -1), 0.0);

		// 拉普拉斯算子
		//Mat kernel_y = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
		//filter2D(src, dst, -1, kernel_y, Point(-1, -1), 0.0);
		int c = 0;
		int index = 0;
		while (true) {
			c = waitKey(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, dst, -1, kernel, Point(-1, -1));
			index++;
			imshow(OUTPUT_WIN, dst);	
		}

		// imshow("Sobel Y", yimg);
		return 0;
	}

第十四课 图像边缘处理
1.卷积边缘问题
卷积的时候边界像素不能被卷积操作,原因在于边界像素没有完全跟kernel重合,所以导致
3×3滤波时有1个像素边缘没被处理,5×5滤波时有两个像素边缘没被处理。
2.处理边缘
在卷积开始前增加边缘像素,填充的像素值为0或者RGB黑色,确保边缘被处理,在处理后在
去掉这些边缘。

	BORDER_DEFAULT   //默认处理方法
	BORDER_CONSTANT  //填充边缘用指定像素值
	BORDER_REPLICATE //填充像素边缘用已知的像素边缘值
	BORDER_WARP      //用另外一边的像素来补偿填充
	
	//输入图像,输出图像,边缘长度,上下左右,边缘类型
	copyMakeBorder(Mat src,Mat dst,int top,int bottom,int left,int right,int borderType,Scalar value)

3.代码演示

	#include <opencv2/opencv.hpp>
	#include <iostream>
	#include <math.h>

	using namespace cv;
	int main(int argc, char** argv) {
		Mat src, dst;
		src = imread("D:/vcprojects/images/test.jpg");
		if (!src.data) {
			printf("could not load image...\n");
			return -1;
		}
		char INPUT_WIN[] = "input image";
		char OUTPUT_WIN[] = "Border Demo";
		namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
		namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE);
		imshow(INPUT_WIN, src);
		/*
		int top = (int)(0.05*src.rows);
		int bottom = (int)(0.05*src.rows);
		int left = (int)(0.05*src.cols);
		int right = (int)(0.05*src.cols);
		RNG rng(12345);
		int borderType = BORDER_DEFAULT;

		int c = 0;
		while (true) {
			c = waitKey(500);
			// ESC
			if ((char)c == 27) {
				break;
			}
			if ((char)c == 'r') {
				borderType = BORDER_REPLICATE;
			} else if((char)c == 'w') {
				borderType = BORDER_WRAP;
			} else if((char)c == 'c') {
				borderType = BORDER_CONSTANT;
			} 
			Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
			copyMakeBorder(src, dst, top, bottom, left, right, borderType, color);
			imshow(OUTPUT_WIN, dst);
		}
		*/
		//边缘也可高斯模糊
		GaussianBlur(src, dst, Size(5, 5), 0, 0,BORDER_DEFAULT);
		imshow(OUTPUT_WIN, dst);

		waitKey(0);
		return 0;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值