Reputation:
I'm trying to get the red channel color space of an image, currently the way I'm doing it, I get a grayscale
image:
img = img[:,:,2]
But I want an image like this:
Above, the top image is the red channel color space image, and the bottom one is the original image. What exactly is being done to achieve this image?
I've also tried
img[:,:,0] = 0
img[:,:,1] = 0
But the result obtained is not as desired. Here's an article on red channel color space: https://en.wikipedia.org/wiki/RG_color_space
Upvotes: 6
Views: 6540
Reputation: 1010
Your second suggestion should throw away the blue and green colors and give you a "red channel image". If you want a RG
(red-green) color space image, throw away only the blue channel:
img[:,:,0] = 0
But the example image you posted doesn't illustrate that, as the resulting image has information left in all three channels. My guess is that it was produced with a "colormap", where different colors represent different values of red in the original image. Such a mapping can look any way you like, so it's not easy to reconstruct it from your example images
Upvotes: 1
Reputation: 107287
Actually, your expected output image is not the red channel color space of the original one. It's sort of a COLORMAP that has been applied on input image. The good news is that OpenCV come up with multiple built-in colormaps. The bad news is that your expected output cant be generate by OpenCV's built-in colormaps. But don't give up, you can map the colors using a custom lookup table using cv2.LUT()
function.
For better demonstration here are some examples with your image:
img = cv2.imread('origin.png')
im_color = cv2.applyColorMap(img, cv2.COLORMAP_HSV)
cv2.imshow('mapped_image', im_color)
# cv2.imwrite('result.png', im_color)
cv2.waitKey(0)
Here are all the OpenCV's COLORMAP's:
print [sub for sub in dir(cv2) if sub.startswith('COLORMAP_')]
['COLORMAP_AUTUMN', 'COLORMAP_BONE', 'COLORMAP_COOL', 'COLORMAP_HOT', 'COLORMAP_HSV', 'COLORMAP_JET', 'COLORMAP_OCEAN', 'COLORMAP_PINK', 'COLORMAP_RAINBOW', 'COLORMAP_SPRING', 'COLORMAP_SUMMER', 'COLORMAP_WINTER']
An example for mapping the colors using a custom lookup table using cv2.LUT()
:
table = np.array([( i * invert_value) for i in np.arange(256)]).astype("uint8")
cv2.LUT(image, table)
Upvotes: 1