爽歪歪666
不以物喜,不以己悲,努力才是永恒的主题。

1. PIL:读取的是图像格式

  1)

    image = Image.open(r'./Input/Images/33039_LR.png')
    print(image)

输出结果:

<PIL.PngImagePlugin.PngImageFile image mode=RGB size=80x120 at 0xD5E8400>

  2)

1     image = Image.open(r'./Input/Images/33039_LR.png').convert('RGB')  PIL.Image.Image对象
2     print(image)
3     w,h = image.size
4     print(w,h)

输出结果:

<PIL.Image.Image image mode=RGB size=80x120 at 0xA079B00>
80 120

  3)

1     import numpy as np
2     image = Image.open(r'./Input/Images/33039_LR.png').convert('RGB')
3     img = np.array(image)
4     print(img)# 0-255

 

2.常用的图像处理方法

 3.skimage

 

skimage即scikit-image,PIL和Pillow只提供最基础的数字图像处理,功能有限,OpenCV是一个c++库,只是提供了Python接口,更新速度非常慢,scikit-image是基于scipy的一款图像处理包,将图片作为numpy数组进行处理,正好与MATLAB一样。

 

子模块名称                 主要实现功能

 

  1. io                            读取、保存和显示图片或视频
  2. data                       提供一些测试图片和样本数据
  3. color                           颜色空间变换
  4. filters             图像增强、边缘检测、排序滤波器、自动阈值等
  5. draw               操作于numpy数组上的基本图形绘制,包括线条、矩形、圆和文本等
  6. transform          几何变换或其它变换,如旋转、拉伸和拉东变换等
  7. morphology          形态学操作,如开闭运算、骨架提取等
  8. exposure              图片强度调整,如亮度调整、直方图均衡等
  9. feature                        特征检测与提取等
  10. measure                  图像属性的测量,如相似性或等高线等
  11. segmentation                          图像分割
  12. restoration                           图像恢复
  13. util                                  通用函数

 

安装:pip install scikit-image

 

官网:https://scikit-image.org/

 

 1 from skimage import io as img

2 from skimage import color, morphology, filters 

# 读取图像-> [H,W,C],范围[0-255]

 1 x = img.imread('%s/%s' % (opt.input_dir,opt.input_name))
 2 
 3 if opt.nc_im == 3:#如果是3通道
 4     x = x[:,:,:,None]
 5     x = x.transpose((3, 2, 0, 1))/255 # [H,W,C,None]->[None,C,H,W]
 6 else:
 7     x = color.rgb2gray(x) #转换为灰度图像
 8     x = x[:,:,None,None]
 9     x = x.transpose(3, 2, 0, 1) # [H,W,None,None]->[None,None,H,W]
10 
11 x = torch.from_numpy(x) # 转换为tensor

 

 

 

 

 

posted on 2019-12-18 10:43  爽歪歪666  阅读(503)  评论(0编辑  收藏  举报