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

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

Abhishek Karambelkar
1#
Abhishek Karambelkar Published in 2018-01-13 04:20:36Z

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)

Silencer
2#
Silencer Reply to 2018-01-13 04:48:35Z

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]
Kriti Sahu
3#
Kriti Sahu Reply to 2018-01-16 15:38:38Z
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
r,g,b=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()
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331703 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO