Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify image color in java program?

Tags:

java

I have a folder with many images with different backgrounds. I have got requirement to sort these images on the basis of background color.

Can I make a java program to read the folder and each image file in there, and decide the image of each file? please share options.

like image 636
Vijay Shanker Dubey Avatar asked Jun 01 '26 23:06

Vijay Shanker Dubey


2 Answers

Yes, it is possible. You can load images with ImageIO.

BufferedImage img = ImageIO.read(imageFile);
int rgb = img.getRGB(x,y);
Color color = new Color(rgb);

But you have to create an algorithm that finds out which color is the backround color. It depends on the kind of images.

like image 175
Till Avatar answered Jun 03 '26 13:06

Till


So, not knowing what your images really look like, you may want to average as much of the background as you can to come up with a good representation of the background color.

I would consider a couple things:
* Read in the pixels of each of the four edges. If there's little variance in the pixel color, then you may be done, just take the average.
* Do the same, but also read in lines from the edge to the middle until you hit a pixel that has a rather different color than your running average. Do this for all edges.

Those would be the cheapest things that I can think of to cover variances in background color. Depending on the images you're working with, you may have to get fancier.

A BufferedImage should get you your image data.

Mark

like image 31
MStodd Avatar answered Jun 03 '26 11:06

MStodd