Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image only in R(Red) channel using python

Hi I'm very new in image processing, now I'm playing with image using python to get more insight. However, I'm in a little pickle with the understanding of the color channels.

In my opinion, a RGB image of a shape (400, 400, 3) means the image is made up of 3 channels and each channel has 400 * 400 pixels. The three pixels in different colors at the same position represent a whole pixel in the full image.

So the image[:, :, 0] represents the image in red channel. If I display it, it should give a image in totally red. I did so but the output is not ideal:

plt.figure(figsize=(15,5))
plt.imshow(im[:,:,0])  # im is the image

Sorry but I don't have enough credit to upload image yet, the output above is greenish, but is not totally green.

Then I took the idea from others that assign the im[:,:,0] to a zero array that has the same shape with the image. Then the output is totally red. The code is:

tmp_im = np.zeros(im.shape, dtype="uint8")
tmp_im[:,:,0] = im[:,:,0]
plt.figure(figsize=(15,5))
plt.imshow(tmp_im)

So I was thinking, the only difference between these two is the parameter of imshow() in the first code is 2D, which just contains elements in R channel. However, the second one is 3D, though elements in the other two channels are 0. Is this the reason of resulting in the different output?

Please correct me if any of my understanding is wrong.

Thank you so much in advance.

like image 508
Qiumin Zhang Avatar asked Oct 03 '18 17:10

Qiumin Zhang


People also ask

How do I remove the red channel from an image in Python?

We can remove red color from the image by putting the value of red color=0 in array. i removed the color red by making a numpy array of same shape as img with zeros and adding [1,1,0] to the zero array,so we get a array with each element as [1,1,0].

How do I split a color image into its 3 RGB channels Python?

Image. split() method is used to split the image into individual bands. This method returns a tuple of individual image bands from an image. Splitting an “RGB” image creates three new images each containing a copy of one of the original bands (red, green, blue).


1 Answers

Yes, correct. In the first case, you extract a 2D array from the image and imshow by default applies a standard colormap, viridis afaik, which in fact is greenish. This is appropriate for data arrays which are supposed to be displayed as a heatmap, but not for pictures.

On the other hand, a 3D array, i.e. an array with RGB values is automatically interpreted as an image by imshow. So if in this case only the first component is different to 0, you'll get a red picture.

However, you can force imshow to use a colormap of your choice for 2D arrays by the cmap kwarg:

plt.imshow(img_name, cmap=cm_name)

with cm_name being one of e.g. 'Greys' or 'Greys_r' to get a grayscale representation of the image (or the R-channel in your case). And of course there are also 'Reds' and 'Reds_r' available - guess what...
For a complete list just put a typo in the cm_name - the error message is quite verbose and informative:

Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r

To show the differences, here a sample plot:

enter image description here

created with the following code:

import imageio
import matplotlib.pyplot as plt
import numpy as np
im = imageio.imread('imageio:chelsea.png')
im_r = np.zeros(np.shape(im))
im_r[:, :, 0] = im[:, :, 0]
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].imshow(im[:, :, 0])
axs[1, 0].imshow(im[:, :, 0], cmap='Greys_r')
axs[0, 1].imshow(im[:, :, 0], cmap='Reds_r')
axs[1, 1].imshow(im_r.astype(int))
axs[0, 0].set_title('pure imshow of 2D-array (R-channel)')
axs[1, 0].set_title('imshow of 2D-array with cmap="Grey_r"')
axs[0, 1].set_title('imshow of 2D-array with cmap="Reds_r"')
axs[1, 1].set_title('imshow of 3D-array with coordinates 1 and 2 \n(i.e.: channels G and B) set to 0')
plt.tight_layout()
like image 136
SpghttCd Avatar answered Sep 19 '22 23:09

SpghttCd