[OpenCV Java] FaceDetection – Croping


Setelah sebelum nya kita sudah mendeteksi wajah, sekarang kita mau cropping setiap wajah tersebut secara otomatis.


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;

public class FaceCrop {

public static void main(String[] args) {

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

CascadeClassifier faceDetector = new CascadeClassifier("/media/ratjoen/DATA/javaFiles/WebCamSec/src/lbpcascade_frontalface.xml");
Mat image = Imgcodecs.imread("/media/ratjoen/DATA/javaFiles/WebCamSec/Arsenal-Team-Photo.jpg");

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

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
//Rect rect_Crop=null;
int ii=0;
for (Rect rect : faceDetections.toArray()) {
Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
Mat image_roi = new Mat(image,rectCrop);//matrix image yang telah terdeteksi dan tercrop
Imgcodecs.imwrite("cropimage"+ii+".png",image_roi);

}

}
}

Advertisements

Author: ridhobustami

orang ngak jelas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s