0

I want to convert a color-coded image to grayscale. The colors in that image correspond to given values (e.g. black is 0 and red is 25) like on this color scale.

enter image description here

Is there a way to convert this image to grayscale and keep that color gradient, so that 0 on the scale remains black and 25 is shown as white?

I tried to convert it with matplotlib and also cv2 but ended up with grayscale images, that did not respect my given color gradient. I would appreciate an answer very much. Thank you!

7
  • Since your gradient is known, you'd need to write a function that maps colors in the input image to positions on that gradient. Commented Mar 29, 2021 at 13:22
  • Is this solely for the above gradient, or are there others of a different size and/or colours and with different start/end points, i.e. not 0..25 ? Commented Mar 29, 2021 at 13:24
  • I want to convert color-coded brain scans. In the images, low (blue) values show brain tissue that is well supplied with blood and high (red) values indicate areas of stroke. But the gradient and the range of values (0 up to 25) is consistent in all the images. Commented Mar 29, 2021 at 13:33
  • You say "black is zero" but then "blue is low"? Commented Mar 29, 2021 at 13:42
  • Are you expecting a floating point value between 0..25, or an integer? Commented Mar 29, 2021 at 13:43

1 Answer 1

1

Depending on the tools you use you can

  • read/convert the color-coded image as RGB,
  • convert RGB to grayscale

or

  • convert the colors in the gradient to grayscale,
  • read/convert the color-coded image using that grayscale palette.

The second approach is more efficient.


Update:

Upon reading your last comment (which should be in the original question), the options become

  • read/convert the color-coded image as RGB,
  • convert RGB to grayscale,
  • rescale by multiplying the pixel values by 10

or

  • convert the colors in the gradient to grayscale, rescaled to 0-255,
  • read/convert the color-coded image using that grayscale palette.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.