Skip to content

Commit

Permalink
Merge pull request #25 from LauLauThom/master
Browse files Browse the repository at this point in the history
Update the face detection
  • Loading branch information
LauLauThom authored Oct 9, 2019
2 parents 777d383 + f07679b commit f2e7ba9
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/main/java/ijopencv/examples/FaceDetectionJ_.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@
import ij.IJ;
import ij.ImagePlus;
import ij.gui.Roi;
import ij.plugin.filter.PlugInFilter;
import ij.plugin.frame.RoiManager;
import ij.process.ImageProcessor;
import ijopencv.ij.ImagePlusMatConverter;
import ijopencv.opencv.MatImagePlusConverter;
import ijopencv.opencv.RectRoiConverter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bytedeco.javacpp.opencv_core;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.cvtColor;
Expand All @@ -40,15 +31,11 @@ public class FaceDetectionJ_ implements Command {
@Parameter
private ImagePlus imp;

private Object opencv_objdetect;

@Override
public void run() {
//Converters
ImagePlusMatConverter ic = new ImagePlusMatConverter();
MatImagePlusConverter mip = new MatImagePlusConverter();
RectRoiConverter rc = new RectRoiConverter();
opencv_core.Mat img2 = ic.convert(imp, opencv_core.Mat.class);
opencv_core.Mat img2 = ImagePlusMatConverter.toMat(imp, 8); // also does RGB to Gray automatically

// Detect the faces and store them as an array of rectangles
opencv_core.RectVector rv = detectFaces(img2);
Expand All @@ -60,19 +47,23 @@ public void run() {
Roi r = rc.convert(rv.get(i), Roi.class);
rm.add(imp, r, 0);
}

//Show all ROI
rm.runCommand("Show All");

}

public opencv_core.RectVector detectFaces(opencv_core.Mat image) {
opencv_core.Mat img_gray = new opencv_core.Mat();
cvtColor(image, img_gray, CV_BGR2GRAY);


// Open xml classifier located in Fiji.app/lib (provided by IJ-OpenCV update site)
opencv_objdetect.CascadeClassifier faceclassifier = new opencv_objdetect.CascadeClassifier(Paths.get(IJ.getDirectory("imagej"),"lib","haarcascade_frontalface_alt.xml").toString());

opencv_core.RectVector rv = new opencv_core.RectVector();

faceclassifier.detectMultiScale(img_gray, rv, 1.1, 2, CASCADE_SCALE_IMAGE, new opencv_core.Size(30, 30), new opencv_core.Size(500, 500));

faceclassifier.detectMultiScale(image, rv, 1.1, 2, CASCADE_SCALE_IMAGE, new opencv_core.Size(30, 30), new opencv_core.Size(500, 500));

faceclassifier.close(); //prevent possible memory leak

return rv;

}
Expand Down

0 comments on commit f2e7ba9

Please sign in to comment.