Page 218 - The Definitive Guide to Building Java Robots
P. 218
Preston_5564C06.fm Page 199 Friday, September 23, 2005 5:13 AM
CHAPTER 6 ■ VISION 199
Example 6-19. getHistogram();
public int[] getHistogram(BufferedImage bufImg) {
// Set up the parameters for the Histogram object.
int[] bins = { 256, 256, 256 }; // The number of bins.
double[] low = { 0.0D, 0.0D, 0.0D }; // The low value.
double[] high = { 256.0D, 256.0D, 256.0D }; // The high value.
// Construct the Histogram object.
Histogram hist = new Histogram(bins, low, high);
// Create the parameter block.
ParameterBlock pb = new ParameterBlock();
pb.addSource(bufImg); // Specify the source image
pb.add(null); // No ROI
pb.add(1); // Sampling
pb.add(1); // periods
// Perform the histogram operation.
PlanarImage output = (PlanarImage) JAI.create("histogram", pb, null);
// Retrieve the histogram data.
hist = (Histogram) output.getProperty("histogram");
// Print 3-band histogram.
int[] mean = new int[] {(int)hist.getMean()[0],
(int)hist.getMean()[1],(int)hist.getMean()[2]};
System.out.println("histogram2=" + mean[0] + "," + mean[1] + ","
+ mean[2]);
return mean;
}
Getting the mean values helps us compute the desired color of an image. From Example 6-17
with colorFilter(), knowing that you first want the red, and second the green, and third the blue
can help you optimize the filter to give you an exact color.
What I’ve found through experimentation is that it’s not so much the colors that make a
difference, but their relationship to each other that’s important. So instead, I’ll create a class
called a ColorGram that represents not only the colors, but also the ratio of those colors to one
another.
Code Objective
The objective here is to create a class that represents colors and their ratios to one another.
97022d2480fe4a63cfdfa123a6e70098