Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2GRAY;
- import static org.bytedeco.javacpp.opencv_imgproc.cvtColor;
- import static org.bytedeco.javacpp.opencv_objdetect.CASCADE_SCALE_IMAGE;
- import org.bytedeco.javacpp.opencv_core;
- import org.bytedeco.javacpp.opencv_core.Mat;
- import org.bytedeco.javacpp.opencv_objdetect;
- 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.RectRoiConverter;
- /**
- *
- * @author jonathan
- */
- public class FaceDetectionJ implements PlugInFilter
- {
- ImagePlus imp;
- @Override
- public int setup(String arg, ImagePlus imp)
- {
- this.imp = imp;
- return DOES_RGB;
- }
- @Override
- public void run(ImageProcessor ip)
- {
- // Converter
- ImagePlusMatConverter ic = new ImagePlusMatConverter();
- opencv_core.Mat img2 = ic.convert(imp, opencv_core.Mat.class);
- // Detect the faces and store them as an array of rectangles
- detectFaces(img2);
- }
- public opencv_core.RectVector detectFaces(opencv_core.Mat image)
- {
- RectRoiConverter rc = new RectRoiConverter();
- opencv_core.Mat img_gray = new opencv_core.Mat();
- cvtColor(image, img_gray, CV_BGR2GRAY);
- opencv_objdetect.CascadeClassifier faceclassifier = new opencv_objdetect.CascadeClassifier(
- "haarcascade_frontalface_alt.xml");
- // and eyes
- opencv_objdetect.CascadeClassifier eyeclassifier = new opencv_objdetect.CascadeClassifier(
- "haarcascade_eye.xml");
- 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.close();
- // Add rectangles to ROI Manager
- RoiManager rm = new RoiManager();
- rm.setVisible(true);
- for (int i = 0; i < rv.size(); i++)
- {
- Roi r = rc.convert(rv.get(i), Roi.class);
- rm.add(imp, r, 0);
- // How do I search for eyes withing the ROI?
- // Something like this?
- //eyeclassifier.detectMultiScale(ROI in img_gray?);
- //for (int j = 0; j < ROI?; j++)
- //{
- // And here? Like above? Roi e = ??
- //}
- }
- return rv; // and the function does actually not need to return anything anymore, right?
- }
- }
Add Comment
Please, Sign In to add comment