I found the opencv documentation to extract the single channels from an RGB image using the cv2.split (img) command, but it does not really return an image of the chosen channel. They all look like grayscale. Can someone help me? I enclose the photos.
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
or
b,g,r = cv2.split(img)
starting image = red channel extracted =
Is it a correct extraction? Where is the full red channel - img?
The extracted red channel may look like a grayscale image but it is correct. It is simply a 2D array with values in the range [0,255]
. To visualize a specific channel, you need to set the other channels to zero. So to show the red channel, the blue and green channels need to be set to zero.
import cv2
img = cv2.imread('1.jpg')
# Set blue and green channels to 0
img[:,:,0] = 0
img[:,:,1] = 0
cv2.imshow('red_img', img)
cv2.waitKey()
In either method, you are creating a grayscale image, so plotting it won't show the color of the channel it was derived from.
You can either use a colormap to add color to the image you display or convert it to an RGB image by setting the other channels to 0.
plt.imshow(r, cmap='Reds')
OR
img = np.stack([r, np.zeros_like(r), np.zeros_like(r)], axis=-1)
plt.imshow(img)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With