如何使用 Java 在 CascadeClassifier 中设置裁剪大小

How to setting cropping size in CascadeCalssifier with Java

public BufferedImage detectAndDisplay(BufferedImage img, CascadeClassifier faceCascade) {
    Mat frameGray = new Mat();
    BufferedImage imgout = null;

    Mat image = ImagePreProcessing.bufferedImageToMat(img);

    // -- Detect faces
    MatOfRect faces = new MatOfRect();
    faceCascade.detectMultiScale(image, faces);

    List<Rect> listOfFaces = faces.toList();
    for (Rect face : listOfFaces) {
        Point center = new Point(face.x + face.width / 2, face.y + face.height / 2);
        Imgproc.ellipse(image, center, new Size(face.width / 2, face.height / 2), 0, 0, 360,
                new Scalar(255, 0, 255), 3);

        Mat faceROI = image.submat(face);
        imgout = ImagePreProcessing.Mat2BufferedImage(faceROI);
        System.out.println("OpenCV: " +center);

    }
    return imgout;
}

我有那个代码..但我不知道设置裁剪输出图像的代码在哪里。 我想要带有圆形模板的原始版本的图片..不要被裁剪 可以给我建议吗:)

输入:

输出:

在您的代码中,您return编辑了原始图像的裁剪图像,因此如果您想要原始图像,请绘制圆并将其转换为 BufferedImage 和 return。

for (Rect face : listOfFaces) {
        Point center = new Point(face.x + face.width / 2, face.y + face.height / 2);
        Imgproc.ellipse(image, center, new Size(face.width / 2, face.height / 2), 0, 0, 360,
                new Scalar(255, 0, 255), 3);

        // dot not crop!!!
        /*Mat faceROI = image.submat(face);
        imgout = ImagePreProcessing.Mat2BufferedImage(faceROI);*/

        System.out.println("OpenCV: " +center);
        imgout = ImagePreProcessing.Mat2BufferedImage(image);

    }