【发布时间】:2018-01-30 23:46:21
【问题描述】:
我已在 opencv 中使用 haarcascade 成功检测到人脸。我已经运行了一个循环,它只会裁剪图像的检测部分并存储到系统中。但它只裁剪一张检测到的人脸,其余的人脸不会被裁剪并保存到系统中。我附上我的代码。请帮助解决我的问题。例如。如果图像包含 4 个人,则 haarcascade 检测到 4 张面孔,但循环仅裁剪一张检测到的图像,其余 3 张图像不会被裁剪..!
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 FaceDetection
{
public static void main(String[] args)
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector = new CascadeClassifier();
faceDetector.load("G:\\haarcascade_frontalface_alt.xml");
//System.out.println ( "Working" );
// Input image
Mat image = Imgcodecs.imread("G:\\sample.jpg");
// Detecting faces
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces",
faceDetections.toArray().length));
// Creating a rectangular box showing faces detected
Rect rectCrop=null;
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),2);
rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
}
Rect[] count = faceDetections.toArray();
System.out.println(""+count.length);
// Saving the output image
String filename = "Ouput.jpg";
Imgcodecs.imwrite("G:\\"+filename, image);
Mat markedImage = new Mat(image,rectCrop);
Imgcodecs.imwrite("G:\\crop1.jpg",markedImage );
}
}
【问题讨论】:
-
代码有效吗? @omkarlanghe
-
代码执行成功..谢谢..! @TheOneWhoMade
-
如果答案有帮助,请使用投票系统下方的复选标记接受答案。 @omkarlanghe
标签: java opencv image-processing