I am trying to slice an image into RGB and I have a problem with plotting these images. I obtain all images from a certain folder with this function:
def get_im
I think matplotlib is just treating each channel (i.e., intensities) as a "heat map".
Pass a color map to the imshow function like so to tell it how you want it to color your image:
plt.imshow(image_slice, cmap=plt.cm.gray)
@mrGreenBrown in response to your comment, I'm assuming that the misc.imread
function you used is from scipy, i.e., scipy.misc.imread
. That function is no different from that of PIL
. See scipy.misc.imread docs. Thanks to @dai for pointing this out.
A single channel of any image is just intensities. It does not have color. For an image expressed in RGB color space, color is obtained by "mixing" amounts (given by the respective channel's intensities) of red, green, and blue. A single channel cannot express color.
What happened was Matplotlib by default displays the intensities as a heatmap, hence the "color".
When you save a single channel as an image in a format say JPEG, the function merely duplicates the single channel 3 times so that the R, G, and B channels all contain the same intensities. This is the typical behavior unless you save it in a format such as PGM which can handle single channel grayscale image. When you try to visualize this image which has the same channel duplicated 3 times, because the contributions from red, green, and blue are the same at each pixel, the image appears as grey.
Passing plt.cm.gray
to the cmap
argument simply tells imshow
not to "color-code" the intensities. So, brighter pixels (pixels approaching white) means there is "more" of that "color" at those locations.
If you want color, you have to make copies of the 3 channel image and set the other channels to have values of 0
.
For e.g., to display a red channel as "red":
# Assuming I is numpy array with 3 channels in RGB order
I_red = image.copy() # Duplicate image
I_red[:, :, 1] = 0 # Zero out contribution from green
I_red[:, :, 2] = 0 # Zero out contribution from blue
A related question from stackoverflow here.