詳解python opencv、scikit-image和PIL圖像處理庫(kù)比較
進(jìn)行深度學(xué)習(xí)時(shí),對(duì)圖像進(jìn)行預(yù)處理的過(guò)程是非常重要的,使用pytorch或者TensorFlow時(shí)需要對(duì)圖像進(jìn)行預(yù)處理以及展示來(lái)觀看處理效果,因此對(duì)python中的圖像處理框架進(jìn)行圖像的讀取和基本變換的掌握是必要的,接下來(lái)python中幾個(gè)基本的圖像處理庫(kù)進(jìn)行縱向?qū)Ρ取?/p>
項(xiàng)目地址:https://github.com/Oldpan/Pytorch-Learn/tree/master/Image-Processing
比較的圖像處理框架:
- PIL
- scikit-image
- opencv-python
PIL:
由于PIL僅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基礎(chǔ)上創(chuàng)建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又加入了許多新特性,因此,我們可以直接安裝使用Pillow。
摘自廖雪峰的官方網(wǎng)站
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.
摘自官網(wǎng)的介紹,scikit-image的更新還是比較頻繁的,代碼質(zhì)量也很好。
opencv-python
opencv的大名就不要多說(shuō)了,這個(gè)是opencv的python版
# Compare Image-Processing Modules
# Use Transforms Module of torchvision
# &&&
# 對(duì)比python中不同的圖像處理模塊
# 并且使用torchvision中的transforms模塊進(jìn)行圖像處理
# 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()]) # 轉(zhuǎn)換為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, 444, 3) img_plt is (444, 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'>
# 定義一個(gè)圖像顯示函數(shù)
def my_imshow(image, title=None):
plt.imshow(image)
if title is not None:
plt.title(title)
plt.pause(0.001) # 這里延時(shí)一下,否則圖像無(wú)法加載
plt.figure()
my_imshow(img_skimage, title='img_skimage')
# 可以看到opencv讀取的圖像打印出來(lái)的顏色明顯與其他不同
plt.figure()
my_imshow(img_opencv, title='img_opencv')
plt.figure()
my_imshow(img_plt, title='img_plt')
# opencv讀出的圖像顏色通道為BGR,需要對(duì)此進(jìn)行轉(zhuǎn)換
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') # 隨機(jī)裁剪 transform_randomCrop = transforms.Compose([transforms.RandomCrop(32, padding=4)]) temp = transform_scale(img_PIL) plt.figure() my_imshow(temp, title='after_randomcrop') # 隨機(jī)進(jìn)行水平翻轉(zhuǎn)(0.5幾率) transform_ranHorFlip = transforms.Compose([transforms.RandomHorizontalFlip()]) temp = transform_scale(img_PIL) plt.figure() my_imshow(temp, title='after_ranhorflip') # 隨機(jī)裁剪到特定大小 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') # 標(biāo)準(zhǔn)化是在整個(gè)數(shù)據(jù)集中對(duì)所有圖像進(jìn)行取平均和均方差,演示圖像數(shù)量過(guò)少無(wú)法進(jìn)行此操作 # print(train_data.mean(axis=(0,1,2))/255) # print(train_data.std(axis=(0,1,2))/255) # transform_normal = transforms.Compose([transforms.Normalize()]) # Lamdba使用用戶自定義函數(shù)來(lái)對(duì)圖像進(jìn)行剪裁 # transform_pad = transforms.Compose([transforms.Lambda()])

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python opencv鼠標(biāo)畫矩形框之cv2.rectangle()函數(shù)
- Python實(shí)現(xiàn)Opencv cv2.Canny()邊緣檢測(cè)
- python opencv檢測(cè)直線 cv2.HoughLinesP的實(shí)現(xiàn)
- Python下opencv庫(kù)的安裝過(guò)程及問(wèn)題匯總
- python讀取并顯示圖片的三種方法(opencv、matplotlib、PIL庫(kù))
- python環(huán)境下安裝opencv庫(kù)的方法
- Python基于OpenCV庫(kù)Adaboost實(shí)現(xiàn)人臉識(shí)別功能詳解
- python使用cv2庫(kù)、下載opencv庫(kù)的方法
相關(guān)文章
Python實(shí)現(xiàn)合并多張圖片成視頻的示例詳解
隨著短視頻的興起,越來(lái)越多的人開始用各種形式進(jìn)行視頻制作,本篇博客從程序員的角度為大家解析一下如何通過(guò)?Python?合并多個(gè)圖片為一個(gè)視頻,需要的可以參考一下2023-02-02
Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問(wèn)題完整實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問(wèn)題,簡(jiǎn)單描述了生產(chǎn)者、消費(fèi)者問(wèn)題的概念、原理,并結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)生產(chǎn)者、消費(fèi)者問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Python實(shí)現(xiàn)平行坐標(biāo)圖的繪制(plotly)方式
今天小編就為大家分享一篇Python實(shí)現(xiàn)平行坐標(biāo)圖的繪制(plotly)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Python Pandas 轉(zhuǎn)換unix時(shí)間戳方式
今天小編就為大家分享一篇Python Pandas 轉(zhuǎn)換unix時(shí)間戳方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
使用Python實(shí)現(xiàn)簡(jiǎn)單的任務(wù)自動(dòng)化
在現(xiàn)代工作和生活中,任務(wù)自動(dòng)化可以極大地提高效率和準(zhǔn)確性,Python則是實(shí)現(xiàn)任務(wù)自動(dòng)化的理想選擇,下面我們來(lái)看看如何用Python實(shí)現(xiàn)任務(wù)自動(dòng)化吧2025-01-01
Pycharm 如何設(shè)置HTML文件自動(dòng)補(bǔ)全代碼或標(biāo)簽
這篇文章主要介紹了Pycharm 如何設(shè)置HTML文件自動(dòng)補(bǔ)全代碼或標(biāo)簽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

