Example usage for org.opencv.videoio VideoCapture grab

List of usage examples for org.opencv.videoio VideoCapture grab

Introduction

In this page you can find the example usage for org.opencv.videoio VideoCapture grab.

Prototype

public boolean grab() 

Source Link

Usage

From source file:templatematching.ProcessFrame.java

public Image processFrame(VideoCapture capture) {

    while (k < 2) {
        capture.grab();
        capture.retrieve(frame);/*  www .j a va  2s.c om*/
        capture.retrieve(frame);
        //treat each frame of the capture individually
        // retives the grabbed frame into Mat obj
        int frame_width = frame.rows();
        int frame_height = frame.cols();
        MatOfRect faces1 = new MatOfRect();
        Mat frame_gray = new Mat();
        Mat ImageROI;
        //change the frame to gray-scale
        Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);//gray scale conversion
        //use histogram equilization
        //Imgproc.equalizeHist(frame_gray, frame_gray );
        //use the face classifie
        faceHaar.detectMultiScale(frame_gray, faces1, 1.1, 2, 2, new Size(30, 30), new Size());
        Rect[] faces = faces1.toArray();

        for (int i = 0; i < faces.length; i++) {
            //  System.out.println("Processing faces");
            Point center = new Point(faces[i].x + faces[i].width * 0.5, faces[i].y + faces[i].height * 0.5);
            Imgproc.ellipse(frame, center, new Size(faces[i].width * 0.5, faces[i].height * 0.5), 0, 0, 360,
                    new Scalar(0, 0, 255), 4, 8, 0);
            Mat faceROI = frame_gray.submat(faces[i]);

            MatOfRect eyes1 = new MatOfRect();
            eyesHaar.detectMultiScale(faceROI, eyes1, 1.15, 2, 2, new Size(30, 30), new Size());
            //eyesHaar.detectMultiScale(faceROI, eyes1, 1.1, 2,  Objdetect.CASCADE_FIND_BIGGEST_OBJECT|Objdetect.CASCADE_SCALE_IMAGE, new Size(30,30),new Size());
            Rect[] eyes = eyes1.toArray();
            //  System.out.println("Processing eyes");
            for (int j = 0; j < eyes.length; j++) {

                Mat eyeROI = frame_gray.submat(eyes[i]);
                Point center1 = new Point(faces[i].x + eyes[j].x + eyes[j].width * 0.5,
                        faces[i].y + eyes[j].y + eyes[j].height * 0.5);
                int radius = (int) ((eyes[j].width + eyes[j].height) * 0.005);
                Imgproc.circle(frame, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
                Pupilx = (int) center1.x;
                Pupily = (int) center1.y;
                ROIwidth = eyes[j].width;
                ROIheight = eyes[j].height;
                Point centerX[] = new Point[2];
                centerX[k] = center1;
                //performing the scaling of the coordintaes to fir to the screen dimensions
                if (k == 0) {
                    scaledPupilx = Pupilx;
                    scaledPupily = Pupily;
                    k++;
                } else {
                    System.out.println("In else part");
                    deltax = (int) Math.abs((centerX[k].x - centerX[k - 1].x));
                    deltay = (int) Math.abs((centerX[k].y - centerX[k - 1].y));

                    scaled_deltax = (deltax * (65535 / ROIwidth));

                    scaled_deltay = (deltay * (65535 / ROIheight));
                    scaledPupilx = centerX[k - 1].x + scaled_deltax;

                    scaledPupily = centerX[k - 1].y + scaled_deltay;
                }
                if (k == 2)
                    k = 0;
                //set the cursor position to the scaled coordinates
                try {
                    Robot robot = new Robot();

                    robot.mouseMove((int) (1366 - scaledPupilx), (int) (768 - scaledPupily));
                } catch (AWTException ex) {

                }
            }
            //define a window for displaying the frame

            //release window if any key is hit

        }

        MatOfByte mem = new MatOfByte();
        Imgcodecs.imencode(".bmp", frame, mem);
        Image im = null;
        try {
            im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return im;
    }
    return null;

}