【OpenCV基础】Sobel、Scharr、Laplacian算子边缘提取

📢:如果你也对机器人、人工智能感兴趣,看来我们志同道合✨
📢:不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852
📢:文章若有幸对你有帮助,可点赞 👍 收藏 ⭐不迷路🙉
📢:内容若有错误,敬请留言 📝指正!原创文,转载请注明出处


一、API-Sobel

Sobel算子又叫索贝尔算子,是计算机视觉领域的一种重要处理方法。主要用于获得数字图像的一阶梯度,常见的应用和物理意义是边缘检测。Sobel算子是把图像中每个像素的上下左右四领域的灰度值加权差,在边缘处达到极值从而检测边缘。
灰度化之后进行边缘检测。

1.1参数及其含义

Sobel( src, dst, ddepth, dx, dy, ksize, scale,delta,borderType )
1.InputArray src,
2.OutputArray dst,
3.int ddepth, 输出图像深度
4. int dx,导数x的阶数
5. int dy,导数y的阶数
6. int ksize = 1\3\5\7,扩展Sobel内核的大小;它必须是1、3、5或7。
7. double scale = 1, 计算派生值的可选比例因子;默认情况下,不应用缩放
8. double delta = 0, 给所选的像素值添加一个值delta。
9. int borderType = BORDER_DEFAULT 用于推断图像外部像素的某种边界模式。有默认值BORDER_DEFAULT。 ); Sobel(grayimg, gx, CV_16S, 1, 0,3,1,0,BORDER_DEFAULT); 后三位均可不填,输出默认值,即:Sobel(grayimg, gx, CV_16S, 1, 0,3) x方向和y方向:
在这里插入图片描述

1.2全部代码

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

using namespace std;
using namespace cv;

int main()
{
	Mat img, grayimg, gx, gy, gx1, gy1, src;

	img = imread("D:/images/lena.png");
	if (!img.data)
	{
		cout << "could not load image !";
		return -1;
	}
	imshow("原图", img);
	cvtColor(img, grayimg, COLOR_BGR2GRAY);
	imshow("灰度图", grayimg);
	//在X方向上求导,能获得竖直边缘
	Sobel(grayimg, gx, CV_16S, 1, 0, 3);
	//在Y方向上求导,能获得水平边缘
	Sobel(grayimg, gy, CV_16S, 0, 1, 3);
	//由于经过sobel算子操作后,像素可能会变成负值,要将所有像素值取绝对值
	convertScaleAbs(gx, gx1);//计算图像gx的像素绝对值,输出到图像gx1
	convertScaleAbs(gy, gy1);//计算图像gy的像素绝对值,输出到图像gy1
	//将X和Y方向增强的图像进行加权处理
	addWeighted(gx1, 0.5, gy1, 0.5, 0, src);
	imshow("|Sobelx|", gx1);
	imshow("|Sobely|", gy1);
	imshow("|Sobelx|+|Sobelx|", src);

	waitKey(0);
	return 0;
}

1.3效果展示

在这里插入图片描述
为了更形象地体现soble在x和y方向上的处理效果,选用一张有明显竖直线和水平线的图像。
X-清晰地显示竖直线
在这里插入图片描述
Y-清晰地显示水平线在这里插入图片描述
X+Y
竖直线和水平线合并
在这里插入图片描述

二、API-Scharr算子

Scharr算子是sobel的加强版
在这里插入图片描述

2.1全部代码

参数等同于soble算子

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

using namespace std;
using namespace cv;

int main()
{
	Mat img, grayimg, gx, gy, gx1, gy1, src;

	img = imread("D:/images/house.png");
	if (!img.data)
	{
		cout << "could not load image !";
		return -1;
	}
	imshow("原图", img);
	cvtColor(img, grayimg, COLOR_BGR2GRAY);
	imshow("灰度图", grayimg);
	Scharr(grayimg, gx, CV_16S, 1, 0, 1);
	Scharr(grayimg, gy, CV_16S, 0, 1, 1);
	//由于经过sobel算子操作后,像素可能会变成负值,要将所有像素值取绝对值
	convertScaleAbs(gx, gx1);//计算图像gx的像素绝对值,输出到图像gx1
	convertScaleAbs(gy, gy1);//计算图像gy的像素绝对值,输出到图像gy1
							 //将X和Y方向增强的图像进行加权处理
	addWeighted(gx1, 0.5, gy1, 0.5, 0, src);
	imshow("|Sobelx|", gx1);
	imshow("|Sobely|", gy1);
	imshow("|Sobelx|+|Sobelx|", src);

	waitKey(0);
	return 0;
}

2.2效果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、API-Laplacian算子

Laplacian即拉普拉斯算子

3.1参数以及含义

Laplacian( src, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT);
Laplacian(grayimg, gx, CV_16S, 3, 1, 0); src: 输入图像。
dst: 输出图像
ddepth: 输出图像的深度。 因为输入图像的深度是 CV_8U ,这里我们必须定义 ddepth = CV_16S 以避免外溢。
kernel_size: 内部调用的 Sobel算子的内核大小,此例中设置为3。 scale, delta 和
BORDER_DEFAULT: 使用默认值。

3.2全部代码

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

using namespace std;
using namespace cv;

int main()
{
	Mat img, grayimg, gx, gx1;

	img = imread("D:/images/house.png");
	if (!img.data)
	{
		cout << "could not load image !";
		return -1;
	}
	imshow("原图", img);
	cvtColor(img, grayimg, COLOR_BGR2GRAY);
	imshow("灰度图", grayimg);
	Laplacian(grayimg, gx, CV_16S, 3, 1, 0);
	//由于经过Laplacianl算子操作后,像素可能会变成负值,要将所有像素值取绝对值
	convertScaleAbs(gx, gx1);//计算图像gx的像素绝对值,输出到图像gx1
	
	imshow("result", gx1);

	waitKey(0);
	return 0;
}

3.3效果展示

在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

嵌小超

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

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

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

打赏作者

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

抵扣说明:

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

余额充值