本文介绍了图像边缘检测的原理,指出边缘是灰度剧变处,检测基于方向导数掩码卷积。实现了通用边缘检测算子EdgeOP,集成Roberts、Prewitt等多种算子,提供四种边缘强度计算方式,并通过测试函数对比效果,展示了不同算子和计算方法的检测结果。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

引入
图像的边缘指的是灰度值发生急剧变化的位置。
在图像形成过程中,由于亮度、纹理、颜色、阴影等物理因素的不同而导致图像灰度值发生突变,从而形成边缘。
边缘是通过检查每个像素的邻域并对其灰度变化进行量化的,这种灰度变化的量化相当于微积分里连续函数中方向导数或者离散数列的差分。
算法原理
传统的边缘检测大多数是通过基于方向导数掩码(梯度方向导数)求卷积的方法。
计算灰度变化的卷积算子包含Roberts算子、Prewitt算子、Sobel算子、Scharr算子、Kirsch算子、Robinson算子、Laplacian算子。
大多数边缘检测算子是基于方向差分卷积核求卷积的方法,在使用由两个或者多个卷积核组成的边缘检测算子时假设有 n 个卷积核,记 Conv1,Conv2,…,ConvnConv1,Conv2,…,Convn,为图像分别与个卷积核做卷积的结果,通常有四种方式来衡量最后输出的边缘强度。
取对应位置绝对值的和:∑i=1n∣convi∣∑i=1n∣convi∣
取对应位置平方和的开方:∑i=1nconvi2∑i=1nconvi2
取对应位置绝对值的最大值:max{∣conv1∣,∣conv2∣,…,∣convi∣}max{∣conv1∣,∣conv2∣,…,∣convi∣}
插值法:∑i=1nai∣convi∣∑i=1nai∣convi∣,其中 ai>=0ai>=0,且 ∑i=1nai=1∑i=1nai=1
代码实现
构建通用的边缘检测算子
因为上述的这些算子在本质上都是通过卷积计算实现的,只是所使用到的卷积核参数有所不同所以可以构建一个通用的计算算子,只需要传入对应的卷积核参数即可实现不同的边缘检测并且在后处理时集成了上述的四种计算最终边缘强度的方式In [1]
import numpy as npimport paddleimport paddle.nn as nnclass EdgeOP(nn.Layer): def __init__(self, kernel): ''' kernel: shape(out_channels, in_channels, h, w) ''' super(EdgeOP, self).__init__() out_channels, in_channels, h, w = kernel.shape self.filter = nn.Conv2D(in_channels=in_channels, out_channels=out_channels, kernel_size=(h, w), padding='SAME', bias_attr=False) self.filter.weight.set_value(kernel.astype('float32')) @staticmethod def postprocess(outputs, mode=0, weight=None): ''' Input: NCHW Output: NHW(mode==1-3) or NCHW(mode==4) Params: mode: switch output mode(0-4) weight: weight when mode==3 ''' if mode==0: results = paddle.sum(paddle.abs(outputs), axis=1) elif mode==1: results = paddle.sqrt(paddle.sum(paddle.pow(outputs, 2), axis=1)) elif mode==2: results = paddle.max(paddle.abs(outputs), axis=1) elif mode==3: if weight is None: C = outputs.shape[1] weight = paddle.to_tensor([1/C] * C, dtype='float32') else: weight = paddle.to_tensor(weight, dtype='float32') results = paddle.einsum('nchw, c -> nhw', paddle.abs(outputs), weight) elif mode==4: results = paddle.abs(outputs) return paddle.clip(results, 0, 255).cast('uint8') @paddle.no_grad() def forward(self, images, mode=0, weight=None): outputs = self.filter(images) return self.postprocess(outputs, mode, weight)
图像边缘检测测试函数
为了方便测试就构建了如下的测试函数,测试同一张图片不同算子/不同边缘强度计算方法的边缘检测效果In [2]
import osimport cv2from PIL import Imagedef test_edge_det(kernel, img_path='test.jpg'): img = cv2.imread(img_path, 0) img_tensor = paddle.to_tensor(img, dtype='float32')[None, None, ...] op = EdgeOP(kernel) all_results = [] for mode in range(4): results = op(img_tensor, mode=mode) all_results.append(results.numpy()[0]) results = op(img_tensor, mode=4) for result in results.numpy()[0]: all_results.append(result) return all_results, np.concatenate(all_results, 1)
Roberts 算子

In [3]
roberts_kernel = np.array([ [[ [1, 0], [0, -1] ]], [[ [0, -1], [1, 0] ]]])_, concat_res = test_edge_det(roberts_kernel)Image.fromarray(concat_res)
Prewitt 算子

In [4]
prewitt_kernel = np.array([ [[ [-1, -1, -1], [ 0, 0, 0], [ 1, 1, 1] ]], [[ [-1, 0, 1], [-1, 0, 1], [-1, 0, 1] ]], [[ [ 0, 1, 1], [-1, 0, 1], [-1, -1, 0] ]], [[ [ -1, -1, 0], [ -1, 0, 1], [ 0, 1, 1] ]]])_, concat_res = test_edge_det(prewitt_kernel)Image.fromarray(concat_res)
Sobel 算子

In [5]
sobel_kernel = np.array([ [[ [-1, -2, -1], [ 0, 0, 0], [ 1, 2, 1] ]], [[ [-1, 0, 1], [-2, 0, 2], [-1, 0, 1] ]], [[ [ 0, 1, 2], [-1, 0, 1], [-2, -1, 0] ]], [[ [ -2, -1, 0], [ -1, 0, 1], [ 0, 1, 2] ]]])_, concat_res = test_edge_det(sobel_kernel)Image.fromarray(concat_res)
Scharr 算子

In [6]
scharr_kernel = np.array([ [[ [-3, -10, -3], [ 0, 0, 0], [ 3, 10, 3] ]], [[ [-3, 0, 3], [-10, 0, 10], [-3, 0, 3] ]], [[ [ 0, 3, 10], [-3, 0, 3], [-10, -3, 0] ]], [[ [ -10, -3, 0], [ -3, 0, 3], [ 0, 3, 10] ]]])_, concat_res = test_edge_det(scharr_kernel)Image.fromarray(concat_res)
Krisch 算子

In [7]
Krisch_kernel = np.array([ [[ [5, 5, 5], [-3,0,-3], [-3,-3,-3] ]], [[ [-3, 5,5], [-3,0,5], [-3,-3,-3] ]], [[ [-3,-3,5], [-3,0,5], [-3,-3,5] ]], [[ [-3,-3,-3], [-3,0,5], [-3,5,5] ]], [[ [-3, -3, -3], [-3,0,-3], [5,5,5] ]], [[ [-3, -3, -3], [5,0,-3], [5,5,-3] ]], [[ [5, -3, -3], [5,0,-3], [5,-3,-3] ]], [[ [5, 5, -3], [5,0,-3], [-3,-3,-3] ]],])_, concat_res = test_edge_det(Krisch_kernel)Image.fromarray(concat_res)
Robinson算子

In [8]
robinson_kernel = np.array([ [[ [1, 2, 1], [0, 0, 0], [-1, -2, -1] ]], [[ [0, 1, 2], [-1, 0, 1], [-2, -1, 0] ]], [[ [-1, 0, 1], [-2, 0, 2], [-1, 0, 1] ]], [[ [-2, -1, 0], [-1, 0, 1], [0, 1, 2] ]], [[ [-1, -2, -1], [0, 0, 0], [1, 2, 1] ]], [[ [0, -1, -2], [1, 0, -1], [2, 1, 0] ]], [[ [1, 0, -1], [2, 0, -2], [1, 0, -1] ]], [[ [2, 1, 0], [1, 0, -1], [0, -1, -2] ]],])_, concat_res = test_edge_det(robinson_kernel)Image.fromarray(concat_res)
Laplacian 算子

In [9]
laplacian_kernel = np.array([ [[ [1, 1, 1], [1, -8, 1], [1, 1, 1] ]], [[ [0, 1, 0], [1, -4, 1], [0, 1, 0] ]]])_, concat_res = test_edge_det(laplacian_kernel)Image.fromarray(concat_res)
总结
简单介绍并实现了几种常用的传统边缘检测算子
以上就是边缘检测系列1:传统边缘检测算子的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/68035.html
微信扫一扫
支付宝扫一扫