0

There is my picture,enter image description here

when I use following code in matplotlib, I can get a picture like this :

enter image description here

import cv2 
import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2hed
from matplotlib.colors import LinearSegmentedColormap

roi_select = cv2.imread(r"C:\Users\hutao\Desktop\cell.png")
roi_select_11 = rgb2hed(roi_select)
cmap_dab = LinearSegmentedColormap.from_list('mycmap', ['white','saddlebrown'])
plt.imshow(roi_select_11[:,:,0],cmap=cmap_dab)

But when I use OpenCV ,there is my code :

def get_mpl_colormap(cmap_name):
    cmap =   LinearSegmentedColormap.from_list('mycmap', ['white','saddlebrown'])
    sm = plt.cm.ScalarMappable(cmap=cmap)
    color_range = sm.to_rgba(np.linspace(0, 1, 256), bytes=True)[:,2::-1]
    return color_range.reshape(256, 1, 3)

roi_select = cv2.imread(r"C:\Users\hutao\Desktop\cell.png")
image_bgr = cv2.applyColorMap(roi_select[:,:,0],get_mpl_colormap('bwr'))
cv2.imshow('image with colormap', image_bgr)
cv2.waitKey()

I can't get the same picture,there is the OpenCV's picture enter image description here How can I get the same picture in OpenCV like figure2?

I'am a new hand in OpenCV , thank you for your answers!

Elyas Karimi
  • 292
  • 4
  • 11
Tao Hu
  • 287
  • 2
  • 12
  • It seems you have never used the argument gotten from `get_mpl_colormap` method. I mean `cmap_name`. Is it redundant? – Elyas Karimi Dec 01 '20 at 08:40
  • Here `roi_select_11 = rgb2hed(roi_select)` you convert RGB to HED, but the `roi_selected` is not RGB. In OpenCV the standard of channels are BGR. – Elyas Karimi Dec 01 '20 at 08:46
  • @ElyasKarimi For your second question,yes ,you are right,but I can't fix it. – Tao Hu Dec 01 '20 at 09:10
  • @ElyasKarimi For your first question,yes ,it's my falut,It is redundant. – Tao Hu Dec 01 '20 at 09:13
  • Why don't you convert them into BGR first? You can do that using `img_rgb = cv2.cvtColor(roi_select, cv2.COLOR_BGR2RGB)` – Elyas Karimi Dec 01 '20 at 09:18
  • @ElyasKarimi Yeah,RGB or BGR i can use `roi_select_11[:,:,0]` or `roi_select_11[:,:,2]` get it ,but I still can't get the figure2 . – Tao Hu Dec 01 '20 at 09:28
  • have you considered the difference between `cv2.imshow` and `plt.imshow`? This link explains the difference: https://stackoverflow.com/questions/38598118/difference-between-plt-show-and-cv2-imshow – Elyas Karimi Dec 01 '20 at 11:36

1 Answers1

0

For a quick fix, you can do

image_bgr = cv2.applyColorMap(255 - roi_select[:,:,0],get_mpl_colormap('bwr'))
cv2.imshow('image with colormap', image_bgr)
cv2.waitKey()
Anuj Karn
  • 42
  • 5
  • Using `255 - roi_select[:,:,0]` just look similar, but there are still some different from figure2.Thank you – Tao Hu Dec 01 '20 at 09:18