3

There are functions such as rgb2hsv, rgb2ind, rgb2lab and rgb2xyz in converting RGB colormap to another one - in Matlab R2014b where the colormap Parula is introduced. However, I cannot find any solution to convert the RGB colormap to Parula i.e. the current default colormap of Matlab. I can set the colormap to be with the figure, but not convert the image as follows. I started to think the benefits of Parula in contrast to HSV in one part of removing black and its neighbor colors in a picture, for example here.

Pseudocommand

im = imread('https://i.stack.imgur.com/cFOSp.png'); 
hsv = rgb2parula(im); % Pseudocommand similar for HSV: hsv = rgb2hsv(im);
imshow(hsv); 

which gives

enter image description here

which is one reason for the quantization noise in later stages of image processing and bad morphological removal, as indicated in the previous thread.

How can you map RGB to Parula in Matlab?

User-made Parulas

I think we need a customized Parula because the function rgb2parula is not available. Proposal here which a variant for Gnuplot about the colormap. How can you convert this piece of code to Matlab?

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • I think you should mention which version you're using. I believe Matlab changed their colour map/scheme for default plots back in the 2014a/b version. Please correct me if I misunderstood the question. – krisdestruction Apr 22 '15 at 19:35

1 Answers1

4

This is not really possible, because Parula does not contain all the possible RGB values. If you think about it, every Matlab colormap is just a subset of the RGB ensemble. In mathematical terms rgb2hsv is an isomorphism, but there cannot be such isomorphism between RGB and a colormap (even the hsv colormap, which does not take all possible HSV values).

Then for a given RGB triplet you can try to find the nearest RGB triplet inside the Parula colormap. Something like:

cm = parula(256);
RGB = rand(1,3);

d2 = (cm(:,1)-RGB(1)).^2 + (cm(:,2)-RGB(2)).^2 + (cm(:,3)-RGB(3)).^2;
[~, mi] = min(d2);

and cm(mi,:) will be the nearest color to RGB in Parula according to this metric. But of course, this is an imperfect solution.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ratbert
  • 5,463
  • 2
  • 18
  • 37
  • I think this is what he was getting at. Not a perfect conversion, but round to the closest colour. – krisdestruction Apr 22 '15 at 19:37
  • *(even the hsv colormap, which does not tacke all possible HSV values).* Do you mean that hsv colormap which does not tackle all possible RGB values? – Léo Léopold Hertz 준영 Apr 22 '15 at 19:50
  • 1
    @Masi: There are two reasons: first the `hsv` function creates a vector of RGB components where _only_ hue varies, the saturation and value being constant. For instance, it is easy to see that there is no white and no black in the outputs of `hsv`. Second, as hsv outputs a `n-by-3`array, well you can't map all the possibe hue values with just `n` color, right ? – Ratbert Apr 22 '15 at 19:54
  • Yes. - Can you give an example how you apply your Parula to the image `im` in Pseudocode? I did not get the example. – Léo Léopold Hertz 준영 Apr 22 '15 at 19:57
  • @Masi: Sure. The method in the solution is just to evaluate the "distance" between your color and the colors in your colormap. For this, I compute the sum of the quadratic difference for the three component (just as you would do for points with XYZ coordinates and use Pythagore's theorem) and then search for the minimal distance with `min`, well in this case the minimal squared distance which is the same. Is that clearer ? – Ratbert Apr 22 '15 at 20:03
  • You say HSV is an isomorphism of RGB mathematically. Why Parula cannot be isomorphism of RGB? There are also gabs in real line so no problem in not having all colors of RGB in Parula. – Léo Léopold Hertz 준영 Apr 22 '15 at 20:13
  • @Masi Sorry for being unclear. When I evoke HSV in the isomorphism analogy, i am refering to the general method to describe colors with hue-saturation-value parameters and not the matlab `hsv` function. The name of the `hsv` function is misleading, hence my comment on it. – Ratbert Apr 22 '15 at 20:24
  • Thank you for clarification! I added an example of optimized Parula in Gnuplot to the body. The function `rgb2parula` is not available officially so I think the only option is to do it. – Léo Léopold Hertz 준영 Apr 22 '15 at 20:41
  • I opened a new thread about a related problem here in reading Parula image into Matlab: http://stackoverflow.com/q/29990133/54964 – Léo Léopold Hertz 준영 May 01 '15 at 15:45