Page 227 - The Definitive Guide to Building Java Robots
P. 227
Preston_5564C06.fm Page 208 Friday, September 23, 2005 5:13 AM
208 CHAPTER 6 ■ VISION
private void init(String fileName, boolean toShow) throws Exception{
setTitle("ColorGram Calibration");
FileInputStream fis = new FileInputStream(fileName);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
currentImage = decoder.decodeAsBufferedImage();
// get important part of image, not background, which is white
currentImage = imageProcessor.threshold(currentImage, 0, 150, true);
// gets mean values
meanValues = imageProcessor.getMean(currentImage);
// used later
redAvg = meanValues[0];
greenAvg = meanValues[1];
blueAvg = meanValues[2];
// init panel
imagePanel = new ImagePanel(currentImage.getWidth(), ➥
currentImage.getHeight());
// set frame properties
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
setBackground(Color.BLACK); // gets image
setSize(currentImage.getWidth() + 8, currentImage.getHeight() + 30);
getContentPane().add(imagePanel, BorderLayout.CENTER);
if (toShow) {
setVisible(true);
show();
}
}
// processing called from optimize methods
private void doProcessing(ColorGram cg) {
// get maximum color ratio count for image and colorgram passed
int max = imageProcessor.colorRatioCount(currentImage, cg);
// if zero initialize count
if (maxCount == 0) {
maxCount = max;
}
// get threshold for colors to be counted
double maxThresh = maxCount * threshhold;
// if current color count greater than threshhold, set as best colorgram
if (max > maxThresh) {
currentImage = imageProcessor.colorRatio(currentImage, cg);
// since cg is changing and by reference
bestColorGram = (ColorGram) cg.clone();
}