Saltar al contenido

cómo recortar la imagen de la cara detectada en opencv java

Entiende el código bien antes de adaptarlo a tu proyecto y si ttienes algo que aportar puedes decirlo en los comentarios.

Solución:

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("nRunning 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 tiene funciones de región de interés que pueden resultarle útiles. Si está utilizando cv::Mat, entonces podría usar algo como lo siguiente.

// 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);

Prueba esto. Este funciona bien.

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 );
    

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *