问题
I'd like to highlight a hand for real-time gesture recognition. I observed that the image of a hand gets highlighted differently for different colour channels using the cv2.imsplit function. But this split function is very costly in terms of time. I'm not able to perform the same functionality using Numpy indexing (as given on the official page)
回答1:
You can use numpy's slice:
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]
回答2:
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()
来源:https://stackoverflow.com/questions/48236881/how-to-obtain-an-images-single-colour-channel-without-using-the-split-function