详解python opencv、scikit-image和PIL图像处理库比较
作者:Oldpan 发布时间:2021-11-10 02:24:13
进行深度学习时,对图像进行预处理的过程是非常重要的,使用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')
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()])
来源:https://oldpan.me/archives/pytorch-transforms-opencv-scikit-image
猜你喜欢
- 如下所示:# the basic ways = 0for x in range(10): s += x# the right ways =
- 近日大家热议的盗版XP黑屏问题想必很多人都知道了,在这里就不多说。据媒体报道,微软公司将实施逐步投放策略,预计本周将投放5%黑屏,下周投放2
- 1、设置web.config文件。以下为引用的内容:<system.web> ...... <globalization
- 看了cragle的《有没有必要将网站Div+Css重构?》的文章,有一些想法不说不快,我也在文章的评论里提到曾经开除过两个执着使用div技术
- 1、获取秒级时间戳与毫秒级时间戳、微秒级时间戳import timeimport datetimet = time.time()print
- 在js中调用asp文件的方法很简单,我们可以用在静态页面的点击数统计,虽然静态页面不支持asp程序,但是我们可以使用js调用,来变相的达到这
- 本文实例讲述了Python自动扫雷实现方法。分享给大家供大家参考。具体如下:#pyWinmineCrack.py# coding: utf-
- 本文实例讲述了Python决策树之基于信息增益的特征选择。分享给大家供大家参考,具体如下:基于信息增益的特征选取是一种广泛使用在决策树(de
- 背景今天有人问我 “为什么数据库中有人推荐使用 int 类型来保存 IP 地址?”。现在(2020年)来看这个东西已经有点过时了,一方面是磁
- 在这个简短的教程中,我会介绍将python列表转换为字符串的不同方法。为什么要将python列表转换为字符串?将python列表转换为字符串
- 使用正则库爬取淘宝商品的商品信息,首先我们需要确定想要爬取的对象我们在淘宝里搜索“python”,出来的结果从url连接中可以得到搜索商品的
- 前言让我的电脑认识我,我的电脑只有认识我,才配称之为我的电脑!今天,我们用Python实现简单的人脸识别技术!Python里,简单的人脸识别
- OCR of Hand-written Data using kNNOCR of Hand-written Digits我们的目标是构建一个
- 简介到目前为止,我们查阅anaconda的官网可发现,由于目前Anaconda没有支持arm架构的版本,在M1芯片Mac上安装的Anacon
- 找了国内30个比较著名的网站的注册表单做样本,对标签和输入区对齐方式做了统计,得到了一个结论:标签水平右对齐更适合中文网站,或者说右对齐更适
- 近期遇到一个需求,就是用PySide2做出一个GUI,并且要将后台使用Matplotlib绘制的图显示在界面上。自己琢磨了蛮久,网上也搜了不
- 一、问题描述 SQL Plus WorkSheet是一个窗口图形界面的SQL语句编辑器,对于那些喜欢窗口界面而不喜欢字符界面的用户,该工具相
- 一开始自学Python的numpy、pandas时候,索引和切片把我都给弄晕了,特别是numpy的切片索引、布尔索引和花式索引,简直就是大乱
- 关于树莓派四驱小车的运动方向控制、摄像头方向控制已经在前面的两篇博文中介绍过。有需要的可以参考。本文也是基于上述两个python文件就绪的情
- 当你链接到应用程序以使用MySQL客户端库时,可能会遇到以mysql_开始的未定义引用错误,如下所示:/tmp/ccFKsdPa.o: 在函