快速解決cv2.imread()讀取圖像為BGR的問題
opencv讀取圖像為b,g,r方法,比如
img = cv2.imread("xx.jpg") cv2.imshow("xx",img)
展示的結(jié)果是正常的:
但是此時讀取到的img已經(jīng)為bgr方式了,如果我們再用其他使用rgb方式讀取的函數(shù)進行讀取時就會出錯,比如我用plt對圖像進行顯示,效果如下:
因為plt函數(shù)是rgb方式讀取的,所以會出錯。這時我們可以手動改變img的通道順序,如下:
b,g,r = cv2.split(img) img_rgb = cv2.merge([r,g,b]) plt.figure() plt.imshow(img_rgb) plt.show()
這時img_rgb就是rgb順序的了.那么這時再用cv2.imshow()顯示出來,rgb錯誤:
補充:盤點踩過的關于cv2 和PIL 圖像讀取的一些小坑
1、首先像素讀取順序不同
PIL 讀取圖像時的像素順序是標準的RGB
from PIL import Image img = Image.open("test.jpg") print img.size print img.getpixel((0,0))
輸出結(jié)果是
(533, 800) (217, 229, 225)
cv2 讀取圖像時的像素順序是標準的BGR
img = cv2.imread(""test.jpg"") print img.shape print img[0][0]
輸出結(jié)果是
(800, 533, 3) [225 229 217]
若要cv2讀取完圖像也是RGB格式,則按如下方法
img = cv2.imread(""test.jpg"")[..., ::-1] print img.shape print img[0][0]
輸出結(jié)果是
(800, 533, 3) [217 229 225]
和用PIL 讀取完的一致
2、cv2 圖像讀取方法的參數(shù)解釋
首先我們先來看一下這個函數(shù)的定義
def imread(filename, flags=None)
filename
參數(shù)傳入的是圖像路徑,支持解析的圖像格式基本上覆蓋全了
- Windows bitmaps - \*.bmp, \*.dib (always supported) - JPEG files - \*.jpeg, \*.jpg, \*.jpe (see the *Note* section) - JPEG 2000 files - \*.jp2 (see the *Note* section) - Portable Network Graphics - \*.png (see the *Note* section) - WebP - \*.webp (see the *Note* section) - Portable image format - \*.pbm, \*.pgm, \*.ppm \*.pxm, \*.pnm (always supported) - Sun rasters - \*.sr, \*.ras (always supported) - TIFF files - \*.tiff, \*.tif (see the *Note* section) - OpenEXR Image files - \*.exr (see the *Note* section) - Radiance HDR - \*.hdr, \*.pic (always supported) - Raster and Vector geospatial data supported by GDAL (see the *Note* section)
flags
@param flags Flag that can take values of cv::ImreadModes
Flags指定了所讀取圖片的顏色類型, 默認值為1
對應值為 -1 到 4
參數(shù) | Value |
---|---|
IMREAD_UNCHANGED | If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). |
IMREAD_GRAYSCALE | If set, always convert image to the single channel grayscale image. |
IMREAD_COLOR | If set, always convert image to the 3 channel BGR color image. |
IMREAD_ANYDEPTH | If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit. |
IMREAD_ANYCOLOR | If set, the image is read in any possible color format. |
IMREAD_LOAD_GDAL | If set, use the gdal driver for loading the image. |
參數(shù) | Value |
---|---|
flag=-1時 | 8位深度,原通道 |
flag=0 | 8位深度,1通道 |
flag=1 | 8位深度 ,3通道 |
flag=2 | 原深度,1通道 |
flag=3 | 原深度,3通道 |
flag=4 | 8位深度 ,3通道 |
IMREAD_UNCHANGED
:不進行轉(zhuǎn)化,比如保存為了16位的圖片,讀取出來仍然為16位。
IMREAD_GRAYSCALE
:進行轉(zhuǎn)化為灰度圖,比如保存為了16位的圖片,讀取出來為8位,類型為CV_8UC1。
IMREAD_COLOR
:進行轉(zhuǎn)化為三通道圖像。
IMREAD_ANYDEPTH
:如果圖像深度為16位則讀出為16位,32位則讀出為32位,其余的轉(zhuǎn)化為8位。
IMREAD_ANYCOLOR
:
IMREAD_LOAD_GDAL
:使用GDAL驅(qū)動讀取文件,GDAL(Geospatial Data Abstraction Library)是一個在X/MIT許可協(xié)議下的開源柵格空間數(shù)據(jù)轉(zhuǎn)換庫。它利用抽象數(shù)據(jù)模型來表達所支持的各種文件格式。它還有一系列命令行工具來進行數(shù)據(jù)轉(zhuǎn)換和處理。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
Django contenttypes 框架詳解(小結(jié))
這篇文章主要介紹了Django contenttypes 框架詳解(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Python命令行參數(shù)解析模塊optparse使用實例
這篇文章主要介紹了Python命令行參數(shù)解析模塊optparse使用實例,本文講解了增加選項(add_option())、行為(action)、設置默認值(default)、生成幫助提示(help)、設置boolean值、錯誤處理、選項組(Grouping Options)等內(nèi)容,需要的朋友可以參考下2015-04-04