gpt4 book ai didi

java - opencv人脸识别,获取图像周围坐标 "bounding box"

转载 作者:行者123 更新时间:2023-12-01 22:10:16 26 4
gpt4 key购买 nike

今天我正在尝试使用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);

}
}

}

最佳答案

试试这个方法..

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

关于java - opencv人脸识别,获取图像周围坐标 "bounding box",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com