0

I have an image with colors who corresponds to values following a gradient.

My gradient is like that :

gradient image

My image look like this :

sample of my image

I search a way to convert a pixel color in my image to find the correct value from this gradient (in C#). For example : a black pixel has a value of 320, a white pixel has a value of 16000, ...

I found this post but the answer is the reverse of what I'm looking for... Converting int value to a color in a gradient

Can anyone offer some help please?

UPDATE:

I find/make this code to reconize the closest color but I'm not sure about the precision to find values...

    private void pictureBox1_Click(object sender, MouseEventArgs e)
    {
        var zone_data = img.GetPixel(e.Location.X, e.Location.Y);

        var colors = new List<Color> { Color.Black, Color.SaddleBrown, Color.Purple, Color.Blue, Color.Cyan, Color.Green, Color.Yellow, Color.Orange, Color.Red, Color.Salmon, Color.LightGray, Color.White };

        Console.WriteLine("Nearest color : " + colors[closestColor(colors, zone_data)].ToString());
    }
    public int closestColor(List<Color> colors, Color target)
    {
        var colorDiffs = colors.Select(n => ColorDiff(n, target)).Min(n => n);
        return colors.FindIndex(n => ColorDiff(n, target) == colorDiffs);
    }
    public int ColorDiff(Color c1, Color c2)
    {
        return (int)Math.Sqrt((c1.R - c2.R) * (c1.R - c2.R)
                               + (c1.G - c2.G) * (c1.G - c2.G)
                               + (c1.B - c2.B) * (c1.B - c2.B));
    }
NicoCraft
  • 91
  • 1
  • 11
  • How are you rendering your gradient? Can you not use the same principle to work out the value? – Lee Taylor Aug 02 '21 at 21:47
  • I do not manage the gradient. I am getting an image from a server that uses this gradient. – NicoCraft Aug 02 '21 at 21:51
  • It sounds like you are trying to convert an image into some maping data. Can you get the original mapping data as JSON/XML/CSV from that same mapping service?: – Neil Aug 02 '21 at 22:01
  • A simple way would be to create a lookup of RGB values, using your gradient to do this. So, start at the left, move by one pixel right, recording the RGB and integer value as you go. When you pick a colour from your chart you will need to find the RGB value that is _closest_ to the ones in your lookup. – Lee Taylor Aug 02 '21 at 22:03
  • You need a mapping but if you can't get from the source you need to make it yourself, filling a Dictionary or maybe . – TaW Aug 02 '21 at 22:16
  • How the *closest* color of my pixel from my gradient will give me the *exact* value? – NicoCraft Aug 02 '21 at 22:27
  • You need to have a formula for those values, which looks simple: For N steps each value seems to be n*(15680 / N). However the colors are not evenly distributed; so if that gradient is a given, I think you need to pick each of those colors one by one. You can easily write a helper app to do that: display the gradient and for each mouse click GetPixel the color and put it in a color list and store the list. Now you can either do a index of color in the list to get at the `n` or, if really really needed, do a search for closest color, Look up color distance for that. – TaW Aug 03 '21 at 08:27

0 Answers0