2 * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
4 * This file is part of LEA.
6 * LEA is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Lesser General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option) any
11 * LEA is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with LEA. If not, see <http://www.gnu.org/licenses/>.
21 * No description given.
23 * @author Florian Frankenberger
29 private int[] pixelBuffer;
33 public EyeDetector(Image image, Rectangle2D faceRect) {
35 percent = 0.15 * faceRect.getWidth();
37 new Rectangle2D(faceRect.getX() + percent, faceRect.getY() + percent, faceRect.getWidth()
38 - percent, faceRect.getHeight() - 2 * percent);
40 width = (int) faceRect.getWidth() / 2;
41 height = (int) faceRect.getHeight() / 2;
42 pixelBuffer = new int[width * height];
44 int startX = (int) faceRect.getX();
45 int startY = (int) faceRect.getY();
47 for (int y = 0; y < height; y++) {
48 for (int x = 0; x < width; x++) {
49 pixelBuffer[(y * width) + x] = (int) image.getPixel(x + startX, y + startY);
55 public Point detectEye() {
56 Point eyePosition = null;
57 float brightness = 255f;
58 for (int y = 0; y < height; ++y) {
59 for (int x = 0; x < width; ++x) {
60 final int position = y * width + x;
62 new int[] { (pixelBuffer[position] & 0xFF0000) >> 16,
63 (pixelBuffer[position] & 0x00FF00) >> 8, pixelBuffer[position] & 0x0000FF };
64 // System.out.println("("+x+","+y+")="+color[0]+" "+color[1]+" "+color[2]);
65 final float acBrightness = getBrightness(color);
67 if (acBrightness < brightness) {
68 eyePosition = new Point(x + (int) percent, y + (int) percent);
69 brightness = acBrightness;
77 private static float getBrightness(int[] color) {
78 int min = Math.min(Math.min(color[0], color[1]), color[2]);
79 int max = Math.max(Math.max(color[0], color[1]), color[2]);
81 return 0.5f * (max + min);