Getting the most frequently appearing color of an image
I am currently trying to develop a project for t-shirt color analysis for开发者_运维技巧 blind people. For this project, I should get the most frequently appearing colors or primary color tones in images to identify the color of t-shirt. I looked for the solution from the internet but I couldn't find a proper solution yet. I am using Java for my project.
The v7 palette support library includes the Palette class, which lets you extract prominent colors from an image. This class, found in android.support.v7.graphics, can extract the following colors:
- Vibrant
- Vibrant Dark
- Vibrant Light
- Muted
- Muted Dark
- Muted Light
To use the Palette class in your project, add the following Gradle dependency to your app's module:
dependencies {
...
compile 'com.android.support:palette-v7:21.0.0'
}
Usage:
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
// Do something with colors...
//You can select any of the colors.
Palette.Swatch swatch = palette.getVibrantSwatch();
if(swatch!=null){ // mind it, it can be null sometime.
int color = swatch.getRgb();
}
}
});
Get a bitmap and go through each pixel. Remeber the color of each one. Then make some averages - make close colors into one( pink, deep orange and red can become only red). Then compare the counts you get for each of basic colors(colors of rainbows).
Colorific from 99 designs has done most of the hard work here, including converting RGB to CIE lab colors to ensure that perceptually similar colors are bucketed together, as well as excluding background colors. It's in Python, and can be run from the command line. So you could use something like the following:
Runtime r = Runtime.getRuntime();
Process p = r.exec( ((URL) this.getClass().getResource("/python/colorific")).getFile() +" "+filename);
String out = CharStreams.toString(new InputStreamReader(p.getInputStream()));
精彩评论