2017-01-07 519 views

回答

2

您可以使用split

blue, green, red = cv2.split(img) 

或者,因爲這是一個代價高昂的操作在時間上,你只需要channles之一(例如,紅色),您可以直接切片原始圖像類似如下:

red = img[:,:,2] 

將返回一個greyscaled圖像,其中亮的像素是具有原始圖像中較高的紅色值的。

希望這有助於!

+1

我打算讓這是一個自我回答的問題,但是,我覺得你的表現更好。謝謝 –

1
# import usefull libraries 
import numpy as np 
import cv2 

# Set global parameters 
RED = 0 
GREEN = 1 
BLUE = 2 

# Load the image 
img_color = cv2.imread("fruits.jpg", flags=cv2.IMREAD_COLOR) 

# Filter the image by desired color 
img_color_filtered = np.asarray([y[RED] for x in img_color for y in x]).reshape((img_color.shape[:2]))