1. 程式人生 > >OpenCV、Skimage、PIL影象處理的細節差異

OpenCV、Skimage、PIL影象處理的細節差異

在進行影象處理時一點要注意 各個庫之間的細微差異,還有要注意影象放縮時插值方法的選擇,而且即使是相同的插值方法,各個庫的實現也不同,結果也會有些許差異

PIL(RGB)

首先介紹PIL(Python Imaging Library)這個庫,這是Python中最基礎的影象處理庫,主要注意對圖片進行處理時w,h的變化.

from PIL import Image
import numpy as np
image = Image.open('test.jpg') # 圖片是400x300 寬x高
print type(image) # out: PIL.JpegImagePlugin.JpegImageFile
print image.size  # out: (400,300)
print image.mode # out: 'RGB'
print image.getpixel((0,0)) # out: (143, 198, 201)
# resize w*h
image = 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)預設是uint8
print image.shape # out: (100, 200, 3)
# 神奇的事情發生了,w和h換了,變成(h,w,c)了
# 注意ndarray中是 行row x 列col x 維度dim 所以行數是高,列數是寬

Skimage(RGB)

import skimage
from skimage import io,transform
import numpy as np
image= io.imread('test.jpg',as_grey=False)
# 第一個引數是檔名可以是網路地址,第二個引數預設為False,True時為灰度圖
print type(image) # out: numpy.ndarray
print image.dtype # out: dtype('uint8')
print image.shape # out: (300, 400, 3) (h,w,c)前面介紹了ndarray的特點
# mode也是RGB
print 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) 
# h*w
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版)(BGR)

import cv2
import numpy as np
image = cv2.imread('test.jpg')
print type(image) # out: numpy.ndarray
print image.dtype # out: dtype('uint8')
print image.shape # out: (300, 400, 3) (h,w,c) 和skimage類似
print image # BGR
'''
array([
        [ [143, 198, 201 (dim=3)],[143, 198, 201],... (w=200)],
        [ [143, 198, 201],[143, 198, 201],... ],
        ...(h=100)
      ], dtype=uint8)

'''
# w*h
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,interpolation
dst為縮放後的影象
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)
'''
'''
cv2.imread(filename, flags=None):
flag:
cv2.IMREAD_COLOR 1: Loads a color image. Any transparency of image will be neglected. It is the default flag. 正常的3通道圖
cv2.IMREAD_GRAYSCALE 0: Loads image in grayscale mode 單通道灰度圖
cv2.IMREAD_UNCHANGED -1: Loads image as such including alpha channel 4通道圖
注意: 預設應該是cv2.IMREAD_COLOR,如果你cv2.imread('gray.png'),雖然圖片是灰度圖,但是讀入後會是3個通道值一樣的3通道圖片

'''

作者:爆米花好美啊
來源:CSDN
原文:https://blog.csdn.net/u013010889/article/details/54347089
版權宣告:本文為博主原創文章,轉載請附上博文連結!