深度学习_卷积神经网络_Sobel算子应用于卷积核

我们用Sobel边缘检测器对卷积运算进行赋值,对一幅3通道图像计算水平和竖直方向的边缘强度。

首先我们读入图像:

#读入图像
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

im = Image.open(...)
im = np.array(...) / 255.0

把图像转换为pytorch Tensor,并调整为卷积层的维度要求,即卷积层输入的张量维度为(N,C,H,W),其中N为图像张量的数量,C为通道数,H,W分别是图像的高度和宽度。

print(im.shape)
im = torch.from_numpy(np.transpose(im, (2, 0, 1)))
im.unsqueeze_(0)
print(im.shape)

结果:
(360, 450,3)
torch.Size([1, 3, 360, 450])

定义水平方向的Sobel算子:

sobel_x = np.array([[-1.0, 0.0, 1.0], [-2.0, 0.0, 2.0], [-1.0, 0.0, 1.0]]).astype(dtype='float32')

sobel_x = np.reshape(sobel_x, (1, 3, 3))
print(sobel_x.shape)
print(sobel_x)

结果:
(1, 3,3)
[[-1, 0,1]
[-2, 0, 2]
[-1, 0, 1]]

定义竖直方向的Sobel算子:

sobel_y = np.array([[-1.0, -2.0, -1.0], [0.0, 0.0, 0.0], [1.0, 2.0, 1.0]]).astype(dtype='float32')
sobel_y = np.reshape(sobel_y, (1, 3, 3))
print(sobel_y.shape)
print(sobel_y)

结果:
(1, 3, 3)
[[-1, -2, -1]
[0, 0, 0]
[1, 2, 1]]

把水平和竖直方向的Sobel算子拼接为(Out, In, Kh,Kw)的阵列,其中Out=2为卷积层输出通道数目(即卷积核的数目,本例中为2,表示有两个卷积核Sobel_x,Sobel_y),In=3为卷积层输入图像的通道数(本例中为RGB三通道图像,因此In=3),Kh和Kw为卷积核的大小,为3。

sobel3d = np.concatenate(
		np.repeat(sobel_x, 3, axis=0).reshape((1, 3, 3, 3)),
		np.repeat(sobel_y, 3, axis=0).reshape((1, 3, 3, 3)),
		axis=0)
print(sobel3d.shape)

结果:
(2, 3,3,3)

定义一个卷积操作,并设置该卷积操作的参数为Sobel_3d:
注意卷积操作的参数设置要与Sobel3d匹配。

conv = nn.Conv2d(in_channels=3, out_channels=2, kernel_size=3, padding=1,stride=[1,1], bias=False)

conv.weight.data = torch.from_numpy(sobel3d)

#用卷积操作对图像进行卷积
edge = conv(im).data
print(edge.shape)

#分别获取水平和竖直方向的边缘响应
edge_x = edge[0, 0, :, :].squeeze().numpy()
edge_y = edge[0, 0, :, :].squeeze().numpy()

结果:
torch.Size([1, 2, 360, 540])

发布了103 篇原创文章 · 获赞 37 · 访问量 4607

猜你喜欢

转载自blog.csdn.net/Rocky6688/article/details/104076259