侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   417

进行深度学习时,对图像进行预处理的过程是非常重要的,使用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模块进行图像处理# packagesfrom PIL import Imagefrom skimage import io, transformimport cv2import torchvision.transforms as transformsimport matplotlib.pyplot as plt%matplotlib inlineimg_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 ofimg_skimage is (444, 444, 3)img_opencv is (444, 444, 3)img_plt is (444, 444, 3)The size ofimg_PIL is (444, 444)The mode ofimg_PIL is RGBThe type ofimg_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()])

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


  • 上一条:
    python深copy和浅copy区别对比解析
    下一条:
    python两个_多个字典合并相加的实例代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客