openCV图像变换

一. 边缘检测

  1. 实现步骤:
    1 消除噪声 ,使用高斯平滑滤波器卷积降噪。
    2 计算梯度幅值和反向
    3 非极大值抑制
    4 阈值
  2. Canny边缘检测:Canny()函数
c++:
	void Canny(InputArray image,OutputArray edges, double threshold1,
	double threshold2,int apertureSize = 3,bool L2gradient = flase)
1.第一个参数,输入的图像
2. 第二个参数,输出的图像
3. 第三个参数,阈值1
4. 第四个参数,阈值2
5. 第五个参数,Sobel算子的孔径大小,默认为3
6. 第六个参数,一个计算图像梯度幅值的标识,某类为false
  1. 程序实例
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
	Mat src = imread("1.jpg");
	Mat dst,src1=src.clone();
	
	Mat edge,gray,edge1;
	dst.create(src1.size(),src1.type());
	cvtColor(src1,gray,COLOR_BGR2GRAY); 	//转换成灰度图片
	blur(gray,edge,Size(3,3); //降噪
	Canny(edge,	edge1,3,9,3);
	dst = Scalar::all(0);		// 将所有元素都设置为0
	src1.copyTo(dst,edge);
	imshow("B",dst);
	waitKey(0);
	return 0;
}

二. sobel算子

  1. 计算过程
    1 分别在x和y两个方向求导
    2 在图像的每一点,结合以上两个求出近似梯度

  2. Sobel算子:Sobel()函数

c++void Sobel(InputArray src,OutputArray dst,int ddepth,int dx,int dy,int ksize=3,
double scale=1,double delta = 0,int borderType=BORDER_DEFAULT);
1. 第一个参数:输入的图片
2. 第二个参数:输出的图片
3. 第三个参数:输出图像的深度
4. 第四个参数:int类型dx,x方向上的差分阶数
5. 第五个参数:int类型dy,y方向上的差分阶数
6. 第六个参数:int类型的ksize,默认为3,表示Sobel核的大小,必须取1,3,5,7
7. 第七个参数:double类型的scale,计算时的缩放因子
8. 第八个参数:double类型的delta,表示偏移值。
9. 第九个参数:边界模式,默认为BORDER_DEFAULT
  1. 程序实例
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
	Mat grad_x,grad_y;
	Mat abs_grad_x,abs_grad_y,dst;
	Mat src = imread("1.jpg");
	imshow("AA",src);
    // 计算x方向的梯度
	Sobel(src,grad_x,CV_16S,1,0,3,1,1,BORDER_DEFAULT);
	convertScaleAbs(grad_x,abs_grad_x);
	imshow("BB",abs_grad_x);
	// 计算y方向的梯度。
	Sobel(src,grad_y,CV_16S,0,1,3,1,1,BORDER_DEFAULT);
	convertScaleAbs(grad_y,abs_grad_y);
	imshow("CC",abs_grad_y);

	waitkey(0);
	return 0;
}

三. 拉普斯拉变换

  1. Laplaceian()函数
c++:
void Laplaceian(InputArray src,OutputArray dst,int ddepth,int ksize=1,
double scale=1,double delta=0,intborderType=BORDER_DEFAULT);
1. 第一个参数:输入的图片
2. 第二个参数:输出的图片
3. 第三个参数:目标图像的深度
4. 第四个参数:计算二阶导数的滤波器的孔径尺寸
5. 第五个参数:缩放因子
6. 第六个参数:偏移值
7. 第七个参数:边界模式
  1. 程序实例
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
	Mat src,src1,src_gray,dst,abs_gray;
	src = imread("1.jpg");
	
	GaussianBlur(src,src1,Size(3,3),0,0,BORDER_DEFAULT);
	cvtColor(src1,src_gray,COLOR_RGB2GRAY);
	
	Laplaceian(src_gray,dst,CV_16S,3,1,0,BORDER_DEFAULT);
	convertScaleAbs(dst,abs_dst);
	
	imshow("A",abs_dst);
	waitKey(0);
	return 0;


}

三. scharr滤波器

  1. 使用Scharr滤波器分别计算x或y方向的图像差分。
c++:
void Scharr(InputArray src,OutputArray dst,int ddepth,int dx,int dy,
double scale=1,double delta=0,intborderType = BORDER_DEFAULT);
1. 第一个参数:输入图像
2. 第二个参数:输出图像
3. 第三个参数:输出图像深度
4. 第四、五个参数:计算x,y方向的梯度
5. 第六个参数:缩放因子
6. 第七个参数:偏移值
7. 第八个参数:边界模式
  1. 程序实例
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
	Mat grad_x,grad_y;
	Mat abs_grad_x,abs_grad_y,dst;
	Mat src = imread("1.jpg");
	
	Scharr(src,grad_x,CV_16S,1,0,1,0,BORDER_DEFAULT);
	convertScaleAbs(grad_x,abs_grad_x);
	imshow("A",abs_grad_x);
	
	Scharr(src,grad_y,CV_16S,0,1,1,0,BORDER_DEFAULT);
	convertScaleAbs(grad_y,abs_grad_y);
	imshow("B",abs_grad_y);

	waitKey(0);
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FPGA之旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值