简体   繁体   中英

Is there any way to find out the majority color in an image file in java?

I want to find out the majority color in an image file and set it to background of a swing frame. What should I do?

you need to extract RGB percentage value from your Image for this some algorithm is there which give RGB value. And you also need to divide your whole image into some parts (example Nine square) and then get the RGB value and by this way you get the color which is more in your Image. May be this will help you.

You can yourself do this by traversing each pixel in image and then getting color for that. and then increment counter for each color found.

It all comes down to efficiency. You could literally read every RGB value of the image, and find the Mode, but that would take a long time.

Rather, what I would do is simply split the image into parts, lets say 100 Squares or something. We take the value in the centre of each square, and whichever appears the most is PROBABLY the most common color. Yes, this is sort of risky, because what if the image just happens to have a less used color in ALL the pixels you evaluated. The best thing to do would be to first of all, have a range, so if you get similar rgb's like (0,2,0) and (0,0,0) you simply treat them as the same.

The next thing you should do, is take about 10% of the image as a sample. It's very likely you will get the proper answer. So, if the image is 500px, use 50px as a sample, evenly distributed along the picture.

This is a trade off between accuracy and speed, because checking the whole image would take a very long time, and doesnt make sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM