美文网首页
opencv-PIL-matplotlib-Skimage-Py

opencv-PIL-matplotlib-Skimage-Py

作者: 苗书宇 | 来源:发表于2019-02-27 20:20 被阅读0次

    世界静好 | 我心依然

    opencv2

    • 图像类型:BGR
    • 数据类型:numpy
    • 元素类型:uint8
    • 通道格式:H,W,C
    import cv2
    import numpy as np
    
    img = cv2.imread('image.jpg')      #读取图片
    cv2.imshow('the window name',img)  #显示图像
    cv2.waitKey()                      
    CV2.imwrite('new_image.jpg',img)   #保存图片
    print(type(img))   #数据类型(numpy)
    print(img.dtype)   #元素类型(uint8)
    print(img.shape)  #通道格式(H,W,C)
    print(img.size)   #像素点数
    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)  #BGR转RGB
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  #BGR转灰度图
    gray = cv2.imread('image.jpg',cv2.IMREAD_GRAYSCALE)  #灰度图读取
    image = cv2.resize(img,(100,200),interpolation=cv2.INTER_LINEAR) #resize
    b,g,r = cv2.split(img)   #通道分离
    merge_img = cv2.merge((b,g,r))   #通道合并
    
    

    PIL

    • 图像类型:RGB
    • 数据类型:Image
    • 元素类型:uint8
    • 通道格式:H,W,C
    from PIL import Image
    import numpy as np
    
    img = Image.open('image.jpg') #读取图片
    img.show()  #展示图片
    print(img_pil.mode)     #图像类型
    print(img_pil.size)     #图像的宽高
    img_arr = np.array(img)   #转为numpy形式,(H,W,C)
    new_img = Image.fromarray(img_arr) #再转换为Image形式
    new_img.save('newimage.jpg') #保存图片
    gary = Image.open('image.jpg').convert('L')  #灰度图
    r,g,b = img.split()  #通道的分离
    img = Image.merge('RGB',(r,g,b))  #通道的合并
    img_copy = img.copy()   #图像复制
    img_resize = img.resize((w,h))   #resize
    

    matplotlib

    • 图像类型:RGB
    • 数据类型:numpy
    • 元素类型:float
    • 通道格式:H,W,C
    import matplotlib.pyplot as plt
    import numpy as np 
    
    img  = plt.imread('image.jpg') #读取图片
    plt.imshow(img) 
    plt.show()
    plt.savefig('new_img.jpg')  #保存图片
    img_r = img[:,:,0]   #灰度图
    plt.imshow(img_r,cmap='Greys_r')  #显示灰度图
    

    Skimage

    • 图像类型:RGB
    • 数据类型:numpy
    • 元素类型:uint8(三原色),float64(resize后或者灰度图,且为0~1)
    • 通道格式:H,W,C
    import skimage
    from skimage import io,transform
    
    import numpy as np
    image= io.imread('test.jpg',as_grey=False) #读取图片 False原图,True灰度图
    print(type(img))   #数据类型(numpy)
    print(img.dtype)   #元素类型(uint8)
    print(img.shape)  #通道格式(H,W,C)
    image = transform.resize(image,(h, w),order=1) # order默认是1,双线性
    

    Pytorch.ToTensor

    • 接受对象:PIL Image或者numpy.ndarray
    • 接受格式:输入为H*W*C
    • 处理过程:自己转换为C*H*W,再转为float后每个像素除以255

    各种库之间的转换

    • Tensor转为numpy:
      np.array(Tensor)
    • numpy转为Tensor:
      torch.from_numpy(numpy.darray)
    • PIL.Image.Image换成numpy:
      np.array(PIL.Image.Image)
    • numpy转成PIL.Image.Image:
      注意:保证numpy.ndarray 转换成np.uint8,numpy.astype(np.uint8),像素值[0,255];  
      灰度图像保证numpy.shape为(H,W),不能出现channels 
      这里需要np.squeeze()。  
      彩色图象保证numpy.shape为(H,W,3)```
      
    • PIL.Image.Image转换成Tensor:
      彩色图像
      img2=Image.open('1.tif').convert('RGB')
      import torchvision.transforms as  transforms
      trans=transforms.Compose([transforms.ToTensor()])
      a=trans(img2)
      a=np.array(a)
      maxi=a.max()
      a=a/maxi*255
      a=a.transpose(1,2,0).astype(np.uint8)
      b=Image.fromarray(a)
      b
      
    • PIL.Image转换成OpenCV
      import cv2  
      from PIL import Image  
      import numpy  
        
      image = Image.open("1.jpg")  
      image.show()  
      img = cv2.cvtColor(np.array(image),cv2.COLOR_RGB2BGR)  
      cv2.imshow("OpenCV",img)  
      cv2.waitKey()  
      

    注释:cv2写图像时,灰度图像shape可以为(H,W)或(H,W,1)。彩色图像(H,W,3)
    要从numpy.ndarray得到PIL.Image.Image,灰度图的shape必须为(H,W),彩色为(H,W,3)

    相关文章

      网友评论

          本文标题:opencv-PIL-matplotlib-Skimage-Py

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