opencv人脸识别,得到"边界框的坐标"围绕图片

时间:2015-08-11 03:11:04

标签: java opencv coordinates

今天我在java中尝试使用opencv的项目,我正在关注this代码。我修改它以使用opencv 3.0.0。基本上我试图从文件夹中取出一堆图像并裁剪出面部识别找到面部的图片。所以问题是我似乎无法获得"边界框的坐标"围绕图像(bounding box)。我想把图像裁剪到盒子里,有人知道我在说什么或做什么吗?

任何帮助表示赞赏! :)我是一名(非常)初学者程序员。谢谢!

import java.io.File;
import java.io.IOException;
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.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class hello {
    public static void main(String[] args) throws IOException {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    System.out.println("\nRunning FaceDetector");

    CascadeClassifier faceDetector = new CascadeClassifier("C:/Users/Family/workspace/detect face/haarcascade_frontalface_alt.xml");

    File folder = new File("C:\\Users\\Family\\Downloads\\Photos");
    File[] listOfFiles = folder.listFiles();

    for (int j = 0; j < listOfFiles.length; j++) {
        System.out.println(listOfFiles[j]);
    }

    for (int i = 0; i < listOfFiles.length-1; i++){
        String picname = listOfFiles[i].toString();

        System.out.println("reading: " + listOfFiles[i]);

        Mat image = Imgcodecs.imread(picname);

        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

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


        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar (0, 255, 0));
        }


        String filename = "monkey.png";
        System.out.println(String.format("Writing %s", filename));
        Imgcodecs.imwrite(filename, image);

    }
}

}

1 个答案:

答案 0 :(得分:3)

试试这种方式..

rect.getX(); //for getting x cordinates
rect.getY(); //for getting y cordinates
rect.getWidth(); //for getting width
rect.getHeight(); //for getting height

此图片可以为您提供帮助。

enter image description here