Opencv - 检测眼睛是闭合还是开放

时间:2013-12-13 10:10:47

标签: java opencv hough-transform eye-detection

大家好我正在研究一个项目,我们正试图检测眼睛是闭合的还是在图片中打开......我们到目前为止所做的是我们检测到眼睛然后是眼睛然后我们应用了hough变换希望虹膜是眼睛打开时唯一的圆圈,问题是当眼睛闭合时......它也会产生一个圆圈

以下是代码:

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.imgproc.Imgproc;




public class FaceDetector {

    public static void main(String[] args) {



        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        System.out.println("\nRunning FaceDetector");

        CascadeClassifier faceDetector = new CascadeClassifier("D:\\CS\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
        CascadeClassifier eyeDetector = new CascadeClassifier("D:\\CS\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml");

        Mat image = Highgui.imread("C:\\Users\\Yousra\\Desktop\\images.jpg");
        Mat gray = Highgui.imread("C:\\Users\\Yousra\\Desktop\\eyes\\E7.png");

        String faces;
        String eyes;


        MatOfRect faceDetections = new MatOfRect();
        MatOfRect eyeDetections = new MatOfRect();

        Mat face;
        Mat crop = null;
        Mat circles = new Mat();
        faceDetector.detectMultiScale(image, faceDetections);

   for (int i = 0; i< faceDetections.toArray().length; i++){

            faces = "Face"+i+".png";

             face = image.submat(faceDetections.toArray()[i]);
             crop = face.submat(4, (2*face.width())/3, 0, face.height());
            Highgui.imwrite(faces, face);
             eyeDetector.detectMultiScale(crop, eyeDetections, 1.1, 2, 0,new Size(30,30), new Size()); 

             if(eyeDetections.toArray().length ==0){

                 System.out.println(" Not a face" + i);
             }else{

                 System.out.println("Face with " + eyeDetections.toArray().length + "eyes" );

                 for (int j = 0; j< eyeDetections.toArray().length ; j++){

                    System.out.println("Eye" );
                    Mat eye = crop.submat(eyeDetections.toArray()[j]);
                    eyes = "Eye"+j+".png";
                    Highgui.imwrite(eyes, eye);

                 }
             }
         }





             Imgproc.cvtColor(gray, gray, Imgproc.COLOR_BGR2GRAY);
             System.out.println("1 Hough :" +circles.size());
 float circle[] = new float[3];

             for (int i = 0; i < circles.cols(); i++)
             {
                     circles.get(0, i, circle);
                 org.opencv.core.Point center = new org.opencv.core.Point();
                 center.x = circle[0];
                 center.y = circle[1];
                 Core.circle(gray, center, (int) circle[2], new Scalar(255,255,100,1), 4);
                 }


             Imgproc.Canny( gray, gray, 200, 10, 3,false);  

             Imgproc.HoughCircles( gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 100, 80, 10, 10, 50 );
             System.out.println("2 Hough:" +circles.size());

             for (int i = 0; i < circles.cols(); i++)
             {
                     circles.get(0, i, circle);
                 org.opencv.core.Point center = new org.opencv.core.Point();
                 center.x = circle[0];
                 center.y = circle[1];
                 Core.circle(gray, center, (int) circle[2], new Scalar(255,255,100,1), 4);
                 }
             Imgproc.Canny( gray, gray, 200, 10, 3,false);  

             Imgproc.HoughCircles( gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 100, 80, 10, 10, 50 );
             System.out.println("3 Hough" +circles.size());

             //float circle[] = new float[3];

             for (int i = 0; i < circles.cols(); i++)
             {
                     circles.get(0, i, circle);
                 org.opencv.core.Point center = new org.opencv.core.Point();
                 center.x = circle[0];
                 center.y = circle[1];
                 Core.circle(gray, center, (int) circle[2], new Scalar(255,255,100,1), 4);
                 }

            String hough = "afterhough.png";
            Highgui.imwrite(hough, gray);
   }     
}

有关如何使其更准确的任何建议吗?

2 个答案:

答案 0 :(得分:2)

你可以检查circles.cols()值是否为2然后眼睛是开放的,如果值是0则关闭眼睛。如果circles.cols()的值从2变为0,您还可以检测眼睛的眨眼。如果眼睛闭合,Hough变换将不会检测到圆圈。

答案 1 :(得分:2)

在大多数情况下,即在眼睛部分打开或关闭的情况下,圆形霍夫变换不太可能正常工作。您最好隔离眼睛周围的矩形区域(边界框)并基于像素强度(灰度级)计算度量。例如,该区域内的像素的方差将是开眼和闭眼之间的良好鉴别器。使用OpenCV Haar级联从使用在脸部周围检测到的边界框的相对位置可以非常可靠地获得眼睛周围的边界框。本文中的图3给出了位置过程的一些概念。

http://personal.ee.surrey.ac.uk/Personal/J.Collomosse/pubs/Malleson-IJCV-2012.pdf