0

I imported a single channel image (from a .mat) file. When I use plt.imshow() for the image, the image looks distorted...kind of like inverted hues? Not sure. I think it's something trivial, but I'm unable to understand what exactly is happening. Some have suggested changing from BGR to RGB but that would be valid for RGB images, not single channel ones.

example of how i'm loading my image file -

import scipy as sp
from scipy.io import loadmat

red=sp.io.loadmat('red.mat')['red']

red is a dictionary from which i extract the value of the key red whose value is essentially the color intensity matrix.

plt.imshow(red)

yields this.

funny thing is that red, blue and the green channels they all have the same colour hues. What am I missing?

Axiumin_
  • 1,751
  • 2
  • 14
  • 20
  • Could you perhaps provide us with the original image to compare? – Axiumin_ Sep 06 '19 at 22:13
  • The original image is a .mat file (array) ..I don't have the original 'red' image. Just the RGB version of it. –  Sep 06 '19 at 22:16
  • You only want to show the red channel, right? – Axiumin_ Sep 06 '19 at 22:17
  • Yes, I understand I can do that if I have the original RGB image, but here I am just importing an array which is supposedly just the channel red. So essentially I'm just importing the red channel and using plt.imshow() but I end up getting this weird color map. –  Sep 06 '19 at 22:18
  • you could try creating a full RGB image, but making the G and B values 0 – Axiumin_ Sep 06 '19 at 22:21
  • i dont think that should be required, i mean that might work - afaik its supposed to be a simple load and show –  Sep 06 '19 at 22:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199096/discussion-between-axiumin-and-aditya-das). – Axiumin_ Sep 06 '19 at 22:25

2 Answers2

0

Imshow is probably using a default colormap.

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.imshow.html

(indeed the documentation points to a default called "viridis")

Use the 'cmap' parameter to specify a colormap (see the list in the following link).

https://matplotlib.org/3.1.1/tutorials/colors/colormaps.html

dwjbosman
  • 770
  • 7
  • 23
0

This has thrown me off in the past! From one of the tutorials on the matplotlib documentation:

Now, with a luminosity (2D, no color) image, the default colormap (aka lookup table, LUT), is applied. The default is called viridis. There are plenty of others to choose from. plt.imshow(red, cmap="hot")

So, for any single-channel image, the default false color colormap is utilized, instead of showing as greyscale.

Community
  • 1
  • 1
mgrollins
  • 583
  • 2
  • 8
  • ugh, okay I see. I did glance over that, but is there anyway to know the best color map? –  Sep 06 '19 at 22:40
  • What is the purpose of the visualization? Maybe force grayscale? There is some discussion here of how/why to choose a colormap: https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html Grayscale: https://stackoverflow.com/questions/3823752/display-image-as-grayscale-using-matplotlib – mgrollins Sep 06 '19 at 22:48
  • I just want to check if I imported my data correctly. All I need to do is import three color channels and do a simple color alignment :) I dont need it to be grayscale, I just want it to be red. Since I imported red. –  Sep 06 '19 at 22:54
  • OK, there is a "reds" colormap or maybe the "gist_heat" depending on if you want a level of 0 to be white or black. `plt.imshow(red, cmap='reds', vmin=0, vmax=255) – mgrollins Sep 06 '19 at 23:02
  • no there isn't a reds colormap, it says its not recognized. Doing it this way obviously isn't the solution. There must be an easier way. I'll keep looking. –  Sep 06 '19 at 23:05
  • Oh, sorry, that is probably my fault! The color map is "Reds" with a capital R – mgrollins Sep 06 '19 at 23:08