0

TL;DR

I have a RGB image (i.e ndarray of shape (nlines, ncolumns, 3)) whose colors are the ones of the HSV colormap (matplotlib.cm.hsv)

How do I transform such RGB image so that the values correspond to a different colormap?

MORE DETAILS

I have two 2D numpy arrays that represent a phase (in -pi, pi) and magnitude of some data. I want to have the phase values 'modulated' by the magnitude, so I create a matrix with like this:

HSV_matrix[..., 0] = phase_scaled # rescaled in (0,1)
HSV_matrix[..., 1] = np.ones_like(phase_scaled)
HSV_matrix[..., 2] = magnitude_scaled # rescaled in (0,1)

which means that the phase is associated to the HUE and the magnitude to the VALUE.

Then I input such matrix to hsv_to_rgb():

 rgb_image = matplotlib.colors.hsv_to_rgb(hsv_matrix)

rgb_image is now a ndarray of shape (nlines, ncolumns, 3) and represent the RGB values of my image where the colors are associated to the phase and the brightness to the magnitude.

At this point the colormap is already defined because, by definition, the values of rgb_image indicate the Red Green and Blue values for every pixel.

After hsv_to_rgb, the colormap of rgb_image is matplotlib.cm.hsv, hence to produce a consistent colorbar:

cmap = cm.ScalarMappable(cmap=cm.hsv, norm=plt.Normalize(vmin=hmin, vmax=hmax))
plt.colorbar(cmap)

where hmin and hmax are the min and max value of whatever I have in my HUE (hsv_matrix[...,0]).

NOTE: Matplotlib imshow ignores the cmap parameter if the input is RGB(A) [1], hence I cannot change the colormap with imshow

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

Robyc
  • 379
  • 1
  • 13
  • I'm stuck at the first line! If you are talking about a Numpy `ndarray` they don't have colormaps.... I'm confused. – Mark Setchell Jul 16 '19 at 12:47
  • I edited the question trying to clarify my goal and my problem. Hope is more clear now. – Robyc Jul 16 '19 at 13:52
  • Thank you. My understanding is improving :-) and I can now follow you up to *"What is the colormap I get..."* Why do you think you get a colormap from converting an HSV array to an RGB array? – Mark Setchell Jul 16 '19 at 13:58
  • by colormap I mean the mapping between values and colors. When I display the image I see colors...which have to be associated with some values. What I want to know is exactly this association. If everything works well, the color variation within the plotted image are associated with the HUE (the phase, in my case). – Robyc Jul 16 '19 at 14:06
  • the OP of the following question seems to confirm my guess that I just need to get the HSV colormap from matplotlib (or wherever) and adjust the min and max value properly https://stackoverflow.com/questions/45626482/how-can-i-add-a-2d-colorbar-or-a-color-wheel-to-matplotlib?rq=1 – Robyc Jul 16 '19 at 14:15
  • I edited the question again to reflect what I know and what I still don't. What I don't know is "how do I change the color mapping represented by the values in the RGB image. – Robyc Jul 17 '19 at 10:31
  • The problem is that you need two color bars: one to associate hue to your phase, and one to associate intensity to your magnitude. You could use a 2D color bar instead, to demonstrate the full range of output colors. – Cris Luengo Jul 17 '19 at 12:34
  • I don't need to. In general yes but in this moment is more important that I switch from a cyclic colormap to something like a diverging one: not for the phase, where a cyclic colormap is perfect but for other non-periodic data that I use as HUE. – Robyc Jul 17 '19 at 13:04
  • But you're right: in general a 2D colorbar would be cool to show both phase and magnitude at the same time. This is shown here: https://stackoverflow.com/questions/45626482/how-can-i-add-a-2d-colorbar-or-a-color-wheel-to-matplotlib?noredirect=1&lq=1 – Robyc Jul 17 '19 at 13:08
  • and also here: https://stackoverflow.com/questions/41966600/matplotlib-colormap-with-two-parameter?noredirect=1&lq=1 – Robyc Jul 17 '19 at 13:13

0 Answers0