【图像处理】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()


结果如下:

读取图像"barbara.nmp"通常需要使用计算机视觉或图像处理库,比如Python的OpenCV或者MATLAB等。图像锐化边缘处理涉及到对图像进行增强边缘特征的操作,主要有以下几种滤波器: 1. **拉普拉斯算子** (Laplacian): 这是一种二阶微分算子,通过计算图像像素点周围梯度的平方和,可以突出图像的边缘和变化区域。在OpenCV中,`cv2.Laplacian(image, cv2.CV_64F)`可以用于应用此操作。 2. **罗伯特算子** (Roberts Cross): 由两个互相垂直的1x1的核组成,分别检测水平和垂直方向的变化,简单易计算,常用于初学者教程中。 3. **Prewitt算子**: 类似于罗伯茨算子,也是由两个小的1x1矩阵构成,但是它们的方向更倾斜,对于检测斜向边缘更有优势。 4. **Sobel算子**: 是一种二维卷积算子,包括水平、垂直以及两个对角线方向的一阶导数估计,因此能较好地捕捉边缘信息。在OpenCV中,`cv2.Sobel(image, cv2.CV_64F, dx=1, dy=0)`用于计算水平 Sobel,`dy=1`用于垂直方向。 要实际应用这些算法,你需要按照以下步骤操作: 1. 导入必要的库函数图像数据。 2. 加载图像 `image = cv2.imread('barbara.nmp', 0)` (假设为灰度图像)。 3. 应用滤波器并保存结果到新的变量。 4. 可选地,可以调整阈值来增强边缘效果,并进行非极大值抑制等后续处理。 ```python import cv2 # 读取图像 img = cv2.imread('barbara.nmp', cv2.IMREAD_GRAYSCALE) # 拉普拉斯算子 laplacian_img = cv2.Laplacian(img, cv2.CV_64F) # 罗伯特算子 kernel = [[1, -1], [-1, 1]] roberts_img = cv2.filter2D(img, -1, kernel) # Prewitt算子 prewitt_horizontal = cv2.Prewitt(img, cv2.CV_64F) prewitt_vertical = cv2.Prewitt(img, cv2.CV_64F, dx=0, dy=1) prewitt_img = prewitt_horizontal + prewitt_vertical # Sobel算子 sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0) sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1) sobel_img = sobel_x + sobel_y # 后续处理和显示结果 # ... ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

McGregorWwww

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

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

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

打赏作者

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

抵扣说明:

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

余额充值