Counting the color "redish / blueish / greenish ..." in the image

I am working on what can count the number of blue / red / yellow / ... pixels in an image. So far I have this code as a test:

public class Main { /* Black: 0,0,0 White: 255, 255, 255 Red: 255, 0, 0 Orange: 255, 127, 0 Yellow: 255, 255, 0 Green: 0, 255, 0 Blue: 0, 0, 255 Indigo: 111, 0, 255 Violet: 143, 0, 255 */ static int blackCount = 0; static int whiteCount = 0; static int redCount = 0; static int orangeCount = 0; static int yellowCount = 0; static int greenCount = 0; static int blueCount = 0; static int indigoCount = 0; static int violetCount = 0; static int otherCount = 0; static int totalCount = 0; public static void main(String[] args) { try { String path = "src/colors.jpg"; BufferedImage image = ImageIO.read(new File(path)); int w = image.getWidth(); int h = image.getHeight(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { Color c = new Color(image.getRGB(x, y)); int red = c.getRed(); int green = c.getGreen(); int blue = c.getBlue(); countColor(red, green, blue); totalCount++; } } printColors(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private static void countColor(int red, int green, int blue) { if (red == 0 && green == 0 && blue == 0) blackCount++; else if (red == 255 && green == 255 && blue == 255) whiteCount++; else if (red == 255 && green == 0 && blue == 0) redCount++; else if (red == 255 && green == 127 && blue == 0) orangeCount++; else if (red == 255 && green == 255 && blue == 0) yellowCount++; else if (red == 0 && green == 255 && blue == 0) greenCount++; else if (red == 0 && green == 0 && blue == 255) blueCount++; else if (red == 111 && green == 0 && blue == 255) indigoCount++; else if (red == 143 && green == 0 && blue == 255) violetCount++; else otherCount++; } private static void printColors() { System.out.println("Black: " + blackCount); System.out.println("White: " + whiteCount); System.out.println("Red: " + redCount); System.out.println("Orange: " + orangeCount); System.out.println("Yellow: " + yellowCount); System.out.println("Green: " + greenCount); System.out.println("Blue: " + blueCount); System.out.println("Indigo: " + indigoCount); System.out.println("Violet: " + violetCount); System.out.println("Other: " + otherCount); System.out.println("Total: " + totalCount); } 

But you may notice a problem ... In RGB, the color "Red" is defined as (255, 0, 0). Therefore, an image containing a lot of red can return β€œ0” because the color used in the image is (254, 0, 0), not (255, 0, 0).

So, I really want to count not only pure red pixels, but also all the "red" pixels. I suppose there is an easier way to remove this than to write insanely long if (red = 255) if (red = 254), ... structure?

+4
source share
5 answers

You can convert the processed pixels into a more convenient color model, namely HSL or HSV .

Then it would be easier to determine the ranges of values ​​of the components belonging to certain groups (reddish / blue / etc.).

+4
source

I would like to try to determine which of the three constants is greater in your color. If the amount of red is greater than the amount of blue and the amount of green, you can assume that your color is β€œred”. The same thing happens with blue and green.

However, you can take into account some corner cases:

  • What do you do when the color is "grayish", i.e. all three constants are the same.
  • What should happen if you count the color (155, 154, 154)? The red constant is the largest, but the color is still gray. Maybe you should say that a color is a certain color if and only if the associated constant is greater than all the others plus a certain value?

xkcd gave a very interesting review on the topic of colors . I recommend that you read the details on how colors work, and may give you some hits on what values ​​make the colors "redish", "greenish", and "blueish". The results are very interesting, but show many interesting cases where the definition of color is nontrivial. Colors can be perceived differently by different people, under the influence of gender, blindness and many other factors.

In addition, the survey includes many color names for many RGB constants that you could use. And it also contains a map of color names!

I really believe that colors are a very interesting topic, however they are really complex, and you should get as much information as possible on this subject before deciding how to write code.

+4
source

You need to identify the reddish, bluish and greenish tint. Obviously, [254,0,0] should go through red. What about [254,5,5]? What about [200, 10, 10]?

It’s best to check the distance from the marked color (for example, with pure red: (255-r)^2 + (0-b)^2 (0-g)^2 ) and compare it with the threshold value that you consider is "effectively red "

And then do the same for the other flowers.

This method will also allow you to easily expand to additional colors (since you can simply check the distance from your color to another color).

Edit: after finding this question: Is there an easy way to compare how close the two colors are to each other , I would recommend converting to HSL (hue, saturation, lightness) and comparing its values ​​in an order based on some threshold. Check this question, there is a lot of good information.

+3
source

I saw a very close question some time ago about getting a color name from an RGB value. The solution was simpler when a color could be converted from RGB to HSV, for example, since hue is a color. After that, you can define some ranges for the hue and use them as color.

So, for example, a hue from 0 to 30 degrees (or any other measure) will be red, from 30 to 60 will be orange, and then yellow ....

Take a look at How do you get a shade of color #xxxxxx? or Display rainbow colors in RGB e.g.

And to get color names from a shade, wikipedia can help ...

+1
source

Instead of checking for a specific value of red, green, or blue, check the range

(you'll have to tweak this for syntax and computation - I'm not a Java encoder)

     private static void countColor (int red, int green, int blue) {
             if (red == 0 && green == 0 && blue == 0) blackCount ++;
             else if (red! = 0 && green == red && blue == red) whiteCount ++;
             else if (red! = 0 && green == 0 && blue == 0) redCount ++;
             else if (red! = 0 && green == (red / 2) && blue == 0) orangeCount ++;
             else if (red! = 0 && green == red && blue == 0) yellowCount ++;
             else if (red == 0 && green ==! = 0 && blue == 0) greenCount ++;
             else if (red == 0 && green == 0 && blue ==! = 0) blueCount ++;
             else if (red == (blue / 3) && green == 0 && blue! = 0) indigoCount ++;
             else if (red == (blue / 0.66) && green == 0 && blue ==! =) violetCount ++;
             else otherCount ++;
         }

If this works, it will check colors matching shades of red, green, blue, white, black, etc.

0
source

All Articles