如何在不使用OpenCV Python中的split函数的情况下获取图像的单色通道?

时间:2018-01-13 04:20:37

标签: python numpy opencv gesture-recognition image-thresholding

我想突出显示实时手势识别的一只手。我观察到,使用cv2.imsplit函数,不同颜色通道的手的图像会以不同方式突出显示。但是这种分裂功能在时间上非常昂贵。我无法使用Numpy索引执行相同的功能(如official page所示)

2 个答案:

答案 0 :(得分:2)

您可以使用numpy的切片:

import cv2
import numpy as np

## read image as np.ndarray in BGR order
img = cv2.imread("test.png")


## use OpenCV function to split channels
b, g, r = cv2.split(img)

## use numpy slice to extract special channel
b = img[...,0]
g = img[...,1]
r = img[...,2]

答案 1 :(得分:1)

import cv2
import numpy as np
from PIL import Image

img_file = "sample.jpg"
image = cv2.imread(img_file)

# USING NUMPY SLICE
red = image[:,:,2]
green = image[:,:,1]
blue = image[:,:,0]

# USING OPENCV SPLIT FUNCTION
b,g,r=cv2.split(image)

# USING NUMPY dsplit
[b,g,r]=np.dsplit(image,image.shape[-1])

# USING PIL 
image = Image.open("image.jpg")
r,g,b = image.split()