[Python图像处理] 十七.图像锐化与边缘检测之Roberts算子、Prewitt算子、Sobel算子和Laplacian算子

该系列文章是讲解Python OpenCV图像处理知识,前期主要讲解图像入门、OpenCV基础用法,中期讲解图像处理的各种算法,包括图像锐化算子、图像增强技术、图像分割等,后期结合深度学习研究图像识别、图像分类应用。希望文章对您有所帮助,如果有不足之处,还请海涵~

由于收集图像数据的器件或传输数图像的通道的存在一些质量缺陷,文物图像时间久远,或者受一些其他外界因素、动态不稳定抓取图像的影响,使得图像存在模糊和有噪声的情况,从而影响到图像识别工作的开展。这时需要开展图像锐化和边缘检测处理,加强原图像的高频部分,锐化突出图像的边缘细节,改善图像的对比度,使模糊的图像变得更清晰。

图像锐化和边缘提取技术可以消除图像中的噪声,提取图像信息中用来表征图像的一些变量,为图像识别提供基础。通常使用灰度差分法对图像的边缘、轮廓进行处理,将其凸显。本文分别采用Laplacian算子、Robert算子、Prewitt算子和Sobel算子进行图像锐化边缘处理实验。本文主要讲解灰度线性变换,基础性知识希望对您有所帮助。

该系列在github所有源代码:

  • 64
    点赞
  • 225
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 25
    评论
以下是对应算子的简单示例代码: Roberts算子: ```python from scipy import ndimage import numpy as np def robert_cross(image): roberts_cross_v = np.array( [[-1,0],[0,1]], dtype=np.int32 ) roberts_cross_h = np.array( [[0,-1],[1,0]], dtype=np.int32 ) vertical = ndimage.convolve( image, roberts_cross_v ) horizontal = ndimage.convolve( image, roberts_cross_h ) return np.sqrt( np.square(horizontal) + np.square(vertical) ) ``` Prewitt算子: ```python from scipy import ndimage import numpy as np def prewitt(image): vert_prewitt = np.array([[-1,-1,-1],[0,0,0],[1,1,1]]) hori_prewitt = np.array([[-1,0,1],[-1,0,1],[-1,0,1]]) prewitt_vert = ndimage.convolve(image, vert_prewitt) prewitt_hori = ndimage.convolve(image, hori_prewitt) return np.sqrt( np.square(prewitt_vert) + np.square(prewitt_hori) ) ``` Sobel算子: ```python from scipy import ndimage import numpy as np def sobel(image): vert_sobel = np.array([[-1,-2,-1],[0,0,0],[1,2,1]]) hori_sobel = np.array([[-1,0,1],[-2,0,2],[-1,0,1]]) sobel_vert = ndimage.convolve(image, vert_sobel) sobel_hori = ndimage.convolve(image, hori_sobel) return np.sqrt( np.square(sobel_vert) + np.square(sobel_hori) ) ``` Laplacian算子: ```python from scipy import ndimage import numpy as np def laplacian(image): laplacian_kernal = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) filtered_img = ndimage.convolve(image, laplacian_kernal) return filtered_img ``` Canny算子: ```python from scipy import ndimage import numpy as np def canny(image, low_threshold=0, high_threshold=255): img_smooth = ndimage.gaussian_filter(image, sigma=1.4) img_x = ndimage.sobel(img_smooth, axis=0) img_y = ndimage.sobel(img_smooth, axis=1) gradient_magnitude = np.hypot(img_x, img_y) gradient_direction = np.arctan2(img_y, img_x) gradient_direction = np.rad2deg(gradient_direction) gradient_direction[gradient_direction < 0] += 180 max_magnitude = np.max(gradient_magnitude) if high_threshold > max_magnitude: high_threshold = max_magnitude if low_threshold > high_threshold: low_threshold, high_threshold = high_threshold, low_threshold row, col = image.shape canny_img = np.zeros((row, col), dtype=np.uint8) weak = np.int32(50) strong = np.int32(255) strong_i, strong_j = np.where(gradient_magnitude >= high_threshold) zeros_i, zeros_j = np.where(gradient_magnitude < low_threshold) weak_i, weak_j = np.where((gradient_magnitude <= high_threshold) & (gradient_magnitude >= low_threshold)) canny_img[strong_i, strong_j] = strong canny_img[weak_i, weak_j] = weak for i in range(1, row-1): for j in range(1, col-1): if canny_img[i,j] == weak: if (canny_img[i+1, j-1] == strong) or (canny_img[i+1, j] == strong) or (canny_img[i+1, j+1] == strong) \ or (canny_img[i, j-1] == strong) or (canny_img[i, j+1] == strong) \ or (canny_img[i-1, j-1] == strong) or (canny_img[i-1, j] == strong) or (canny_img[i-1, j+1] == strong): canny_img[i, j] = strong else: canny_img[i, j] = 0 return canny_img ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Eastmount

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

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

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

打赏作者

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

抵扣说明:

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

余额充值