坐看云起时|

一枚码农

园龄:7年6个月粉丝:5关注:1

opencv-python 4.7. Canny边缘检测

理论

Canny边缘检测是一种流行的边缘检测算法,它是由John F. Canny开发的。

  1. 这是一个多阶段算法,我们将了解其中的每个阶段。

  2. 降噪
    由于边缘检测易受图像中的噪声影响,因此第一步是使用5x5高斯滤波器去除图像中的噪声。我们在之前的章节中已经看到了这一点。

  3. 计算图像的强度梯度
    然后在水平和垂直方向上用Sobel核对平滑后的图像进行滤波,以获得水平方向($$ G_{x} $$)和垂直方向($$ G_{y} $$)的一阶导数。从这两个图像中,我们可以找到每个像素的边缘梯度和方向,如下所示:
    $$ Edge_Gradient\left ( G \right )= \sqrt{G_{x}^{2}+G_{y}^{2}} $$
    $$ Angle\left ( \theta \right )= tan^{-1}\left ( \frac{G_{y}}{G_{x}} \right ) $$
    渐变方向始终垂直于边缘。梯度方向被归为四类:垂直,水平,和两个对角线。

  4. 非极大值抑制
    在获得梯度的大小和方向之后,完成图像的全扫描以去除可能不构成边缘的任何不需要的像素。为此,在每个像素处,检查像素是否是其在梯度方向上的邻域中的局部最大值。检查下图:
    image
    A点位于边缘(垂直方向)。渐变方向与边缘垂直。B点和C点处于梯度方向。因此,用点B和C检查点A,看它是否形成局部最大值。如果是这样,则考虑下一阶段,否则,它被抑制(置零)。
    简而言之,你得到的结果是具有“细边”的二进制图像。

  5. 滞后阈值
    这个阶段决定哪些边缘都是边缘,哪些边缘不是边缘。为此,我们需要两个阈值,minVal和maxVal。强度梯度大于maxVal的任何边缘肯定是边缘,而minVal以下的边缘肯定是非边缘的,因此被丢弃。位于这两个阈值之间的人是基于其连通性的分类边缘或非边缘。如果它们连接到“可靠边缘”像素,则它们被视为边缘的一部分。否则,他们也被丢弃。见下图:
    image
    边缘A高于maxVal,因此被视为“确定边缘”。虽然边C低于maxVal,但它连接到边A,因此也被视为有效边,我们得到完整的曲线。但边缘B虽然高于minVal并且与边缘C的区域相同,但它没有连接到任何“可靠边缘”,因此被丢弃。所以我们必须相应地选择minVal和maxVal才能获得正确的结果。 假设边是长线,这个阶段也会消除小像素噪声。 所以我们最终得到的是图像中的强边缘。

OpenCV中的Canny边缘检测

OpenCV将以上所有步骤放在单个函数cv.Canny()中。我们将看到如何使用它。第一个参数是我们的输入图像。第二个和第三个参数分别是我们的minVal和maxVal。第三个参数是aperture_size,它是用于查找图像渐变的Sobel卷积核的大小。默认情况下它是3。最后一个参数是L2gradient,它指定用于查找梯度幅度的等式。如果它是True,它使用上面提到的更准确的等式,否则它使用这个函数:$$ Edge_Gradient\left ( G \right )= \left | G_{x} \right |+\left | G_{y} \right | $$
默认情况下,它为False。

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread(r'C:\Users\yuyalong\Pictures\Saved Pictures\opera.jpg', 0)

# L2gradient=False
edges = cv.Canny(img, 100, 200)

# L2gradient=True
edges1 = cv.Canny(img, 100, 200, L2gradient=True)

plt.subplot(1, 3, 1), plt.imshow(img, cmap='gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 2), plt.imshow(edges, cmap='gray')
plt.title('edges-(L2gradient=False)'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 3), plt.imshow(edges1, cmap='gray')
plt.title('edges-(L2gradient=True)'), plt.xticks([]), plt.yticks([])

plt.show()

image

本文作者:一枚码农

本文链接:https://www.cnblogs.com/yimeimanong/p/17272852.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   一枚码农  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
 
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Sold Out Hawk
  2. 2 光辉岁月 Beyond
Sold Out - Hawk
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Jon Steingard

作曲 : Jon Steingard

I ain't like no one you met before

I'm running for the front

When they're all running for the door

And I won't sit down won't back out

You can't ever shut me up

Cause I'm on a mission

And I won't quit now

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Because the battle's already been won

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

This ain't just some temporary phase

You can't face this kind of grace

And leave the way you came

This is permanent with intent

And there won't be no stopping it now

I'm on a mission and it's heaven sent

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Cause my soul is like a stadium

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to shout

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

No trials coming against me

Could put a dent in my passion

They're just an opportunity

To put my faith into action

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

I ain't got nothing left to be afraid of

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out