溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用cv2.imread()讀取BGR圖像

發(fā)布時(shí)間:2021-03-16 16:36:36 來源:億速云 閱讀:1570 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹如何使用cv2.imread()讀取BGR圖像,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

opencv讀取圖像為b,g,r方法,比如

img = cv2.imread("xx.jpg")
cv2.imshow("xx",img)

因?yàn)閜lt函數(shù)是rgb方式讀取的,所以會(huì)出錯(cuò)。這時(shí)我們可以手動(dòng)改變img的通道順序,如下:

b,g,r = cv2.split(img)
img_rgb = cv2.merge([r,g,b])
plt.figure()
plt.imshow(img_rgb)
plt.show()

這時(shí)img_rgb就是rgb順序的了.那么這時(shí)再用cv2.imshow()顯示出來,rgb錯(cuò)誤:

如何使用cv2.imread()讀取BGR圖像

補(bǔ)充:盤點(diǎn)踩過的關(guān)于cv2 和PIL 圖像讀取的一些小坑

1、首先像素讀取順序不同

PIL 讀取圖像時(shí)的像素順序是標(biāo)準(zhǔn)的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 讀取圖像時(shí)的像素順序是標(biāo)準(zhǔn)的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ù)解釋

首先我們先來看一下這個(gè)函數(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指定了所讀取圖片的顏色類型, 默認(rèn)值為1

對(duì)應(yīng)值為 -1 到 4

參數(shù)Value
IMREAD_UNCHANGEDIf set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALEIf set, always convert image to the single channel grayscale image.
IMREAD_COLORIf set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTHIf set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLORIf set, the image is read in any possible color format.
IMREAD_LOAD_GDALIf set, use the gdal driver for loading the image.
參數(shù)Value
flag=-1時(shí)8位深度,原通道
flag=08位深度,1通道
flag=18位深度 ,3通道
flag=2原深度,1通道
flag=3原深度,3通道
flag=48位深度 ,3通道

IMREAD_UNCHANGED :不進(jìn)行轉(zhuǎn)化,比如保存為了16位的圖片,讀取出來仍然為16位。

IMREAD_GRAYSCALE :進(jìn)行轉(zhuǎn)化為灰度圖,比如保存為了16位的圖片,讀取出來為8位,類型為CV_8UC1。

IMREAD_COLOR :進(jìn)行轉(zhuǎn)化為三通道圖像。

IMREAD_ANYDEPTH :如果圖像深度為16位則讀出為16位,32位則讀出為32位,其余的轉(zhuǎn)化為8位。

IMREAD_ANYCOLOR

IMREAD_LOAD_GDAL :使用GDAL驅(qū)動(dòng)讀取文件,GDAL(Geospatial Data Abstraction Library)是一個(gè)在X/MIT許可協(xié)議下的開源柵格空間數(shù)據(jù)轉(zhuǎn)換庫。它利用抽象數(shù)據(jù)模型來表達(dá)所支持的各種文件格式。它還有一系列命令行工具來進(jìn)行數(shù)據(jù)轉(zhuǎn)換和處理。

關(guān)于如何使用cv2.imread()讀取BGR圖像就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI