[英]How to show image in color channel of a image not in grayscale?
我試圖獲取彩色通道中的圖像,而不是灰度圖像。 這是它的代碼:-
img = cv2.imread("image.jpg")
blue = img[:,:,0].copy() # Blue channel image
blue[:,:,1] = 0 # Making green channel 0
blue[:,:,2] = 0 # Making red channel 0
但是,當我嘗試使“ image.jpg”的藍色通道的綠色通道和紅色通道0變為指向第3行和第4行時出現錯誤。
錯誤: IndexError: too many indices for array
我在Mac上使用OpenCV 3.3和python 3.6。
好吧,這里你去:
import cv2
import numpy as np
def get_channel(im, n):
if 0 <= n <= 2 and im.shape[2]==3:
new_im = np.zeros_like(im)
new_im[:, :, n] = im[:, :, n]
return new_im
return False
im = cv2.imread(your_image_filename_goes_here)
cv2.imshow("Chanel", get_channel(im, 0))
cv2.waitKey(0)
cv2.destroyAllWindows()
在您的示例中, blue
形狀為(x,y),而img
形狀為(x,y,3)。 這就是為什么顯示藍色是灰度的原因,因為作為二維數組opencv並不知道它是藍色通道。
彩色圖像只是三個“灰度圖像”的總和,盡管它們不像普通灰度圖像那樣代表光強度,但它們代表一定的彩色光強度。 並且三個通道的組合給出了顏色。
灰度圖像只是整數(3,3)的二維數組:
y y y
y y y
y y y
RGB(或OpenCV BGR)圖像是整數(3、3、3)的三維數組:
B B B G G G R R R
B B B G G G R R R
B B B G G G R R R
如果您僅拍攝BGR圖像的第一個頻道,那么您正在拍攝(3,3):
B B B
B B B
B B B
對於OpenCV,它與灰度圖像相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.