List of usage examples for org.opencv.core MatOfRect toList
public List<Rect> toList()
From source file:ch.zhaw.facerecognitionlibrary.Helpers.FaceDetection.java
License:Open Source License
public Rect[] getFaces(Mat img) { MatOfRect faces = new MatOfRect(); List<Rect> facesList = null; float mRelativeFaceSize = 0.2f; int mAbsoluteFaceSize = 0; if (faceDetector != null) { // If no face detected --> rotate the picture 90 and try again angle = 0;/*from w w w . ja v a 2s . c o m*/ for (int i = 1; i <= 4; i++) { int height = img.rows(); if (Math.round(height * mRelativeFaceSize) > 0) { mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize); } faceDetector.detectMultiScale(img, faces, 1.1, 2, 2, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size()); // Rotate by 90 if (faces.empty()) { angle = 90 * i; MatOperation.rotate_90n(img, 90); } else { facesList = faces.toList(); // Check that each found face rectangle fits in the image, if not, remove it for (Rect face : facesList) { if (!(0 <= face.x && 0 <= face.width && face.x + face.width <= img.cols() && 0 <= face.y && 0 <= face.height && face.y + face.height <= img.rows())) { facesList.remove(face); } } if (!(facesList.size() > 0)) { return null; } // Faces found with the current image rotation this.img = img; break; } } } else { Log.e(TAG, "Detection method is not selected!"); } if (facesList != null) { return (Rect[]) facesList.toArray(); } else { return null; } }
From source file:emotion.Eye.java
public Eye(Mat _face) { reg = null;//from ww w .ja v a2s . co m CascadeClassifier eyes_cascade; eyes_cascade = new CascadeClassifier( "E:\\Studia\\OpenCV\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml"); //Detect faces and write eyeLine to array of rectangles MatOfRect eyes = new MatOfRect(); eyes_cascade.detectMultiScale(_face, eyes); if (eyes.toArray().length == 0) { Logger.getLogger("No face found in the image!"); return; } for (int i = 0; i < eyes.toList().size(); ++i) { Rect tempRect = eyes.toArray()[i].clone(); if (tempRect.x < _face.width() / 2) { Eye.leftRect = recalculate(tempRect, _face); Eye.leftEye = new Mat(_face, Eye.leftRect); imwrite("leftEYe.jpg", Eye.leftEye); } else { Eye.rightRect = recalculate(tempRect, _face); Eye.rightEye = new Mat(_face, Eye.rightRect); imwrite("rightEye.jpg", Eye.rightEye); } } templatingOuterCorner(Eye.leftEye, false); templatingInnerCorner(Eye.leftEye, false); templatingOuterCorner(Eye.rightEye, true); templatingInnerCorner(Eye.rightEye, true); }
From source file:org.wso2.carbon.cep.objectdetection.siddhiextension.ObjectDetectionExtension.java
License:Open Source License
/** * Detect objects using OpenCV./* w w w . ja v a 2 s.co m*/ * * @param imageHex * the image hex string * @param cascadePath * the cascade path * @return the detected object count */ private long detectObjects(String imageHex, String cascadePath) { long objectCount = 0; try { // conversion to Mat byte[] imageByteArr = (byte[]) new Hex().decode(imageHex); Mat image = Highgui.imdecode(new MatOfByte(imageByteArr), Highgui.IMREAD_UNCHANGED); // initializing classifier CascadeClassifier cClassifier = new CascadeClassifier(); cClassifier.load(cascadePath); // pre-processing Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY); Imgproc.equalizeHist(image, image); // detecting objects MatOfRect imageRect = new MatOfRect(); cClassifier.detectMultiScale(image, imageRect); // image count objectCount = ((Integer) imageRect.toList().size()).longValue(); } catch (DecoderException e) { e.printStackTrace(); } return objectCount; }
From source file:usefull.hogPeopleDetection.java
License:LGPL
public static void main(String[] args) throws InterruptedException { // load the Core OpenCV library by name System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // create video capture device object VideoCapture cap = new VideoCapture(); // try to use the hardware device if present int CAM_TO_USE = 0; // create a new image object Mat matFrame = new Mat(); // try to open first capture device (0) try {/* w w w.java 2s. com*/ cap.open(CAM_TO_USE); } catch (Exception e1) { System.out.println("No webcam attached"); // otherwise try opening a video file try { cap.open("files/video.mp4"); } catch (Exception e2) { System.out.println("No video file found"); } } // if the a video capture source is now open if (cap.isOpened()) { // create a new window object Imshow ims = new Imshow("HOG People Detection"); boolean keepProcessing = true; // create new HoG detection object HOGDescriptor HOG = new HOGDescriptor(); HOG.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector()); // create MatOfRect foundLocations = new MatOfRect(); MatOfDouble foundWeights = new MatOfDouble(); while (keepProcessing) { // grab the next frame from video source cap.grab(); // decode and return the grabbed video frame cap.retrieve(matFrame); // if the frame is valid (not end of video for example) if (!(matFrame.empty())) { // perform detection HOG.detectMultiScale(matFrame, foundLocations, foundWeights, 0, new Size(8, 8), new Size(32, 32), 1.05, 8, false); List<Rect> rectangles = foundLocations.toList(); for (int i = 0; i < rectangles.size(); i++) { Core.rectangle(matFrame, new Point(rectangles.get(i).x, rectangles.get(i).y), new Point(rectangles.get(i).x + rectangles.get(i).width, rectangles.get(i).y + rectangles.get(i).height), new Scalar(255, 0, 0), 2, 1, 0); } // display image with a delay of 40ms (i.e. 1000 ms / 25 = 25 fps) ims.showImage(matFrame); Thread.sleep(40); } else { keepProcessing = false; } } } else { System.out.println("error cannot open any capture source - exiting"); } // close down the camera correctly cap.release(); }