Reproduce color palette from an image containing that color palette (IVIS machine)

51 Views Asked by At

I would like to know if that is possible to reproduce a color palette from an image.

Here is an example. Pages 1 and 3 show a rainbow palette in the figure legend.

The software is proprietary and has very little room for customization. I would like to regenerate the legend by R (or any other programming language, e.g. python) but retain the scale/ range of the colors.

I recognize that some color picker tools are available. However, the resolution of the palette is quite low, and often 3-4 colors are displayed in the same row. I am not sure which color I should pick in the raster image.

2

There are 2 best solutions below

0
jooleer On

You can use OpenCV (cv2) library and export the color data as a histogram. It's also possible to apply a mask to a certain area of the image to extract the color data from that part.

For examples/documentation you can find more here: Histograms - 1 : Find, Plot, Analyze and this one might suite you a bit more where it shows how to create 2D histograms: Histograms - 3 : 2D Histograms

Hope this helps!

2
Seth On

Base R has a rainbow palette, which is similar to what is shown in the linked PDF

A better option might be the turbo color map in the viridis package, which transitions more gradually across the visible spectrum.

This PDF is a great general reference to colors in R, and may help you construct the gradient you're aiming to replicate.

In ggplot2, you can create these sorts of palettes using scale_gradient (link).

library(ggplot2)
library(scales)

  ggplot(mtcars, aes(factor(cyl), disp, fill = mpg)) +
  geom_col() +
  scale_fill_gradientn(colors = c(muted('red'), 'red', 'yellow1'))

Created on 2023-04-19 with reprex v2.0.2