做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簡單的畫圖請關注下篇~
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
