详解python opencv、scikit-image和PIL图像处理库比较


Posted in Python onDecember 26, 2019

进行深度学习时,对图像进行预处理的过程是非常重要的,使用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, 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'>
# 定义一个图像显示函数
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')

详解python opencv、scikit-image和PIL图像处理库比较

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,1,2))/255)
# transform_normal = transforms.Compose([transforms.Normalize()])

# Lamdba使用用户自定义函数来对图像进行剪裁
# transform_pad = transforms.Compose([transforms.Lambda()])

详解python opencv、scikit-image和PIL图像处理库比较

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中使用pyhook实现键盘监控的例子
Jul 18 Python
Python Web框架Flask中使用七牛云存储实例
Feb 08 Python
python爬虫之BeautifulSoup 使用select方法详解
Oct 23 Python
Python语言描述机器学习之Logistic回归算法
Dec 21 Python
Python生成任意范围任意精度的随机数方法
Apr 09 Python
在IPython中执行Python程序文件的示例
Nov 01 Python
PyQt5 对图片进行缩放的实例
Jun 18 Python
Django 实现前端图片压缩功能的方法
Aug 07 Python
python实现的多任务版udp聊天器功能案例
Nov 13 Python
django 文件上传功能的相关实例代码(简单易懂)
Jan 22 Python
Python爬虫爬取ts碎片视频+验证码登录功能
Feb 22 Python
Python实现我的世界小游戏源代码
Mar 02 Python
torch 中各种图像格式转换的实现方法
Dec 26 #Python
python两个_多个字典合并相加的实例代码
Dec 26 #Python
Python时间差中seconds和total_seconds的区别详解
Dec 26 #Python
python requests模拟登陆github的实现方法
Dec 26 #Python
python 实现按对象传值
Dec 26 #Python
Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析
Dec 26 #Python
Pandas时间序列:重采样及频率转换方式
Dec 26 #Python
You might like
我的论坛源代码(八)
2006/10/09 PHP
php mail to 配置详解
2014/01/16 PHP
php获取文件夹路径内的图片以及分页显示示例
2014/03/11 PHP
PHP微信API接口类
2016/08/22 PHP
在 Laravel 6 中缓存数据库查询结果的方法
2019/12/11 PHP
用 Javascript 验证表单(form)中的单选(radio)值
2009/09/08 Javascript
javascript事件函数中获得事件源的两种不错方法
2014/03/17 Javascript
Javascript实现的常用算法(如冒泡、快速、鸽巢、奇偶等)
2014/04/29 Javascript
Jquery日期选择datepicker插件用法实例分析
2015/06/08 Javascript
基于jQuery实现仿搜狐辩论投票动画代码(附源码下载)
2016/02/18 Javascript
使用postMesssage()实现跨域iframe页面间的信息传递方法
2016/03/29 Javascript
canvas 弹幕效果(实例分享)
2017/01/11 Javascript
在 Angular2 中实现自定义校验指令(确认密码)的方法
2017/01/23 Javascript
canvas绘制多边形
2017/02/24 Javascript
详解node单线程实现高并发原理与node异步I/O
2017/09/21 Javascript
基于axios 解决跨域cookie丢失的问题
2018/09/26 Javascript
原生JavaScript实现五子棋游戏
2020/11/09 Javascript
Python里隐藏的“禅”
2014/06/16 Python
python里大整数相乘相关技巧指南
2014/09/12 Python
python机器学习之神经网络(一)
2017/12/20 Python
python3利用Dlib19.7实现人脸68个特征点标定
2018/02/26 Python
PyTorch CNN实战之MNIST手写数字识别示例
2018/05/29 Python
python基础梳理(一)(推荐)
2019/04/06 Python
IronPython连接MySQL的方法步骤
2019/12/27 Python
基于pandas向csv添加新的行和列
2020/05/25 Python
expedia比利时:预订航班+酒店并省钱
2018/07/13 全球购物
HQhair美国/加拿大:英国化妆品、美容及美发产品商城
2019/04/15 全球购物
Onzie官网:美国时尚瑜伽品牌
2019/08/21 全球购物
护理自荐信
2013/10/22 职场文书
大学生创业计划书的范文
2014/01/07 职场文书
ktv筹备计划书
2014/05/03 职场文书
党员自我剖析材料
2014/08/31 职场文书
导游词怎么写
2015/02/04 职场文书
实习护士自荐信
2015/03/25 职场文书
JavaScript中10个Reduce常用场景技巧
2022/06/21 Javascript
Python软件包安装的三种常见方法
2022/07/07 Python