美文网首页
使用paddle实现图像模糊

使用paddle实现图像模糊

作者: 盗花 | 来源:发表于2020-10-29 16:21 被阅读0次

    卷积核使得当前像素跟它邻域内的像素取平均,实现图像的模糊与平滑,示例如下:

    # img = Image.open('tiger.jpg').convert('L')
    img = Image.open('tiger.jpg')
    # img = Image.open('car.jpg')
    img = np.array(img)
    im0 = img[:, :, 0]
    im1 = img[:, :, 1]
    im2 = img[:, :, 2]
    
    def train(im):
        with fluid.dygraph.guard(fluid.CUDAPlace(0)):
            w = np.ones([1, 1, 9, 9], dtype='float32') / 81
            # w = np.repeat(w, 3, axis=1)
            # w = np.repeat(w, 3, axis=0)
            conv = Conv2D(num_channels=1, num_filters=1, filter_size=(9, 9), param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer(value=w)))
    
            x = im.astype('float32')
            x = x.reshape(1, 1, img.shape[0], img.shape[1])
            x = fluid.dygraph.to_variable(x)
            y = conv(x)
            out = y.numpy()
            return out
    
    plt.figure(figsize=(20, 12))
    f = plt.subplot(1, 2, 1)
    f.set_title('input image')
    # plt.imshow(img, cmap='gray')
    plt.imshow(img)
    
    f = plt.subplot(1, 2, 2)
    f.set_title('output feature map')
    out0 = train(im0).squeeze()
    out1 = train(im1).squeeze()
    out2 = train(im2).squeeze()
    out = np.ones((out0.shape[0], out0.shape[1], 3))
    out[:, :, 0] = out0
    out[:, :, 1] = out1
    out[:, :, 2] = out2
    
    plt.imshow(out.astype('int64'))
    

    原图为:


    tiger.jpg

    模糊处理后的图为:


    blur_tiger.jpg

    相关文章

      网友评论

          本文标题:使用paddle实现图像模糊

          本文链接:https://www.haomeiwen.com/subject/wmakvktx.html