python圖片處理Image和skimage的不同

做cnn的難免要做大量的圖片處理。由於接手項目時間不長,且是新項目,前段時間寫代碼都很趕,現在稍微總結(恩,總結是個好習慣)。

1,首先安裝python-Image和python-skimage、python-matplotlib。

  簡單代碼:

import Image as img
import os
from matplotlib import pyplot as plot
from skimage import io,transform
import argparse

def show_data(data):
    fig = plot.figure()
    ax = fig.add_subplot(121)
    ax.imshow(data, cmap='gray')
    ax2 = fig.add_subplot(122)
    ax2.imshow(data)
    plot.show()
if __name__ == "__main__":
    parse = argparse.ArgumentParser()
    parse.add_argument('--picpath', help = "the picture' path")
    args = parse.parse_args()
    img_file1 = img.open(args.picpath)#Image讀圖片
    one_pixel = img_file1.getpixel((0,0))[0]
    print "picture's first pixe: ",one_pixel  
    print "the picture's size: ", img_file1.size#Image讀出來的size是高寬
    show_data(img_file1)
    img_file2 = io.imread(args.picpath)#skimage讀圖片
    show_data(img_file2)
    print "picture's first pixel: ", img_file2[0][0][0]
    print "the picture's shape: ", img_file2.shape#skimage讀出來的shape是高,寬, 通道

調用及輸出:


其實Image讀出來的是PIL什麼的類型,而skimage.io讀出來的數據是numpy格式的。如果想直接看Image和skimage讀出來圖片的區別,可以直接輸出它們讀圖片以後的返回結果。

2.Image和skimage讀圖片:

img_file1 = img.open(args.picpath)
img_file2 = io.imread(args.picpath)


3.讀圖片後數據的大小:

print "the picture's size: ", img_file1.size
print "the picture's shape: ", img_file2.shape


4.得到像素:

one_pixel = img_file1.getpixel((0,0))[0]
img_file2[0][0][0]



分析:

1.從3的輸出可以看出img讀圖片的大小是圖片的(height,width);

skimage的是(height,width, channel)[這也是爲什麼caffe在單獨測試時要要在代碼中設置:transformer.set_transpose('data',(2,0,1)),因爲caffe可以處理的圖片的數據格式是(channel,height,width),所以要轉換數據啊]

2.img讀出來的圖片獲得某點像素用getpixel((h,w))可以直接返回這個點三個通道的像素值

  skimage讀出來的圖片可以直接img_file2[0][0][0]獲得,但是一定記住它的格式,並不是你想的(channel,height,width)

 

關於matplotlib簡單的畫圖請關注下篇~


發佈了74 篇原創文章 · 獲贊 50 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章