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
34 private int[] pixelBuffer;
38 // public EyeDetector(Image image, Rectangle2D faceRect) {
39 public EyeDetector(Image image, double fx, double fy, double fwidth, double fheight) {
41 percent = 0.15 * fwidth;
42 Rectangle2D adjustedFaceRect = new Rectangle2D(fx + percent, fy + percent, fwidth - percent, fheight - 2 * percent);
43 // percent = 0.15 * faceRect.getWidth();
44 // Rectangle2D adjustedFaceRect =
45 // new Rectangle2D(faceRect.getX() + percent, faceRect.getY() + percent, faceRect.getWidth()
46 // - percent, faceRect.getHeight() - 2 * percent);
48 width = (int) adjustedFaceRect.getWidth() / 2;
49 height = (int) adjustedFaceRect.getHeight() / 2;
50 pixelBuffer = new int[width * height];
52 int startX = (int) adjustedFaceRect.getX();
53 int startY = (int) adjustedFaceRect.getY();
55 for (int y = 0; y < height; y++) {
56 for (int x = 0; x < width; x++) {
57 pixelBuffer[(y * width) + x] = (int) image.getPixel(x + startX, y + startY);
64 public Point detectEye() {
65 Point eyePosition = null;
66 float brightness = 255f;
67 for ( int y = 0; y < height; ++y) {
68 for ( int x = 0; x < width; ++x) {
69 int position = y * width + x;
70 int[] color = new int[] { (pixelBuffer[position] & 0xFF0000) >> 16,(pixelBuffer[position] & 0x00FF00) >> 8, pixelBuffer[position] & 0x0000FF };
71 // System.out.println("("+x+","+y+")="+color[0]+" "+color[1]+" "+color[2]);
72 final float acBrightness = getBrightness(color);
74 if (acBrightness < brightness) {
75 eyePosition = new Point(x + (int) percent, y + (int) percent);
76 brightness = acBrightness;
85 private static float getBrightness( int[] color) {
86 int min = Math.min(Math.min(color[0], color[1]), color[2]);
87 int max = Math.max(Math.max(color[0], color[1]), color[2]);
89 return 0.5f * (max + min);