需要帮助解决使用Java和OpenCV进行眼睛检测时的异常处理问题

时间:2014-08-24 07:47:11

标签: java opencv

我试图在Java中使用OpenCV来检测眼睛,它在某些时候运行得非常好,但在此之后发生异常" ArrayIndexOutOfBound"。我在JPanel中运行程序,不断检测面部和眼睛。眼睛和面部正确检测。所以我猜代码是正确的,只需要改变一些东西来解决异常。 如何摆脱它?

代码如下所示:

 public Mat eyeDetector(Mat image , MatOfRect facedetections)
 {
    // XML Files needed for Detection

CascadeClassifier eye_cascade= new  CascadeClassifier("other/haarcascade_eye_tree_eyeglasses.xml");

    MatOfRect eyedetections=new MatOfRect();

    Rect[] facesArray = facedetections.toArray();

    for (int i = 0; i < facesArray.length; i++)
    {     
         Mat faceROI = image.submat(facesArray[i]);

         //-- In each face, detect eyes

         eye_cascade.detectMultiScale(faceROI, eyedetections);

         Rect[] eyesArray = eyedetections.toArray();

         System.out.println("Eyes Detected:" + eyesArray.length);

         for (int j = 0; j < eyesArray.length; j++)
         {
            //System.out.println("for loop");

            Point center1 = new Point(facesArray[i].x + eyesArray[i].x + eyesArray[i].width * 0.5, facesArray[i].y + eyesArray[i].y + eyesArray[i].height * 0.5);
            int radius = (int) Math.round((eyesArray[i].width + eyesArray[i].height) * 0.25);
            Core.circle(image, center1, radius, new Scalar(255, 0, 0), 4, 8, 0); 
         }
       }
    return image;
}

public static void main(String[] args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    new FaceDetection().faceDetector();   
}

//Face Detection Method
void faceDetector()
{
    int x=0,y=0;
    //int frameno = 0;
    FacePanel panel = new FacePanel();
    panel.initialize();
    VideoCapture camera = new VideoCapture(0);

 //Opens the camera. 0 for in built camera
    Mat image = new Mat(); //creates matrix for image
    BufferedImage buffimage = null;

    //Checks whether camera is opened or not
    if(camera.isOpened())
    {
        while(true)
        {
            //Captures the image from camera
            if(camera.read(image))
            {
                //frameno++;
                //if((frameno % 5) == 0)
                //{
                    //Class for detecting face. Provided by OpenCV
                    CascadeClassifier FaceDetection = new CascadeClassifier("other/lbpcascade_frontalface.xml");

                    MatOfRect faceDetections = new MatOfRect();

                    //This Method detects faces
                    FaceDetection.detectMultiScale(image, faceDetections);

                    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

                    //Draw a green rectangle around face
                    for (Rect rect : faceDetections.toArray())
                    {
                        x=rect.x;
                        y=rect.y;
                        Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                                new Scalar(0, 255, 0));
                    }

                    //System.out.println(x+y);
                    Mat eye_detect = new Eye_Detection().eyeDetector(image, faceDetections);
                    buffimage = new Convert().matToBuff(eye_detect);
                    panel.display(buffimage , x , y); 
                //}
            }
        }

1 个答案:

答案 0 :(得分:1)

问题在于抛出异常的声明

for (int i = 0; i < facesArray.length; i++)
{
    ...
    for (int j = 0; j < eyesArray.length; j++)
    {
        ...
        Point center1 = new Point(
            facesArray[i].x + eyesArray[i].x + eyesArray[i].width * 0.5, 
            facesArray[i].y + eyesArray[i].y + eyesArray[i].height * 0.5);
    ...

您用于eyesArray的下标应为j而不是i