欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python Opencv和PIL讀取圖像文件的差別

 更新時間:2019年12月27日 09:31:42   作者:Oldpan博客  
這篇文章主要介紹了詳解Python Opencv和PIL讀取圖像文件的差別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

之前在進(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)文章

最新評論