Python+OpenCV 图像边缘检测四种实现方法
作者:newname 时间:2022-06-08 02:40:44
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# 设置兼容中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
D:\Anaconda\AZWZ\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll
warnings.warn("loaded more than 1 DLL from .libs:\n%s" %
horse = cv.imread('img/horse.jpg',0)
plt.imshow(horse,cmap=plt.cm.gray)
plt.imshow(horse,cmap=plt.cm.gray)
1.Sobel算子
# 1,0 代表沿x方向做sobel算子
x = cv.Sobel(horse,cv.CV_16S,1,0)
# 0,1 代表沿y方向做sobel算子
y = cv.Sobel(horse,cv.CV_16S,0,1)
# 格式转换
absx = cv.convertScaleAbs(x)
absy = cv.convertScaleAbs(y)
# 边缘检测结果
res = cv.addWeighted(absx,0.5,absy,0.5,0)
plt.figure(figsize=(20,20))
plt.subplot(1,2,1)
m1 = plt.imshow(horse,cmap=plt.cm.gray)
plt.title("原图")
plt.subplot(1,2,2)
m2 = plt.imshow(res,cmap=plt.cm.gray)
plt.title("Sobel算子边缘检测")
Text(0.5, 1.0, 'Sobel算子边缘检测')
2.Schaar算子(更能体现细节)
# 1,0 代表沿x方向做sobel算子
x = cv.Sobel(horse,cv.CV_16S,1,0,ksize=-1)
# 0,1 代表沿y方向做sobel算子
y = cv.Sobel(horse,cv.CV_16S,0,1,ksize=-1)
# 格式转换
absx = cv.convertScaleAbs(x)
absy = cv.convertScaleAbs(y)
# 边缘检测结果
res = cv.addWeighted(absx,0.5,absy,0.5,0)
plt.figure(figsize=(20,20))
plt.subplot(1,2,1)
m1 = plt.imshow(horse,cmap=plt.cm.gray)
plt.title("原图")
plt.subplot(1,2,2)
m2 = plt.imshow(res,cmap=plt.cm.gray)
plt.title("Schaar算子边缘检测")
Text(0.5, 1.0, 'Schaar算子边缘检测')
3.Laplacian算子(基于零穿越的,二阶导数的0值点)
res = cv.Laplacian(horse,cv.CV_16S)
res = cv.convertScaleAbs(res)
plt.figure(figsize=(20,20))
plt.subplot(1,2,1)
m1 = plt.imshow(horse,cmap=plt.cm.gray)
plt.title("原图")
plt.subplot(1,2,2)
m2 = plt.imshow(res,cmap=plt.cm.gray)
plt.title("Laplacian算子边缘检测")
Text(0.5, 1.0, 'Laplacian算子边缘检测')
4.Canny边缘检测(被认为是最优的边缘检测算法)
res = cv.Canny(horse,0,100)
# res = cv.convertScaleAbs(res) Canny边缘检测是一种二值检测,不需要转换格式这一个步骤
plt.figure(figsize=(20,20))
plt.subplot(1,2,1)
m1 = plt.imshow(horse,cmap=plt.cm.gray)
plt.title("原图")
plt.subplot(1,2,2)
m2 = plt.imshow(res,cmap=plt.cm.gray)
plt.title("Canny边缘检测")
Text(0.5, 1.0, 'Canny边缘检测')
总结
来源:https://blog.csdn.net/weixin_51545953/article/details/121523545
标签:Python,OpenCV,边缘检测
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用 OpenCV 进行图像投影变换
2021-09-10 03:08:19
![](https://img.aspxhome.com/file/2023/6/127656_0s.png)
Python Vaex实现快速分析100G大数据量
2021-05-24 08:48:58
![](https://img.aspxhome.com/file/2023/8/80538_0s.png)
Python数据结构与算法之图的广度优先与深度优先搜索算法示例
2022-05-05 07:34:53
Python实例一个类背后发生了什么
2023-05-19 05:44:18
Python日志处理模块logging用法解析
2021-01-05 14:45:55
python爬虫之request模块深入讲解
2021-09-21 00:35:51
![](https://img.aspxhome.com/file/2023/7/118397_0s.png)
python实现超市商品销售管理系统
2021-01-06 22:18:14
![](https://img.aspxhome.com/file/2023/9/93529_0s.jpg)
Python编写nmap扫描工具
2021-08-07 14:53:10
Python中itertools的用法详解
2022-06-05 13:34:52
解决Python 中JSONDecodeError: Expecting value: line 1 column 1 (char 0)错误
2023-10-30 22:42:03
![](https://img.aspxhome.com/file/2023/6/71426_0s.png)
python聊天程序实例代码分享
2021-07-09 13:03:33
Linux安装Python3如何和系统自带的Python2并存
2023-08-25 03:42:09
Python os模块常用方法和属性总结
2021-05-06 13:46:12
pyspark给dataframe增加新的一列的实现示例
2022-06-13 20:00:19
PHP使用redis实现分布式锁的示例详解
2023-06-01 16:32:19
Pycharm配置opencv与numpy的实现
2021-09-19 08:20:55
![](https://img.aspxhome.com/file/2023/4/114654_0s.jpg)
使用Python AIML搭建聊天机器人的方法示例
2022-01-04 10:14:28
使用python-cv2实现视频的分解与合成的示例代码
2021-06-21 11:51:46
对Django中static(静态)文件详解以及{% static %}标签的使用方法
2021-03-27 20:28:21
pandas string转dataframe的方法
2021-08-15 14:47:04