如何在opencv java中裁剪检测到的面部图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在opencv java中裁剪检测到的面部图像相关的知识,希望对你有一定的参考价值。

我在java中使用opencv 2.4.10检测到图像中的面部

我把我的面部检测代码。

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.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class FaceDetector {

public static void main(String[] args)throws Exception {
    int x,y,height,width;

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    System.out.println("
Running FaceDetector");

    //CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("haarcascade_frontalface_alt.xml").getPath());
    CascadeClassifier faceDetector=new CascadeClassifier();
    faceDetector.load("C:/opencv-2.4.10/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");
    //System.out.println(""+faceDetector);
   // Mat image = Highgui .imread(FaceDetector.class.getResource("D:/shekar.jpg").getPath());
    Mat image = Highgui .imread("D:/Eclipse - New Juno/New Juno Projects/detectface/man1.jpg");
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

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

    for (Rect rect : faceDetections.toArray()) {
        Core.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 = "china.jpg";
    System.out.println(String.format("Writing %s", filename));
    Highgui.imwrite(filename, image);


}

}

现在我想裁剪检测到的面部并将其保存为opencv java中的新jpg图像。

裁剪代码是

 Rect rectCrop = new Rect(x, y, width, height);
 Mat image_roi = image(rectCrop);
        image_roi.copyTo(cropimage);
        imwrite("cropimage.jpg",image_roi);

错误是

方法图像(Rect)未定义FaceDetector类型方法imwrite(String,Mat)未定义FaceDetector类型

请有人帮我解决这个问题。

谢谢

答案
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.core.Size;
import org.opencv.highgui.Highgui;
import static org.opencv.highgui.Highgui.imwrite;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class FaceDetector {
    private static Mat cropImage;

public static void main(String[] args)throws Exception {
    int x = 0,y = 0,height = 0,width = 0;

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        System.out.println("
Running FaceDetector");

//CascadeClassifier faceDetector = new       CascadeClassifier(FaceDetector.class.getResource("haarcascade_frontalface_alt.xm    l").getPath());
    CascadeClassifier faceDetector = new CascadeClassifier(Snapshot.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));
Mat image = Highgui.imread("C:\image.jpg");
faceDetector.detectMultiScale(image, face_Detections);
System.out.println(String.format("Detected %s faces",  face_Detections.toArray().length));
Rect rect_Crop=null;
for (Rect rect : face_Detections.toArray()) {
    Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
            new Scalar(0, 255, 0));
    rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
}



 Mat image_roi = new Mat(image,rectCrop);
  Highgui.imwrite("C:\cropimage_912.jpg",image_roi);



}}
另一答案

OpenCV具有您感兴趣的感兴趣区域功能。如果您使用的是cv :: Mat,那么您可以使用以下内容。

// Take your Final Detected Image
image;

// These values need to be your determined face rect values
cv::Rect myROI(x, y,width, height);

// Crop the full image to that image contained by the rectangle myROI
// Note that this doesn't copy the data
cv::Mat croppedImage = image(myROI);
另一答案

试试这个。这个工作得很好。

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("D:\OpenCv\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml");
        System.out.println ( "Working" );
        // Input image
        Mat image = Imgcodecs.imread("D:\input.jpg");

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

        // 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));
            rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
        }

        // Saving the output image
        String filename = "Ouput.jpg";
        Imgcodecs.imwrite("D:\"+filename, image);

        Mat markedImage = new Mat(image,rectCrop);
        Imgcodecs.imwrite("D:\cropimage.jpg",markedImage );
    }
}
另一答案

如何裁剪多个面孔? dhvani。

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

Mat image_roi = new Mat(image,rectCrop);
    Highgui.imwrite("C:\cropimage_912.jpg",image_roi);
}}
另一答案
 int detectedFaces = faceDetections.toArray().length;
        Rect rect_Crop=null;

        // Draw a bounding box around each face.
        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));
            rect_Crop = new Rect(rect.x, rect.y, rect.width, rect.height);


            Mat image_roi = new Mat(image,rect_Crop);
            StringBuilder sb = new StringBuilder("outputimage");
            sb.append(detectedFaces).append(".jpg");
            Imgcodecs.imwrite(sb.toString(),image_roi);
            detectedFaces--;
        }

这是为了询问如何使用openCV java裁剪多个面孔的人。

另一答案
 Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++) {
        Rect rect = facesArray[i];
        Imgproc.rectangle(frame, rect.tl(), rect.br(), new Scalar(0, 255, 0), 2);
        Mat submat = frame.submat(facesArray[i]);
        imwrite("./face" + i + ".jpg", submat);
    }

这看起来是最聪明的方法。

以上是关于如何在opencv java中裁剪检测到的面部图像的主要内容,如果未能解决你的问题,请参考以下文章

如何将从(对象检测)裁剪的检测到的面部保存到其特定创建的文件夹中?

如何从网络摄像头 OpenCV 裁剪圆形图像并删除背景

Android 人脸地标裁剪

使用 OpenCV 和 C 裁剪图像

Flutter 人脸检测

OpenCV 人脸检测 ROI 断言失败