当前位置:   article > 正文

pytorch使用卷积神经网络提取图片边缘信息_pytorch图片轮廓提取网络层设计

pytorch图片轮廓提取网络层设计
  1. import numpy as np
  2. import torch
  3. from torch import nn
  4. from torch.autograd import Variable
  5. import torch.nn.functional as F
  6. from PIL import Image
  7. import matplotlib.pyplot as plt
  8. im=Image.open('./girl.jpg').convert('L')
  9. im = np.array(im, dtype='float32')
  10. plt.figure()
  11. plt.subplot(1,2,1)
  12. plt.imshow(im.astype('uint8'),cmap='gray')
  13. im1 = torch.from_numpy(im.reshape((1,1,im.shape[0],im.shape[1])))
  14. conv1 = nn.Conv2d(1, 1, 3, bias=False)
  15. sobel_kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype='float32')
  16. sobel_kernel = sobel_kernel.reshape((1, 1, 3, 3))
  17. conv1.weight.data = torch.from_numpy(sobel_kernel)
  18. edge1 = conv1(Variable(im1))
  19. edge1 = edge1.data.squeeze().numpy()
  20. plt.subplot(1,2,2)
  21. plt.imshow(edge1, cmap='gray')
  22. plt.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/717097
推荐阅读
相关标签
  

闽ICP备14008679号