python与openCV对图像处理的canny算子实现源码分析,单个图片123

在这里插入图片描述

# coding=utf-8
import cv2
import numpy as np

imput0="W:/PY/WDPY/ceshi/sswhd (5).jpg"
img = cv2.imread( imput0, 0 )

img = cv2.GaussianBlur( img, (3, 3), 0 )
#高斯平滑处理原图像降噪
canny = cv2.Canny( img, 20, 100 )
#canny算子处理调用Canny函数,指定阈值
cv2.imshow( 'Canny', canny )
cv2.waitKey( 0 )
cv2.destroyAllWindows()

另外可以自定义函数,不过运行CannyAlogrithm(img,30,50)报错,出现不知道什么问题,。嘿嘿,未完待续

def CannyAlogrithm(image,MINThreshold,MAXThreshold):
    image = GaussianSmooth(image)
    Gx = SobelAlogrithm(image,"horizontal")
    Gy = SobelAlogrithm(image,"vertical")
    G = np.sqrt(np.square(Gx.astype(np.float64))+np.square(Gy.astype(np.float64)))
    G = G*(255/np.max(G)).astype(np.uint8)
    cita = np.arctan2(Gy.astype(np.float64),Gx.astype(np.float64))
    nms_image = NonmaximumSuppression(G,cita)
    nms_image = (nms_image*(255/np.max(nms_image))).astype(np.uint8)
    usemap = np.zeros(nms_image.shape)
    high_list = []
    for i in range(nms_image.shape[0]):
        for j in range(nms_image.shape[1]):
            if nms_image[i,j]>MAXThreshold:
                high_list.append((i,j))
                
    direct = [(0,1),(1,1),(-1,1),(-1,-1),(1,-1),(1,0),(-1,0),(0,-1)]
    def DFS(stepmap,start):
        route = [start]
        while route:
            now = route.pop()
            if usemap[now] == 1:
                break
            usemap[now] = 1
            for dic in direct:
                next_coodinate = (now[0]+dic[0],now[1]+dic[1])
                if next_coodinate[0]<stepmap.shape[0]-1 and next_coodinate[0]>=0 \
                and next_coodinate[1]<stepmap.shape[1]-1 and next_coodinate[1]>=0 \
                and not usemap[next_coodinate] and nms_image[next_coodinate]>MINThreshold:
                    route.append(next_coodinate)
    for i in high_list:
        DFS(nms_image,i)
    return nms_image*usemap
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海宝7号

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

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

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

打赏作者

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

抵扣说明:

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

余额充值