Opencv - 检测眼睛是闭合还是开放 [英] Opencv - detecting whether the eye is closed or open

查看:191
本文介绍了Opencv - 检测眼睛是闭合还是开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi guys I am working on a project where we are trying to detect whether the eye is closed or open in a picture ... what we done so far is that we detected the face then the eyes and then we applied hough transform hoping that the Iris would be the only circle when the eye is open the problem is that when the eye is closed ... it produces a circle as well

这是代码:

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);
   }     
}

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

Any suggestions on how to make it more accurate?

推荐答案

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

Circular Hough transform is unlikely to work well in the majority of cases i.e. where the eye is partially open or closed. You'd be better off isolating rectangular regions (bounding boxes) around the eyes and computing a measure based on pixel intensities (grey levels). For example the variance of pixels within the region would be a good discriminator between open and closed eyes. Obtaining a bounding box around the eyes can be done quite reliably using relative position from the bounding box detected around the face using OpenCV Haar cascades. Figure 3 in this paper gives some idea of the location process.

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

这篇关于Opencv - 检测眼睛是闭合还是开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆