【图像处理】python实现图像锐化边缘检测算子(Robert、Sobel、Prewitt、Laplacian算子)

一、Robert算子

罗伯特梯度法(Robert Gradient), 是一种交叉差分方法。其数学表达式可近似为:

G[f(x, y)] ≈|f(i, j)-f(i+1, j+1) |+|f(i+1, j)-f(i, j+1)|

################################################
#           Robert算子
################################################
def robert_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)
    for i in range(1, h-1):
        for j in range(1, w-1):
            image_new[i][j] = np.abs((image[i][j]-image[i+1][j+1])) + np.abs(image[i+1][j]-image[i][j+1])
    return image_new

二、Sobel算子

采用梯度微分锐化图像,同时会使噪声、条纹等得到增强, Sobel算子则在一定程度上克服了这个问题。Sobel算子法的基本原理是:计算3×3窗口的灰度, 将其作为变换后图像g(i, j)的灰度。公式如下:

################################################
#           Sobel算子
################################################
def sobel_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)

    for i in range(1, h-1):
        for j in range(1, w-1):
            sx = (image[i + 1][j - 1] + 2 * image[i + 1][j] + image[i + 1][j + 1]) - \
                 (image[i - 1][j - 1] + 2 * image[i - 1][j] + image[i - 1][j + 1])
            sy = (image[i - 1][j + 1] + 2 * image[i][j + 1] + image[i + 1][j + 1]) - \
                 (image[i - 1][j - 1] + 2 * image[i][j - 1] + image[i + 1][j - 1])
            image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
    return image_new

三、Prewitt算子

与Sobel相比,Prewitt算子有一定的抗干扰性,图像效果比较干净。

公式如下:

################################################
#           Prewitt算子
################################################
def prewitt_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)

    for i in range(1, h-1):
        for j in range(1, w-1):
            sx = (image[i - 1][j - 1] + image[i - 1][j] + image[i - 1][j + 1]) - \
                 (image[i + 1][j - 1] + image[i + 1][j] + image[i + 1][j + 1])
            sy = (image[i - 1][j - 1] + image[i][j - 1] + image[i + 1][j - 1]) - \
                 (image[i - 1][j + 1] + image[i][j + 1] + image[i + 1][j + 1])
            image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
    return image_new

四、Laplacian算子

拉普拉斯运算是偏导数运算的线性组合运算,属于二阶微分运算。与以上三类一阶微分运算相比,Laplacian算子获得的边界更为细致,包含了更多信息,

公式如下:

 

################################################
#           Laplacian算子
################################################
def laplacian_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)
    for i in range(1, h-1):
        for j in range(1, w-1):
            image_new[i][j] = image[i + 1][j] + image[i - 1][j] + image[i][j + 1] + image[i][j - 1] - 8 * image[i][j]
    return image_new

五、完整代码

import numpy as np
import matplotlib.pyplot as plt
import random


################################################
#           Robert算子
################################################
def robert_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)
    for i in range(1, h-1):
        for j in range(1, w-1):
            image_new[i][j] = np.abs((image[i][j]-image[i+1][j+1])) + np.abs(image[i+1][j]-image[i][j+1])
    return image_new


################################################
#           Sobel算子
################################################
def sobel_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)

    for i in range(1, h-1):
        for j in range(1, w-1):
            sx = (image[i + 1][j - 1] + 2 * image[i + 1][j] + image[i + 1][j + 1]) - \
                 (image[i - 1][j - 1] + 2 * image[i - 1][j] + image[i - 1][j + 1])
            sy = (image[i - 1][j + 1] + 2 * image[i][j + 1] + image[i + 1][j + 1]) - \
                 (image[i - 1][j - 1] + 2 * image[i][j - 1] + image[i + 1][j - 1])
            image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
    return image_new


################################################
#           Prewitt算子
################################################
def prewitt_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)

    for i in range(1, h-1):
        for j in range(1, w-1):
            sx = (image[i - 1][j - 1] + image[i - 1][j] + image[i - 1][j + 1]) - \
                 (image[i + 1][j - 1] + image[i + 1][j] + image[i + 1][j + 1])
            sy = (image[i - 1][j - 1] + image[i][j - 1] + image[i + 1][j - 1]) - \
                 (image[i - 1][j + 1] + image[i][j + 1] + image[i + 1][j + 1])
            image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
    return image_new


################################################
#           Laplacian算子
################################################
def laplacian_filter(image):
    h = image.shape[0]
    w = image.shape[1]
    image_new = np.zeros(image.shape, np.uint8)
    for i in range(1, h-1):
        for j in range(1, w-1):
            image_new[i][j] = image[i + 1][j] + image[i - 1][j] + image[i][j + 1] + image[i][j - 1] - 8 * image[i][j]
    return image_new

#############################################################################


if __name__ == "__main__":
    img = plt.imread("1.jpg")

    rgb_weight = [0.299, 0.587, 0.114]
    img_gray = np.dot(img, rgb_weight)

################################################
#           原图
################################################
    plt.subplot(241)
    plt.imshow(img)
    plt.xticks([])
    plt.yticks([])
    plt.title("Original")

################################################
#           灰度图
################################################
    plt.subplot(242)
    plt.imshow(img_gray, cmap=plt.cm.gray)
    plt.xticks([])
    plt.yticks([])
    plt.title("Gray")

################################################
#           Robert算子
################################################
    img_Robert = robert_filter(img_gray)
    img_Robert = img_Robert.astype(np.float64)
    plt.subplot(245)
    plt.imshow(img_Robert, cmap=plt.cm.gray)
    plt.xticks([])
    plt.yticks([])
    plt.title("robert_filter")

################################################
#           Sobel算子
################################################
    img_Sobel = sobel_filter(img_gray)
    img_Sobel = img_Sobel.astype(np.float64)
    plt.subplot(246)
    plt.imshow(img_Sobel, cmap=plt.cm.gray)
    plt.xticks([])
    plt.yticks([])
    plt.title("sobel_filter")

################################################
#           Prewitt算子
################################################
    img_Prewitt = prewitt_filter(img_gray)
    img_Prewitt = img_Prewitt.astype(np.float64)
    plt.subplot(247)
    plt.imshow(img_Prewitt, cmap=plt.cm.gray)
    plt.xticks([])
    plt.yticks([])
    plt.title("prewitt_filter")

################################################
#           Laplacian算子
################################################
    img_Laplacian = laplacian_filter(img_gray)
    img_Laplacian = img_Laplacian.astype(np.float64)
    plt.subplot(248)
    plt.imshow(img_Laplacian, cmap=plt.cm.gray)
    plt.xticks([])
    plt.yticks([])
    plt.title("laplacian_filter")
    plt.show()


结果如下:

  • 17
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

McGregorWwww

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

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

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

打赏作者

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

抵扣说明:

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

余额充值