Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In skimage, how to get cmap from one generated image, and use it in another image?

If I plot two images with cmap="gray":

  • on Im1 (left), the tile with value 0.1 is light grey
  • On Im2 (right), the tiles are all defined with value 0.1, but there are all black

So how do I obtain the same light grey on Im2 ?

import matplotlib.pyplot as plt
import numpy as np

Im1 = np.array([[0.1,0.2],[0.02,0.002]])
plt.subplot(1, 2, 1)
plt.imshow(Im1, cmap="gray")

Im2 = np.array([[0.1,0.1],[0.1,0.1]])
plt.subplot(1, 2, 2)
plt.imshow(Im2, cmap="gray")

plt.show()

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you

>Solution :

You probably want to use the same Normalize object on both subplots, like this:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import Normalize

Im1 = np.array([[0.1,0.2],[0.02,0.002]])
Im2 = np.array([[0.1,0.1],[0.1,0.1]])

_min = min(t.min() for t in [Im1, Im2])
_max = max(t.max() for t in [Im1, Im2])
norm = Normalize(vmin=_min, vmax=_max)

plt.subplot(1, 2, 1)
plt.imshow(Im1, cmap="gray", norm=norm)

plt.subplot(1, 2, 2)
plt.imshow(Im2, cmap="gray", norm=norm)

plt.show()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading