图像边缘检测


前言

  有了图像放大缩小,图像灰度化处理等相关基础知识过后,就可以进行图像边缘检测了。边缘检测最后也会在FPGA上面实现,此处小编已经控制不住要剧透了。也是一样,先从软件的角度来理解这些图像边缘检测算法。


一、图像边缘检测

  边缘检测原理如下动态图所示。假如你有一些别人发明的算子,算子在第二章介绍。使用算子在原图上进行扫描,算子中的值乘以对应的像素值,然后加起来就行了。你可以使用截图工具,截取动态图,计算一下是否正确。
在这里插入图片描述

二、边缘检测算子

  算子其实就是滤波器,在深度学习里面又叫卷积,下面3种算子给出了具体的值,而在卷积神经网络里面,卷积核的值是需要训练得到。

1. Roberts算子

G x = [ 1 0 0 − 1 ] G y = [ 0 − 1 1 0 ] G_x = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} Gx=[1001]Gy=[0110]

2. Prewitt算子

G x = [ − 1 0 1 − 1 0 1 − 1 0 1 ] G y = [ − 1 − 1 − 1 0 0 0 1 1 1 ] G_x = \begin{bmatrix} -1 & 0 & 1\\ -1 & 0 & 1\\ -1 & 0 & 1 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} -1 & -1 & -1\\ 0 & 0 & 0\\ 1 & 1 & 1 \end{bmatrix} Gx= 111000111 Gy= 101101101

3. Sobel算子

G x = [ − 1 0 + 1 − 2 0 + 2 − 1 0 + 1 ] G y = [ + 1 + 2 + 1 0 0 0 − 1 − 2 1 ] G_x = \begin{bmatrix} -1 & 0 & +1\\ -2 & 0 & +2\\ -1 & 0 & +1 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} +1 & +2 & +1\\ 0 & 0 & 0\\ -1 & -2 & 1 \end{bmatrix} Gx= 121000+1+2+1 Gy= +101+202+101


三、代码实现

# robert算子
robert_x = np.array([[1, 0],
                    [0, -1]])
robert_y = np.array([[0, -1],
                    [1, 0]])
# prewitt算子              
prewitt_x = np.array([[-1, 0, 1],
                     [-1, 0, 1],
                     [-1, 0, 1]])
prewitt_y = np.array([[1, 1, 1],
                     [0, 0, 0],
                     [-1, -1, -1]])
# sobel算子          
sobel_x = np.array([[-1, 0, +1],
                    [-2, 0, +2],
                    [-1, 0, +1]])
sobel_y = np.array([[+1, +2, +1],
                    [0, 0, 0],
                   [-1, -2, -1]])
# 图像灰度处理                   
def weight_gray(image):
    weight_image = image[:, :, 0] * 0.11 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.3
    weight_image = weight_image.astype(np.uint8)
    return weight_image   
# 图像边缘检测                  
def edge_dimage(image, operator, threshold):
    shape = image.shape
    h, w = shape
    sh, sw = operator[0].shape
    sobel_image = np.zeros(image.shape)
    for i in range(h - sh):
        for j in range(w - sw):
            ix = np.multiply(image[i: i + sh, j: j + sw], operator[0])
            iy = np.multiply(image[i: i + sh, j: j + sw], operator[1])
            ix = np.sum(ix)
            iy = np.sum(iy)
            ig = np.sqrt(ix ** 2 + iy ** 2) 
            if ig > threshold:
            	sobel_image[i, j] = 255
            else:
            	sobel_image[i, j] = 0
    sobel_image = sobel_image.astype(np.uint8)
    return sobel_image
    
image = cv2.imread("three_body.jpg")    
gray = weight_gray(image)
roimage = edge_dimage(gray, (robert_x, robert_y), 32)  
primage = edge_dimage(gray, (prewitt_x, prewitt_y), 32)  
sbimage = edge_dimage(gray, (sobel_x, sobel_y), 32)
# 画子图
plt.figure(figsize=(10, 7))
plt.subplot(221)
plt.title("gray")
plt.imshow(gray, cmap='gray')
plt.subplot(222)
plt.title("sobel")
plt.imshow(sbimage, cmap='gray')
plt.subplot(223)
plt.title("roberts")
plt.imshow(roimage, cmap='gray')
plt.subplot(224)
plt.title("prewitt")
plt.imshow(primage, cmap='gray')

  threshold=32

在这里插入图片描述
  threshold=64

在这里插入图片描述
  threshold=96

在这里插入图片描述

总结

  这大概就是卷积神经网络的由来,以前叫做算子,现在叫做卷积。小编也迫不及待的想要动手实现卷积神经网络了(numpy),敬请期待。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值