List of usage examples for org.opencv.objdetect CascadeClassifier detectMultiScale
public void detectMultiScale(Mat image, MatOfRect objects, double scaleFactor, int minNeighbors, int flags, Size minSize, Size maxSize)
From source file:com.raulh82vlc.face_detection_sample.opencv.domain.EyesDetectionInteractorImpl.java
License:Apache License
/** * <p>Build a template from a specific eye area previously substracted * uses detectMultiScale for this area, then uses minMaxLoc method to * detect iris from the detected eye</p> * * @param area Preformatted Area//from ww w .j a v a 2 s .c o m * @param size minimum iris size * @param grayMat image in gray * @param rgbaMat image in color * @param detectorEye Haar Cascade classifier * @return built template */ @NonNull private static Mat buildTemplate(Rect area, final int size, @NonNull Mat grayMat, @NonNull Mat rgbaMat, CascadeClassifier detectorEye) { Mat template = new Mat(); Mat graySubMatEye = grayMat.submat(area); MatOfRect eyes = new MatOfRect(); Rect eyeTemplate; detectorEye.detectMultiScale(graySubMatEye, eyes, 1.15, 2, Objdetect.CASCADE_FIND_BIGGEST_OBJECT | Objdetect.CASCADE_SCALE_IMAGE, new Size(EYE_MIN_SIZE, EYE_MIN_SIZE), new Size()); Rect[] eyesArray = eyes.toArray(); if (eyesArray.length > 0) { Rect e = eyesArray[0]; e.x = area.x + e.x; e.y = area.y + e.y; Rect eyeRectangle = getEyeArea((int) e.tl().x, (int) (e.tl().y + e.height * 0.4), e.width, (int) (e.height * 0.6)); graySubMatEye = grayMat.submat(eyeRectangle); Mat rgbaMatEye = rgbaMat.submat(eyeRectangle); Core.MinMaxLocResult minMaxLoc = Core.minMaxLoc(graySubMatEye); FaceDrawerOpenCV.drawIrisCircle(rgbaMatEye, minMaxLoc); Point iris = new Point(); iris.x = minMaxLoc.minLoc.x + eyeRectangle.x; iris.y = minMaxLoc.minLoc.y + eyeRectangle.y; eyeTemplate = getEyeArea((int) iris.x - size / 2, (int) iris.y - size / 2, size, size); FaceDrawerOpenCV.drawEyeRectangle(eyeTemplate, rgbaMat); template = (grayMat.submat(eyeTemplate)).clone(); } return template; }
From source file:dr.Interface.java
private void detect_BtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_detect_BtnMouseClicked if (file != null) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); CascadeClassifier faceDetector = new CascadeClassifier(HAAR_FILE_PATH); CascadeClassifier eyeDetector = new CascadeClassifier("src//dr//haarcascade_eye.xml"); Mat image = Highgui.imread(file.getAbsolutePath()); MatOfRect faceDetections = new MatOfRect(); MatOfRect eyeDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections, scale, minN, 0, min, max); 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)); }// ww w . j a v a 2 s . c o m if (eye_detection.isSelected()) { eyeDetector.detectMultiScale(image, eyeDetections, e_scale, e_minN, 0, e_min, e_max); for (Rect rect : eyeDetections.toArray()) { Core.circle(image, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), rect.width / 2, new Scalar(255, 0, 255)); Core.circle(image, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), rect.width / 2 + 1, new Scalar(255, 0, 255)); Core.circle(image, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), rect.width / 2 + 1, new Scalar(255, 0, 255)); } } String filename = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf("\\")) + "\\out.png"; Highgui.imwrite(filename, image); BufferedImage i; try { if ((i = ImageIO.read(new File(filename))) != null) { current_image = i; loadImage_Lbl.setIcon(new ImageIcon(resize_image(i))); } else { JOptionPane.showMessageDialog(this, "Nu s-a detectat nimic!", "Info", JOptionPane.INFORMATION_MESSAGE); } } catch (IOException ex) { Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex); } } }