OpenCV、Skimage、PIL图像处理的细节差异
来源:互联网 发布:数据自动统计软件 编辑:程序博客网 时间:2024/05/18 09:52
上一篇博客中介绍了caffe实现多label输入,其中有一些图片处理的操作(训练验证数据),当时我选择了PIL库,但是在测试中用了caffe官网demo的代码,它使用了caffe提供的一些python接口,而它调用的是skimage这个库,所以有些许差异,可能会带来精度上的一些影响。这篇博客先介绍一下这些库的差异,下篇博客介绍测试时可能出现的小错误
PIL
首先介绍PIL(Python Imaging Library)这个库,这是Python中最基础的图像处理库,主要注意对图片进行处理时w,h的变化
from PIL import Imageimport numpy as npimage = Image.open('test.jpg') # 图片是400x300 宽x高print type(image) # out: PIL.JpegImagePlugin.JpegImageFileprint image.size # out: (400,300)print image.mode # out: 'RGB'print image.getpixel((0,0)) # out: (143, 198, 201)# resizeimage = image.resize(200,100,Image.NEAREST)print image.size # out: (200,100)'''代码解释**注意image是 class:`~PIL.Image.Image` object**,它有很多属性,比如它的size是(w,h),通道是RGB,,他也有很多方法,比如获取getpixel((x,y))某个位置的像素,得到三个通道的值,x最大可取w-1,y最大可取h-1比如resize方法,可以实现图片的放缩,具体参数如下resize(self, size, resample=0) method of PIL.Image.Image instance Returns a resized copy of this image. :param size: The requested size in pixels, as a 2-tuple: (width, height). 注意size是 (w,h),和原本的(w,h)保持一致 :param resample: An optional resampling filter. This can be one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`, :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`, :py:attr:`PIL.Image.BICUBIC` or :py:attr:`PIL.Image.LANCZOS`. If omitted, or if the image has mode "1" or "P", it is set :py:attr:`PIL.Image.NEAREST`. See: :ref:`concept-filters`. 注意这几种插值方法,默认NEAREST最近邻(分割常用),分类常用BILINEAR双线性,BICUBIC立方 :returns: An :py:class:`~PIL.Image.Image` object.'''image = np.array(image,dtype=np.float32) # image = np.array(image)默认是uint8print image.shape # out: (100, 200, 3)# 神奇的事情发生了,w和h换了,变成(h,w,c)了# 注意ndarray中是 行row x 列col x 维度dim 所以行数是高,列数是宽
Skimage
skimage即是Scikit-Image,官网
import skimagefrom skimage import io,transformimport numpy as npimage= io.imread('test.jpg',as_grey=False)# 第一个参数是文件名可以是网络地址,第二个参数默认为False,True时为灰度图print type(image) # out: numpy.ndarrayprint image.dtype # out: dtype('uint8')print image.shape # out: (300, 400, 3) (h,w,c)前面介绍了ndarray的特点# mode也是RGBprint image'''注意此时image里都是整数uint8,范围[0-255]array([ [ [143, 198, 201 (dim=3)],[143, 198, 201],... (w=200)], [ [143, 198, 201],[143, 198, 201],... ], ...(h=100) ], dtype=uint8)'''image= io.imread('test.jpg',as_grey=True)print image.shape # out: (300, 400)print image'''此时image范围变为[0-1]array([[ 0.73148549, 0.73148549, 0.73148549, ..., 0.73148549, 0.73148549, 0.73148549], [ 0.73148549, 0.73148549, 0.73148549, ..., 0.73148549, .....]])'''print image.dtype # out: dtype('float64')image = io.imread('test.jpg',as_grey=False) image = transform.resize(image,(100, 200),order=1) # order默认是1,双线性#resize后image范围又变成[0-1]print image.dtype # out: dtype('float64')print image.shape # out: (100, 200, 3)print image'''array([[[ 0.56078431, 0.77647059, 0.78823529], [ 0.56078431, 0.77647059, 0.78823529], [ 0.56078431, 0.77647059, 0.78823529], ..., ...]])''''''resize函数接口resize(image, output_shape, order=1, mode='constant', cval=0, clip=True, preserve_range=False)order : int, optional The order of interpolation. The order has to be in the range 0-5: - 0: Nearest-neighbor - 1: Bi-linear (default) - 2: Bi-quadratic - 3: Bi-cubic - 4: Bi-quartic - 5: Bi-quintic'''print skimage.img_as_float(image).dtype # out: float64# img_as_float可以把image转为double,即float64
OpenCV(python版)
OpenCV是个很强大的图像处理库,性能也很好。
import cv2import numpy as npimage = cv2.imread('test.jpg')print type(image) # out: numpy.ndarrayprint image.dtype # out: dtype('uint8')print image.shape # out: (300, 400, 3) (h,w,c) 和skimage类似print image'''array([ [ [143, 198, 201 (dim=3)],[143, 198, 201],... (w=200)], [ [143, 198, 201],[143, 198, 201],... ], ...(h=100) ], dtype=uint8)'''image = cv2.resize(image,(100,200),interpolation=cv2.INTER_LINEAR)print image.dtype # out: dtype('uint8')print image.shape # out: (200, 100, 3) '''注意注意注意 和skimage不同 resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) 关键字参数为dst,fx,fy,interpolationdst为缩放后的图像dsize为(w,h),但是image是(h,w,c)fx,fy为图像x,y方向的缩放比例,interplolation为缩放时的插值方式,有三种插值方式:cv2.INTER_AREA:使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN方法 cv2.INTER_CUBIC: 立方插值cv2.INTER_LINEAR: 双线形插值 cv2.INTER_NN: 最近邻插值[详细可查看该博客](http://www.tuicool.com/articles/rq6fIn)'''
在进行图像处理时一点要注意 各个库之间的细微差异,还有要注意图像放缩时插值方法的选择,而且即使是相同的插值方法,各个库的实现也不同,结果也会有些许差异。
1 0
- OpenCV、Skimage、PIL图像处理的细节差异
- PIL 的图像处理模块
- python图像处理之skimage
- 【python 图像处理】skimage的子模块介绍
- python5-PIL的其他图像处理模块
- 用 python skimage做图像处理
- pillow pil 图像处理
- PIL处理图像
- PIL图像处理
- PIL-图像处理
- matplotlib、PIL、cv2图像操作差异分析
- Python PIL图像处理-----图像的手绘效果
- opencv的图像处理
- OpenCV的图像处理
- skimage与PIL与png
- PIL---使用Python处理图像
- PIL处理图像(一)
- Python图像处理库 PIL
- CodeForces - 111C Petya and Spiders
- 2、猜数游戏
- spark 报错收集
- 欢迎使用CSDN-markdown编辑器
- 3、购物金额结算
- OpenCV、Skimage、PIL图像处理的细节差异
- 果编译时候出现错误:QMAKESPEC has not been set, so configuration cannot be deduced.
- 怎么解决java.lang.NoClassDefFoundError错误
- 4、求数组最大值
- HTML基本基础语法知识
- 恢复CPU二级缓存经验
- 5、向数组中插入一个元素
- Pom.xml文件教程详解
- 构造函数中参数初始化表与函数体赋值的区别