Example usage for org.opencv.videoio VideoCapture VideoCapture

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

Introduction

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

Prototype

public VideoCapture(int index) 

Source Link

Usage

From source file:FaceGUI.ScannerGUI.java

public void start() {
    webSource = new VideoCapture(0);
    myThread = new DaemonThread();
    Thread t = new Thread(myThread);
    t.setDaemon(true);// w  w w.j a va2s. c  o  m
    myThread.runnable = true;
    t.start();
}

From source file:FaceRecog.App.java

private void runMainLoop(String[] args) {
    ImageProcessor imageProcessor = new ImageProcessor();
    Mat webcamMatImage = new Mat();
    Image tempImage;/*from ww  w .j  ava 2 s  . co m*/
    VideoCapture capture = new VideoCapture(0);
    capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 320);
    capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 240);
    if (capture.isOpened()) {
        while (true) {
            capture.read(webcamMatImage);
            if (!webcamMatImage.empty()) {
                tempImage = imageProcessor.toBufferedImage(webcamMatImage);
                ImageIcon imageIcon = new ImageIcon(tempImage, "Captured video");
                imageLabel.setIcon(imageIcon);
                frame.pack(); //this will resize the window to fit the image
            } else {
                System.out.println(" -- Frame not captured -- Break!");
                break;
            }
        }
    } else {
        System.out.println("Couldn't open capture.");
    }
}

From source file:formularios.FrmCamera.java

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:
    System.out.println("Hello, OpenCV");
    // Load the native library.
    System.out.println(System.getProperty("java.library.path"));
    System.loadLibrary("opencv-300");

    VideoCapture camera = new VideoCapture(0);
    camera.open(0); //Useless
    if (!camera.isOpened()) {
        System.out.println("Camera Error");
    } else {//from w  ww  .j  a  v  a 2s .c  o m
        System.out.println("Camera OK?");
    }

    Mat frame = new Mat();

    //camera.grab();
    //System.out.println("Frame Grabbed");
    //camera.retrieve(frame);
    //System.out.println("Frame Decoded");

    camera.read(frame);
    System.out.println("Frame Obtained");

    /* No difference
    camera.release();
    */

    System.out.println("Captured Frame Width " + frame.width());

    Imgcodecs.imwrite("camera.jpg", frame);
    System.out.println("OK");

}

From source file:geral.JFoto.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    webSource = new VideoCapture(0);
    myThread = new DaemonThread();
    t = new Thread(myThread);
    t.setDaemon(true);/*w  w w . j  a  va  2 s.c  om*/
    myThread.runnable = true;
    t.start();
    jButton1.setEnabled(false); //start button
    jButton2.setEnabled(true); // stop button
}

From source file:gui.WebCamTesting.java

private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartActionPerformed
    // TODO add your handling code here:

    webSource = new VideoCapture(0); //video capture from default cam
    myThread = new DaemonThread(); //create object from thread class
    Thread t = new Thread(myThread);
    t.setDaemon(true);/*from   w w  w  .  j av a  2s  .co  m*/
    myThread.runnable = true;
    t.start(); //start thread

    jButtonStart.setEnabled(false); //deactivate start button
    jButtonPause.setEnabled(true); // activate pause button
}

From source file:Interface_Layer.Capture.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    webSource = new VideoCapture(0);

    myThread = new DaemonThread();
    Thread t = new Thread(myThread);
    t.setDaemon(true);/*from   ww  w.  j av a 2 s . co  m*/
    myThread.runnable = true;
    t.start();
    jButton1.setEnabled(false); //start button
    jButton2.setEnabled(true); // stop button
}

From source file:io.github.jakejmattson.facialrecognition.FacialRecognition.java

License:Open Source License

private static void capture() {
    File classifier = new File("lbpcascade_frontalface_improved.xml");

    if (!classifier.exists()) {
        displayFatalError("Unable to find classifier!");
        return;// w  ww  .j av  a  2 s. c o  m
    }

    CascadeClassifier faceDetector = new CascadeClassifier(classifier.toString());
    VideoCapture camera = new VideoCapture(0);

    if (!camera.isOpened()) {
        displayFatalError("No camera detected!");
        return;
    }

    if (!DATABASE.exists())
        DATABASE.mkdir();

    ImageFrame frame = new ImageFrame();

    while (frame.isOpen() && camera.isOpened()) {
        Mat rawImage = new Mat();
        camera.read(rawImage);
        Mat newImage = detectFaces(rawImage, faceDetector, frame);
        frame.showImage(newImage);
    }

    camera.release();
}

From source file:mx.iteso.desi.vision.WebCamStream.java

License:Apache License

public void startStream(JPanel dst) {
    this.photoPanel = dst;
    this.webSource = new VideoCapture(webCamNumber);
    this.frame = new Mat();
    this.webcamTh = new WebCamThread();
    Thread t = new Thread(webcamTh);
    t.setDaemon(true);//from  w  w w  .  j a  v  a  2s . co m
    t.start();
    webcamTh.runnable = true;
}

From source file:opencv.CamCapture.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    stopflag = 1;//w w w  .  j  a v a 2s .  c  o m
    System.loadLibrary("opencv_java300");
    vc = new VideoCapture(0);
    final int SENSITIVITY_VALUE = 20;
    final int BLUR_SIZE = 10;
    final int WIDTH = jLabel3.getHeight();
    final int HEIGHT = jLabel3.getWidth();
    try {
        t = new Thread() {

            Mat previousFrame = new Mat();
            Mat currentFrame = new Mat();
            Mat grayImage1 = new Mat();
            Mat grayImage2 = new Mat();
            Mat differenceImage = new Mat();
            Mat thresholdImage = new Mat();

            @Override
            public void run() {

                while (true) {
                    if (vc.isOpened()) {

                        vc.read(previousFrame);
                        Imgproc.resize(previousFrame, previousFrame, new Size(HEIGHT, WIDTH));
                        Imgproc.cvtColor(previousFrame, grayImage1, Imgproc.COLOR_BGR2GRAY);

                        vc.read(currentFrame);
                        Imgproc.resize(currentFrame, currentFrame, new Size(HEIGHT, WIDTH));
                        Imgproc.cvtColor(currentFrame, grayImage2, Imgproc.COLOR_BGR2GRAY);

                        //movement Search

                        Core.absdiff(grayImage1, grayImage2, differenceImage);
                        Imgproc.threshold(differenceImage, thresholdImage, SENSITIVITY_VALUE, 255,
                                Imgproc.THRESH_BINARY);
                        Imgproc.blur(thresholdImage, thresholdImage, new Size(BLUR_SIZE, BLUR_SIZE));
                        Imgproc.threshold(thresholdImage, thresholdImage, SENSITIVITY_VALUE, 255,
                                Imgproc.THRESH_BINARY);
                        jLabel4.setIcon(new ImageIcon(Mat2bufferedImage(thresholdImage)));
                        searchForMovement(thresholdImage, currentFrame);
                        //Previous frame is now this frame
                        grayImage2.copyTo(grayImage1);
                        ImageIcon i = new ImageIcon(Mat2bufferedImage(previousFrame));
                        jLabel3.setIcon(i);
                        jLabel3.repaint();

                        if (stopflag == 0) {
                            vc.release();
                            break;
                        }
                    } else {

                        System.err.println("Cam Error");
                    }
                }

            };

        };
    } catch (Exception e) {
        System.err.println(e.toString());
    }
    t.start();

}

From source file:org.usfirst.frc.team5066.controller2017.GripRunner.java

License:Open Source License

/**
 * Make a connection to a camera.//  w  w  w.  j  av  a 2s  . c  o  m
 * 
 * @param device  Camera number.
 * @param width  Window width in pixels.
 * @param height Window height in pixels.
 * @param exposure Relative exposure.
 * @return
 */
public static VideoCapture makeCamera(int device, int width, int height, double exposure) {
    VideoCapture camera = new VideoCapture(1);
    camera.set(Videoio.CAP_PROP_FRAME_WIDTH, width);
    camera.set(Videoio.CAP_PROP_FRAME_HEIGHT, height);
    if (exposure > -1.0) {
        System.out.println("\t" + exposure);
        camera.set(Videoio.CAP_PROP_AUTO_EXPOSURE, 0);
        camera.set(Videoio.CAP_PROP_EXPOSURE, exposure);
    }
    if (!camera.isOpened()) {
        throw new RuntimeException("Camera will not open");
    }
    return camera;
}