#Paddle框架:图像边缘检测
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D
from paddle.fluid.initializer import NumpyArrayInitializer
img = Image.open('1.jpg')
with fluid.dygraph.guard():
#设置卷积核参数
w = np.array([[-1, -1, -1], [-1, 8, -1],[-1, -1, -1]],dtype='float32')/8
w = w.reshape([1, 1, 3, 3])#输入通道为3,需要调整通道
w = np.repeat(w, 3, axis=1)
# 创建卷积算子,输出通道数为1,卷积核大小为3x3,
# 并使用上面的设置好的数值作为卷积核权重的初始化参数
conv = Conv2D(num_channels=3,num_filters=1,filter_size=[3, 3],param_attr=fluid.ParamAttr(
initializer=NumpyArrayInitializer(value=w)
))
#将读取的图片转换
x= np.array(img).astype('float32')
x=np.transpose(x,(2,0,1))
# 将数据形状调整为[N, C, H, W]格式
x = x.reshape(1, 3, img.height, img.width)
x = fluid.dygraph.to_variable(x)
y=conv(x)
out = y.numpy()
plt.figure(figsize=(20,10))
f = plt.subplot(121)
f.set_title('input_image',fontsize=15)
plt.imshow(img)
f=plt.subplot(122)
f.set_title('out_image', fontsize=15)
plt.imshow(out.squeeze(), cmap='gray')
plt.show()
Paddle框架下的图像边缘检测
最新推荐文章于 2024-03-19 09:55:19 发布