How to obtain an image's single colour channel without using the split function in OpenCV Python?

你离开我真会死。 提交于 2020-08-09 08:49:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!