How To Obtain An Image's Single Colour Channel Without Using The Split Function In Opencv Python?
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
Solution 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]
Solution 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()
Post a Comment for "How To Obtain An Image's Single Colour Channel Without Using The Split Function In Opencv Python?"