详解Python Opencv和PIL读取图像文件的差别

yipeiwu_com6年前Python基础

前言

之前在进行深度学习训练的时候,偶然发现使用PIL读取图片训练的效果要比使用python-opencv读取出来训练的效果稍好一些,也就是训练更容易收敛。可能的原因是两者读取出来的数据转化为pytorch中Tensor变量稍有不同,这里进行测试。

之后的代码都导入了:

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import torch
import cv2

测试

使用PIL和cv2读取图片时会有细微的区别,通过下面的代码可以发现两者读取图片是有区别的,也就是使用PIL读取出来的图片转为numpy格式和直接使用cv读取的图片在像素点上并不是完全一致:

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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Ubuntu18.04下python版本完美切换的解决方法

Ubuntu18.04下python版本完美切换的解决方法

ubuntu18.04版本,python版本python2.7,python3.5,python3.6 因为安装一些库会安装到python3.6上,而默认使用的是python2.7,使用...

python使用if语句实现一个猜拳游戏详解

python使用if语句实现一个猜拳游戏详解

任务要求 在控制台中提示输入石头、剪刀、布,按回车键,然后给出游戏结果。 分析 我们知道在游戏规则中,石头克剪刀,剪刀克布,布克石头。但是这在计算机中并不是很好直接的表示,因此我们分别...

解决在pycharm中显示额外的 figure 窗口问题

解决在pycharm中显示额外的 figure 窗口问题

问题描述 在电脑中重新安装Anaconda3&PyCharm后,运行原来的程序画图时出现了下图界面。 不能弹出如下图所示的“figure”窗口。 解决方法: 这是因为PyCharm在...

基于tensorflow加载部分层的方法

一般使用 saver.restore(sess, modeldir + "model.ckpt") 即可加载已经训练好的网络,可是有时候想值使用部分层的参数,这时候可以选择在加载网...

python basemap 画出经纬度并标定的实例

如下所示: 两个函数:Basemap.drawparallels ##纬度 Basemap.drawmeridians ##经度 from mpl_toolkits.bas...