0

I am new here and I need some help.

I have a gray image, and I need to colour it using Python.

This is the kind of images I have: enter image description here

And I need to transform it to be like the images that can be plot by using matplotlib ColorMap "CMRmap" like this one and save it:

enter image description here

Thank you in advance for helping me.

Shawn Mathew
  • 2,198
  • 1
  • 14
  • 31
Wassim Bouatay
  • 183
  • 2
  • 3
  • 10
  • Welcome to stack overflow! Unfortunately we are not a code writing service, and we ask, at the minimum, that you show some code for what you've tried based on your own research, and what went wrong with your attempts – G. Anderson Jul 18 '19 at 15:16
  • you don't need to transform the image, just display it with the correct colormap – Miki Jul 18 '19 at 15:51
  • Use matplotlib.pyplot to display your image. It will color code automatically or you can select other color tables or create your own. – fmw42 Jul 18 '19 at 16:40
  • Actually i need to save the images, i already know how to display it this way. – Wassim Bouatay Jul 19 '19 at 08:48
  • This should help... https://stackoverflow.com/a/31546410/2836621 – Mark Setchell Jul 19 '19 at 09:16

2 Answers2

2

Sounds like you've figured out the colormap part, but not the saving. Building on Shawn's answer, if you want to save the figure, make a call to plt.savefig() instead of plt.show(). Then pass the path you want to save it to as an argument.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread(r"path\to\img", 0)
plt.imshow(img, cmap='CMRmap')
plt.savefig("\path\to\output\file")

Hope this helps!

1

Expanding on @Miki's comment, you simply need to use a colormap. The colored image shows the CMRmap colormap.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread(r"path\to\img", 0)
plt.imshow(img, cmap='CMRmap')
plt.plot()
plt.savefig('foo.png')

Output:

enter image description here

Matplotlib lists all the colormaps here

Edit: updated answer with OP's clarification.

Shawn Mathew
  • 2,198
  • 1
  • 14
  • 31