OpenCV:眼睛的轮廓从面部ROI移开

时间:2019-04-04 20:26:40

标签: java opencv

我尝试只检测脸部矩形内的眼睛。实际上,只有在识别出人脸时才能检测到眼睛,但是相应的矩形却放错了位置:

enter image description here

绿色矩形正确捕捉到脸部。但是青色的矩形眼睛放错了位置。似乎它们源自坐标0,0,而不是源自矩形矩形的左上角。

    // detect faces
    this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,  new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size());


    Rect[] facesArray = faces.toArray();
    MatOfRect eyes = new MatOfRect();
    this.eyeCascade.load("resources/haarcascades/haarcascade_eye.xml");

    for (int i = 0; i < facesArray.length; i++) {
        Rect currentFace = facesArray[i];
        Imgproc.rectangle(frame, currentFace.tl(), currentFace.br(), new Scalar(0, 255, 0), 3);
        Imgproc.putText(frame, "face", currentFace.tl(), 2, 2, new Scalar(0, 255, 0));

        Mat faceROI = grayFrame.submat(currentFace);

        this.eyeCascade.detectMultiScale(faceROI, eyes, 1.1,2,0 | Objdetect.CASCADE_SCALE_IMAGE, new Size(this.absoluteFaceSize/5, this.absoluteFaceSize/5), new Size());
        Rect[] eyesArray = eyes.toArray();
        for (Rect eye : eyesArray) {
            Point tl = new Point(currentFace.x+eye.tl().x, currentFace.y+eye.tl().y);
            Point br = new Point(currentFace.x + eye.br().x, currentFace.y + eye.br().y);
            Imgproc.rectangle(faceROI, tl, br, new Scalar(0, 0, 255), 3);
            Imgproc.putText(faceROI, "Eye", tl, 2, 2, new Scalar(0, 0, 255));
        }
    }

0 个答案:

没有答案