-2

everyone,

I am trying to convert a height colormap image (RGBA) into a 2D matrix so we can do something about it. I thought this would be a well-developed procedure but I cannot find any existing function to do this.

Here is the same of my colormap, red means a higher area (peak), blue means the lower area (valley), it ranges from (blue) -10 to (red) 10 um

The image is a surface topography of a milled surface. We want to get the roughness Ra by taking the average (arithmetical mean height) of the matrix. We also have the measurement of the Ra so we can compare to see if our calculations are correct.

I tried to convert the rgb to hsv and wish to find some relation between the height and the hue value (I do convert hue value to -10 to 10). But it seems the calculated Ra is way off the measurement. Also, I have already cut off the edges when I calculate the image roughness.

May I ask if anyone has an idea about this issue? Python or matlab preferred but other platforms/languages are fine too.

The RGBa image is not marked relatively, and here is the color bar for the image. Color scale

Five
  • 1
  • 1
  • 1
    You need the exact color scale used to create the image.There are all kinds of ways to get from blue/low to red/high, but if you aren't using the exact 1:1 (color:height) scale then you'll calculate incorrect results. You'll also need the absolute distance between highest and lowest points on the image if the color scale is relative. You're missing too much information if you're just looking at the image. – Woodford Sep 28 '21 at 15:59
  • Thank you for the reply! Yes, the color scale is not on the image. Since we need to use the image for analysis so I didn't put it there, but we do have a color bar listed somewhere else. The image is scaled from -10 to 10, the display color is not relative. May I ask how should I use that color scale? – Five Sep 28 '21 at 16:03
  • Then simply replace each pixel's RGBa value with the corresponding height value from the color scale. – Woodford Sep 28 '21 at 16:07
  • Do you have any examples? I thought about it in the beginning, but I do not know how to map m*n*4 matrix (RGBa image) to j*k matrix (height). I do not want to convert pixel by pixel as we have more than 1000+ images, I am afraid it will take forever. – Five Sep 28 '21 at 16:10
  • You're converting an _m*n*4_ matrix to an _m*n*1_ matrix and you have to do that conversion pixel by pixel. Any other method involving more complicated image processing will take **much** longer and produce more imprecise results. – Woodford Sep 28 '21 at 16:19

1 Answers1

0

Interesting question. It turns out that Matlab's rgb2ind() function can do 95% of the work. This answer is substantially based on this Matlab Central post: https://www.mathworks.com/matlabcentral/answers/72876-comparing-colors-against-palette#comment_144421 (there are a number of other techniques described in various Matlab Central posts, and that one, which may have various advantages; but this one is pretty simple and requires no toolboxes)

First you need to convert the colorbar into a color map. You can do this with the [X,cmap] = rgb2ind(RGB,Q) syntax, where:

  • RGB is the color image of the colormap (trim off the white edges in your image editor of choice first)
  • Q is the number of colors to extract (16 in your case; you may need to play with this a little to handle some of the dithering in the image)
  • X is the colorbar image, converted into indexed colors (which you can use to check the order of the colors in the colormap)
  • cmap is the colormap

If the colormap is not in the correct order (i.e. the one that corresponds to the order of the values), you will need to adjust the order of the matrix. This is the one somewhat manual step, but you only have to do it the once. You may find this Mathworks blog post on plotting color swatches helpful for this: https://blogs.mathworks.com/steve/2020/03/10/how-to-display-color-swatches/

Now that you have the colormap, you can convert each image into indexed values, using the X = rgb2ind(RGB,inmap) syntax, where:

  • RGB is the color image of the data
  • inmap is the colormap you got before
  • X is the data image, converted into indexed colors
sentiment
  • 91
  • 2