0

I'm designing a program with java that compares pixels and their RGB values within a picture. For example I have one column of pixels and I want to assign a value to each pixel in which it ranks each pixel according to a scale.

Example Scale:

Dark green = 500 > Light Green =400 > Yellow =300 > Light Red=200 > Dark Red=100

I want to be able to attain/print these values and everything in between as in getting something like 430 when a pixel is a color between light green and dark green. I struggling to find a method that will allow me to compare these colors.

Ryan
  • 15
  • 3
  • And your question is...? – Turing85 May 02 '18 at 19:00
  • 1
    RGB is terrible for color comparison. Consider converting to [LAB](https://stackoverflow.com/a/5021831/223424), or at least to [HSB](https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#RGBtoHSB(int%2C%20int%2C%20int%2C%20float%5B%5D)). – 9000 May 02 '18 at 19:00
  • If you are using RGB values, the average color is the average of both R, both G, and both B values. See implementation of: https://stackoverflow.com/questions/25878029/smooth-color-interpolation-along-a-bresenham-line/25878225#25878225 with the value of P set at 0.5 – Compass May 02 '18 at 19:01
  • 2
    It seems like you just want to sort your colors by their hue value. Despite that you won't get good results when they vary in saturation or value. Comparing colors is generally a topic which cannot be solved. You might also want to have a look at delta e2000 algorithm. – TheWhiteLlama May 02 '18 at 19:01
  • @Compass: a shift R+10, G-10 gives a very different _tint_ than R-10, G+10, while keeping the average. – 9000 May 02 '18 at 19:02
  • Given what he wants, this is the best he can hope for targeting Java/RGB with no other provided Libs. – Compass May 02 '18 at 19:08

1 Answers1

0

I have needed something similar to this in a couple of programs I have made. My solution however was to compare R G and B values of two pixels individually, find the percent of difference relative to the 256 maximum value of each, and then average or add the differences across all three depending what I needed to do with the information.

However my implementation I wrote today seems flawed and for some reason always returns 100% similarity. I will share the line of code though since you can get the jist of what it should do.

public static int compareRGB(int rgb1, int rgb2){

    int[] rgb1Colors = new int[3];
    int[] rgb2Colors = new int[3];

    rgb1Colors[0] = getRed(rgb1);
    rgb1Colors[1] = getGreen(rgb1);
    rgb1Colors[2] = getRed(rgb1);

    rgb2Colors[0] = getRed(rgb2);
    rgb2Colors[1] = getGreen(rgb2);
    rgb2Colors[2] = getBlue(rgb2);

    return (100-(((Math.abs(rgb1Colors[0]-rgb2Colors[0])+Math.abs(rgb1Colors[1]-rgb2Colors[1])+Math.abs(rgb1Colors[2]-rgb2Colors[2]))/768)*100));
}
Goel Nimi
  • 37
  • 7