1. 程式人生 > 程式設計 >詳解python opencv、scikit-image和PIL影象處理庫比較

詳解python opencv、scikit-image和PIL影象處理庫比較

進行深度學習時,對影象進行預處理的過程是非常重要的,使用pytorch或者TensorFlow時需要對影象進行預處理以及展示來觀看處理效果,因此對python中的影象處理框架進行影象的讀取和基本變換的掌握是必要的,接下來python中幾個基本的影象處理庫進行縱向對比。

專案地址:https://github.com/Oldpan/Pytorch-Learn/tree/master/Image-Processing

比較的影象處理框架:

  • PIL
  • scikit-image
  • opencv-python

PIL:

由於PIL僅支援到Python 2.7,加上年久失修,於是一群志願者在PIL的基礎上建立了相容的版本,名字叫Pillow,支援最新Python 3.x,又加入了許多新特性,因此,我們可以直接安裝使用Pillow。

摘自廖雪峰的官方網站

scikit-image

scikit-image is a collection of algorithms for image processing. It is available free of charge and free of restriction. We pride ourselves on high-quality,peer-reviewed code,written by an active community of volunteers.
摘自官網的介紹,scikit-image的更新還是比較頻繁的,程式碼質量也很好。

opencv-python


opencv的大名就不要多說了,這個是opencv的python版

# Compare Image-Processing Modules
# Use Transforms Module of torchvision
#        &&&
# 對比python中不同的影象處理模組
# 並且使用torchvision中的transforms模組進行影象處理

# packages
from PIL import Image
from skimage import io,transform
import cv2

import torchvision.transforms as transforms
import matplotlib.pyplot as plt
%matplotlib inline

img_PIL = Image.open('./images/dancing.jpg')
img_skimage = io.imread('./images/dancing.jpg')
img_opencv = cv2.imread('./images/dancing.jpg')
img_plt = plt.imread('./images/dancing.jpg')

loader = transforms.Compose([
  transforms.ToTensor()]) # 轉換為torch.tensor格式


print('The shape of \n img_skimage is {}\n img_opencv is {}\n img_plt is {}\n'.format(img_skimage.shape,img_opencv.shape,img_plt.shape))
print('The type of \n img_skimage is {}\n img_opencv is {}\n img_plt is {}\n'.format(type(img_skimage),type(img_opencv),type(img_plt)))
The shape of
img_skimage is (444,444,3)
img_opencv is (444,3)
img_plt is (444,3)
The size of
img_PIL is (444,444)
The mode of
img_PIL is RGB
The type of
img_skimage is <class 'numpy.ndarray'>
img_opencv is <class 'numpy.ndarray'>
img_plt is <class 'numpy.ndarray'>
img_PIL if <class 'PIL.JpegImagePlugin.JpegImageFile'>
# 定義一個影象顯示函式
def my_imshow(image,title=None):
  plt.imshow(image)
  if title is not None:
    plt.title(title)
  plt.pause(0.001) # 這裡延時一下,否則影象無法載入

plt.figure()
my_imshow(img_skimage,title='img_skimage')
# 可以看到opencv讀取的影象打印出來的顏色明顯與其他不同
plt.figure()
my_imshow(img_opencv,title='img_opencv')
plt.figure()
my_imshow(img_plt,title='img_plt')

# opencv讀出的影象顏色通道為BGR,需要對此進行轉換
img_opencv = cv2.cvtColor(img_opencv,cv2.COLOR_BGR2RGB)
plt.figure()
my_imshow(img_opencv,title='img_opencv_new')

toTensor = transforms.Compose([transforms.ToTensor()])

# 尺寸變化、縮放
transform_scale = transforms.Compose([transforms.Scale(128)])
temp = transform_scale(img_PIL)
plt.figure()
my_imshow(temp,title='after_scale')

# 隨機裁剪
transform_randomCrop = transforms.Compose([transforms.RandomCrop(32,padding=4)])
temp = transform_scale(img_PIL)
plt.figure()
my_imshow(temp,title='after_randomcrop')

# 隨機進行水平翻轉(0.5機率)
transform_ranHorFlip = transforms.Compose([transforms.RandomHorizontalFlip()])
temp = transform_scale(img_PIL)
plt.figure()
my_imshow(temp,title='after_ranhorflip')

# 隨機裁剪到特定大小
transform_ranSizeCrop = transforms.Compose([transforms.RandomSizedCrop(128)])
temp = transform_ranSizeCrop(img_PIL)
plt.figure()
my_imshow(temp,title='after_ranSizeCrop')

# 中心裁剪
transform_centerCrop = transforms.Compose([transforms.CenterCrop(128)])
temp = transform_centerCrop(img_PIL)
plt.figure()
my_imshow(temp,title='after_centerCrop')

# 空白填充
transform_pad = transforms.Compose([transforms.Pad(4)])
temp = transform_pad(img_PIL)
plt.figure()
my_imshow(temp,title='after_padding')

# 標準化是在整個資料集中對所有影象進行取平均和均方差,演示影象數量過少無法進行此操作
# print(train_data.mean(axis=(0,1,2))/255)
# print(train_data.std(axis=(0,2))/255)
# transform_normal = transforms.Compose([transforms.Normalize()])

# Lamdba使用使用者自定義函式來對影象進行剪裁
# transform_pad = transforms.Compose([transforms.Lambda()])

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。