詳解Python Opencv和PIL讀取圖像文件的差別
前言
之前在進(jìn)行深度學(xué)習(xí)訓(xùn)練的時候,偶然發(fā)現(xiàn)使用PIL讀取圖片訓(xùn)練的效果要比使用python-opencv讀取出來訓(xùn)練的效果稍好一些,也就是訓(xùn)練更容易收斂??赡艿脑蚴莾烧咦x取出來的數(shù)據(jù)轉(zhuǎn)化為pytorch中Tensor變量稍有不同,這里進(jìn)行測試。
之后的代碼都導(dǎo)入了:
from PIL import Image import matplotlib.pyplot as plt import numpy as np import torch import cv2
測試
使用PIL和cv2讀取圖片時會有細(xì)微的區(qū)別,通過下面的代碼可以發(fā)現(xiàn)兩者讀取圖片是有區(qū)別的,也就是使用PIL讀取出來的圖片轉(zhuǎn)為numpy格式和直接使用cv讀取的圖片在像素點(diǎn)上并不是完全一致:
In[11]: image = cv2.imread('datasets/0_target.jpg') In[18]: image_pil = Image.open('datasets/0_target.jpg').convert('RGB') In[19]: image_pil = np.array(image_pil) In[20]: image_cv = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) In[21]: image_cv == image_pil Out[21]: array([[[ True, True, False], [ True, False, False], [False, False, False], ..., [ True, True, True], [ True, True, True], [ True, True, True]], [[ True, True, False], [ True, True, True], [False, True, False], ..., [ True, True, False], [ True, True, True], [ True, True, True]], [[ True, True, False], [ True, True, True], [False, False, False], ..., [ True, True, True], [ True, True, True], [ True, True, False]], ..., [[ True, True, True], [ True, True, True], [ True, True, True], ..., [False, False, True], [ True, True, True], [False, False, False]], [[ True, True, True], [ True, True, True], [ True, True, True], ..., [ True, True, True], [ True, True, True], [False, False, False]], [[ True, False, False], [ True, False, False], [ True, False, False], ..., [ True, True, True], [False, False, False], [ True, False, False]]]) In[26]: image_cv.shape Out[26]: (682, 700, 3) In[27]: image_pil.shape Out[27]: (682, 700, 3) In[28]: image_pil - image_cv Out[28]: array([[[ 0, 0, 1], [ 0, 255, 3], [255, 1, 2], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 2], [ 0, 0, 0], [255, 0, 2], ..., [ 0, 0, 254], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 2], [ 0, 0, 0], [255, 1, 2], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 254]], ..., [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [254, 1, 0], [ 0, 0, 0], [ 1, 255, 3]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 2, 254, 4]], [[ 0, 1, 253], [ 0, 1, 253], [ 0, 1, 255], ..., [ 0, 0, 0], [ 1, 254, 1], [ 0, 255, 2]]], dtype=uint8)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python神經(jīng)網(wǎng)絡(luò)之批量學(xué)習(xí)tf.train.batch函數(shù)示例
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)之批量學(xué)習(xí)tf.train.batch函數(shù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05使用Python遍歷文件夾實(shí)現(xiàn)查找指定文件夾
這篇文章主要為大家介紹了如何使用Python遍歷文件夾從而實(shí)現(xiàn)查找指定文件夾下所有相同名稱的文件、所有相同后綴名的文件,感興趣的可以了解一下2022-07-07python使用open函數(shù)對文件進(jìn)行處理詳解
今天看了open函數(shù),看到w+ r+ a+ 這種可讀可寫的操作,下面這篇文章主要給大家介紹了關(guān)于python使用open函數(shù)對文件進(jìn)行處理的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Windows下用py2exe將Python程序打包成exe程序的教程
這篇文章主要介紹了Windows下用py2exe將Python程序打包成exe程序的教程,文中主要針對Python3.x版本進(jìn)行說明,需要的朋友可以參考下2015-04-04Python中torch.load()加載模型以及其map_location參數(shù)詳解
torch.load()作用用來加載torch.save()保存的模型文件,下面這篇文章主要給大家介紹了關(guān)于Python中torch.load()加載模型以及其map_location參數(shù)的相關(guān)資料,需要的朋友可以參考下2022-09-09