Example usage for org.opencv.core MatOfRect empty

List of usage examples for org.opencv.core MatOfRect empty

Introduction

In this page you can find the example usage for org.opencv.core MatOfRect empty.

Prototype

public boolean empty() 

Source Link

Usage

From source file:ch.zhaw.facerecognitionlibrary.Helpers.FaceDetection.java

License:Open Source License

public Rect[] getFaces(Mat img) {
    MatOfRect faces = new MatOfRect();
    List<Rect> facesList = null;
    float mRelativeFaceSize = 0.2f;
    int mAbsoluteFaceSize = 0;
    if (faceDetector != null) {
        // If no face detected --> rotate the picture 90 and try again
        angle = 0;/*from ww  w .  j a  v a  2  s.  c o  m*/
        for (int i = 1; i <= 4; i++) {
            int height = img.rows();
            if (Math.round(height * mRelativeFaceSize) > 0) {
                mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
            }
            faceDetector.detectMultiScale(img, faces, 1.1, 2, 2, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize),
                    new Size());
            // Rotate by 90
            if (faces.empty()) {
                angle = 90 * i;
                MatOperation.rotate_90n(img, 90);
            } else {
                facesList = faces.toList();
                // Check that each found face rectangle fits in the image, if not, remove it
                for (Rect face : facesList) {
                    if (!(0 <= face.x && 0 <= face.width && face.x + face.width <= img.cols() && 0 <= face.y
                            && 0 <= face.height && face.y + face.height <= img.rows())) {
                        facesList.remove(face);
                    }
                }
                if (!(facesList.size() > 0)) {
                    return null;
                }
                // Faces found with the current image rotation
                this.img = img;
                break;
            }
        }

    } else {
        Log.e(TAG, "Detection method is not selected!");
    }
    if (facesList != null) {
        return (Rect[]) facesList.toArray();
    } else {
        return null;
    }
}

From source file:ch.zhaw.facerecognitionlibrary.Helpers.FaceDetection.java

License:Open Source License

public Eyes getEyes(Mat img) {
    double halfWidth = img.cols() / 2;
    double height = img.rows();
    double[] values = new double[4];
    values[0] = 0;/*from w ww .  ja va2s.  c  om*/
    values[1] = 0;
    values[2] = halfWidth;
    values[3] = height;
    Rect rightHalf = new Rect(values);
    values[0] = halfWidth;
    Rect leftHalf = new Rect(values);
    MatOfRect rightEyes = new MatOfRect();
    MatOfRect leftEyes = new MatOfRect();

    Mat rightHalfImg = img.submat(rightHalf);
    rightEyeDetector.detectMultiScale(rightHalfImg, rightEyes);
    Mat leftHalfImg = img.submat(leftHalf);
    leftEyeDetector.detectMultiScale(leftHalfImg, leftEyes);

    if (rightEyes.empty() || leftEyes.empty() || rightEyes.toArray().length > 1
            || leftEyes.toArray().length > 1) {
        return null;
    }

    Rect rightEye = rightEyes.toArray()[0];
    Rect leftEye = leftEyes.toArray()[0];

    MatOfFloat rightPoint = new MatOfFloat(rightEye.x + rightEye.width / 2, rightEye.y + rightEye.height / 2);
    MatOfFloat leftPoint = new MatOfFloat(img.cols() / 2 + leftEye.x + leftEye.width / 2,
            leftEye.y + leftEye.height / 2);

    MatOfFloat diff = new MatOfFloat();
    Core.subtract(leftPoint, rightPoint, diff);
    double angle = Core.fastAtan2(diff.toArray()[1], diff.toArray()[0]);
    double dist = Core.norm(leftPoint, rightPoint, Core.NORM_L2);
    Eyes eyes = new Eyes(dist, rightPoint, leftPoint, angle);
    return eyes;
}

From source file:frclib.FrcFaceDetector.java

License:Open Source License

/**
 * This method is called to detect objects in the image frame.
 *
 * @param image specifies the image to be processed.
 * @param detectedTargets specifies the preallocated buffer to hold the detected targets.
 * @param detectedObjects specifies the object rectangle array to hold the detected objects.
 * @return detected objects, null if none detected.
 */// www  . j  a v a 2  s .c  o m
@Override
public MatOfRect detectObjects(Mat image, MatOfRect detectedObjects) {
    final String funcName = "detectedObjects";

    if (debugEnabled) {
        dbgTrace.traceEnter(funcName, TrcDbgTrace.TraceLevel.CALLBK, "image=%s,objRects=%s", image.toString(),
                detectedObjects.toString());
    }

    faceDetector.detectMultiScale(image, detectedObjects);
    if (!detectedObjects.empty()) {
        faceRects = detectedObjects.toArray();
    } else {
        faceRects = null;
        detectedObjects = null;
    }

    if (videoOutEnabled) {
        putFrame();
    }

    currImage = image;

    if (debugEnabled) {
        dbgTrace.traceExit(funcName, TrcDbgTrace.TraceLevel.CALLBK, "=%s",
                Boolean.toString(detectedObjects != null));
    }

    return detectedObjects;
}