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)

Python+OpenCV 图像边缘检测四种实现方法

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算子边缘检测')

Python+OpenCV 图像边缘检测四种实现方法

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算子边缘检测')

Python+OpenCV 图像边缘检测四种实现方法

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算子边缘检测')

Python+OpenCV 图像边缘检测四种实现方法

4.Canny边缘检测(被认为是最优的边缘检测算法)

Python+OpenCV 图像边缘检测四种实现方法

Python+OpenCV 图像边缘检测四种实现方法

Python+OpenCV 图像边缘检测四种实现方法

Python+OpenCV 图像边缘检测四种实现方法


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边缘检测')

Python+OpenCV 图像边缘检测四种实现方法

总结

Python+OpenCV 图像边缘检测四种实现方法

Python+OpenCV 图像边缘检测四种实现方法

来源:https://blog.csdn.net/weixin_51545953/article/details/121523545

标签:Python,OpenCV,边缘检测
0
投稿

猜你喜欢

  • python批量下载抖音视频

    2023-09-05 11:26:14
  • python使用imap-tools模块下载邮件附件的示例

    2023-09-16 08:39:38
  • Django利用cookie保存用户登录信息的简单实现方法

    2021-03-22 16:47:26
  • Python的Django框架实现数据库查询(不返回QuerySet的方法)

    2024-01-28 06:05:02
  • Go语言中你所不知道的位操作用法

    2024-05-29 22:07:00
  • GoLang 中的随机数的示例代码

    2024-04-26 17:16:51
  • 用户研究角度看设计(1)“复制链接”的故事

    2008-12-26 17:48:00
  • Vue.js性能优化N个技巧(值得收藏)

    2024-04-16 10:40:51
  • 详解python使用Nginx和uWSGI来运行Python应用

    2023-07-25 20:40:55
  • 一文带你了解Golang中的缓冲区Buffer

    2024-04-23 09:47:18
  • python GUI库图形界面开发之PyQt5 Qt Designer工具(Qt设计师)详细使用方法及Designer ui文件转py文件方法

    2023-05-17 00:32:46
  • 如何取得所有的Session变量

    2008-06-08 13:59:00
  • 彻底弄懂CSS盒子模式之三(浮动的表演和清除的自述)

    2007-05-11 16:52:00
  • SQL查询语句求出用户的连续登陆天数

    2024-01-17 22:00:40
  • MySQL 常用引擎总结分享

    2024-01-27 04:05:28
  • Mysql中explain作用详解

    2024-01-28 11:40:46
  • electron-vue利用webpack打包实现多页面的入口文件问题

    2024-05-13 10:39:51
  • 用js+cookie记录滚动条位置

    2024-06-05 09:11:02
  • Python时间戳使用和相互转换详解

    2023-01-22 08:14:28
  • Pandas中DataFrame数据删除详情

    2023-12-18 10:40:02
  • asp之家 网络编程 m.aspxhome.com