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() 

Source Link

Usage

From source file:OCV_CntrlUvcCamera.java

License:Open Source License

@Override
public void run(ImageProcessor arg0) {
    boolean bret = true;

    // ----- stop dialog during continuous grabbing -----
    diag_free = new JDialog(diag_free, title, false);
    JButton but_stop_cont = new JButton("Stop");

    but_stop_cont.addMouseListener(new MouseAdapter() {
        @Override/*from w  w w  . j  a  v  a  2  s .  co m*/
        public void mouseClicked(MouseEvent e) {
            flag_fin_loop = true;
            diag_free.dispose();
        }
    });

    diag_free.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            flag_fin_loop = true;
        }
    });

    diag_free.add(but_stop_cont);
    diag_free.setSize(100, 75);
    // ----- end of stop dialog -----

    // initialize camera
    VideoCapture src_cap = new VideoCapture();
    Mat src_mat = new Mat();
    bret = src_cap.open(device);

    if (!bret) {
        IJ.error("Camera initialization is failed.");
        diag_free.dispose();
        return;
    }

    src_cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
    src_cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);

    // Setting the image display window
    width = (int) src_cap.get(CV_CAP_PROP_FRAME_WIDTH);
    height = (int) src_cap.get(CV_CAP_PROP_FRAME_HEIGHT);

    ImagePlus impDsp = IJ.createImage(title, width, height, 1, 24);

    int[] impdsp_intarray = (int[]) impDsp.getChannelProcessor().getPixels();

    impDsp.show();
    impDsp.setRoi(0, 0, impDsp.getWidth(), impDsp.getHeight());

    // show stop dialog
    diag_free.setVisible(true);

    // run
    for (;;) {
        if (flag_fin_loop) {
            break;
        }

        // grab
        impDsp.startTiming();
        bret = src_cap.read(src_mat);
        IJ.showTime(impDsp, impDsp.getStartTime(), title + " : ");

        if (!bret) {
            IJ.error("Error occurred in grabbing.");
            diag_free.dispose();
            break;
        }

        if (src_mat.empty()) {
            IJ.error("Mat is empty.");
            diag_free.dispose();
            break;
        }

        // display
        if (src_mat.type() == CvType.CV_8UC3) {
            OCV__LoadLibrary.mat2intarray(src_mat, impdsp_intarray, width, height);
        } else {
            IJ.error("Color camera is supported only.");
            diag_free.dispose();
            break;
        }

        impDsp.draw();

        // wait
        wait(wait_time);
    }

    diag_free.dispose();

    if (src_cap.isOpened()) {
        src_cap.release();
    }
}

From source file:carmelo.JavaFXOpenCV.java

@Override
public void start(Stage primaryStage) {

    capture = new VideoCapture();

    // servicio que ejecutar la captura periodicamente
    service = new ScheduledService<Image>() {
        @Override//from  w  w w.ja  va  2 s.  c  om
        protected Task<Image> createTask() {
            // crear un CameraTask usando el VideoCapture y 
            // el metodo para procesar la imagen idicado JavaFXOpenCV.this::procesarImagen
            return new CameraTask(capture, JavaFXOpenCV.this::procesarImagen);
        }
    };

    // ejecutar el servicio cada 33.3 ms
    service.setPeriod(Duration.millis(33.333333));

    // al finalizar cada ejecucion ejecutar el metodo this::ready
    service.setOnReady(this::ready);

    imageView = new ImageView();
    imageView.setFitHeight(600);
    imageView.setFitWidth(800);

    Button btn = new Button();
    btn.setText("Iniciar Camara");
    btn.setOnAction(this::start);

    VBox root = new VBox(imageView, btn);
    root.setPadding(new Insets(10.0));
    root.setSpacing(10.0);

    Scene scene = new Scene(root);

    primaryStage.setTitle("JavaFX & OpenCV");
    primaryStage.setScene(scene);
    primaryStage.sizeToScene();
    primaryStage.setOnCloseRequest(this::stop);
    primaryStage.setResizable(false);

    primaryStage.show();
}

From source file:cctvanalization.FXMLDocumentController.java

@Override
public void initialize(URL url, ResourceBundle rb) {
    videoCapture = new VideoCapture();
    grabbedFramesPrev = new ArrayList<>();
    grabbedFramesNext = new ArrayList<>();
    grabbedFramesTemp = new ArrayList<>();
    currentFrame = new Mat();
    previousFrame = new Mat();

    Timeline timeline = new Timeline();
    KeyFrame keyFrame = new KeyFrame(Duration.millis(10), event -> {
        if (grabbedFramesPrev.size() > 1) {
            System.out.println("Frames found");
            currentFrame = grabbedFramesPrev.get(grabbedFramesPrev.size() - 1);
            previousFrame = grabbedFramesPrev.get(grabbedFramesPrev.size() - 2);

            //            Mat subtraction = new Mat();
            //            System.out.println("Mat created");

            for (int r = 0; r < currentFrame.height(); r++) {
                for (int c = 0; c < currentFrame.width(); c++) {
                    double currentValue[] = currentFrame.get(r, c);
                    double previousValue[] = previousFrame.get(r, c);
                    double newValue = currentValue[0] - previousValue[0];
                    if (newValue < 0) {
                        newValue = 0;//  www . ja va  2s  .  c om
                    }
                    //    System.out.printf("%f   ", newValue);
                    //         subtraction.put(r, c, newValue);

                    if (newValue > 10) {
                        int changes = 0;
                        for (int i = r - 10; i <= r; i++) {
                            for (int j = c - 10; j <= c; j++) {
                                if (i >= 0 && j >= 0) {
                                    double currCheckValue[] = currentFrame.get(i, j);
                                    double prevCheckValue[] = previousFrame.get(i, j);
                                    double checkValue = currCheckValue[0] - prevCheckValue[0];
                                    if (checkValue < 0) {
                                        checkValue = 0;
                                    }
                                    if (checkValue > 10) {
                                        changes = changes + 1;
                                    }
                                }
                            }
                        }
                        if (changes > 40) {
                            System.out.println("ChangeFound");
                            if (alarmCont == 0) {
                                alarmCont = 1;
                            }
                            if (alarmCont == 1) {
                                ringAlarm();
                                saveFrame(currentFrame, "changedFrame", "png");
                                alarmCont = 2;
                            }
                            break;
                        }
                    }
                }
                //   System.out.printf("\n");
            }
        }
    });
    timeline.getKeyFrames().add(keyFrame);
    timeline.setCycleCount(Timeline.INDEFINITE);
    timeline.play();
}

From source file:com.ibm.streamsx.edgevideo.device.Camera.java

License:Open Source License

public void open() {
    if (initialized)
        return;//from   w ww . j av  a 2 s  .  c om
    camera = new VideoCapture();

    // Properties seem to be a sore spot in OpenCV.
    // Some properties aren't supported on certain cameras and it
    // can be hard to tell which are/arent.
    // In some (all?) cases, setting a property before the device
    // is opened has no effect.

    camera.open(cameraDeviceIndex);
    initialized = true;
    checkOpened();

    adjustForFps();
}

From source file:houghtransform.CaptureJFrame.java

/**
 * Creates new form CaptureJFrame/*from   w  w w. j a  v  a  2  s  .c  om*/
 * @param choiceT
 * @throws java.io.IOException
 */
public CaptureJFrame(int choiceT) throws IOException {
    initComponents();

    this.choiceT = choiceT;

    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent we) {
            for (int i = 0; i < thrN; i++) {
                thread[i].stop();
            }
            for (int i = 0; i < thrN; i++) {
                try {
                    thread[i].t.join();
                } catch (InterruptedException ex) {
                    System.out.println("Parent thread is die!!!");
                }
            }
            frameRate.cancel();
            task.cancel();
            webCam.release();
            dispose();
        }
    });

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    webCam = new VideoCapture();
    webCam.open(0);
    if (webCam.isOpened() == false)
        throw new IOException("No access to camera!!!");

    int width = (int) webCam.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = (int) webCam.get(CV_CAP_PROP_FRAME_HEIGHT);

    this.setSize(width * 2 + 25, height + 80);

    for (int i = 0; i < thrN; i++) {
        thread[i] = new Capture(this, webCam, i, choiceT);
        thread[i].start();
    }

    frameRate.scheduleAtFixedRate(task, 1000, 1000);
}

From source file:imagegame.Camera.java

public Camera() {
    super("Camera - click screen to take a picture");
    initComponents();//from  w  ww.  ja  va  2s . c o  m

    setLayout(new BorderLayout());
    setLibraryPath();
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    videoCapture = new VideoCapture();
    videoCapture.open(0);

    System.out.println("Camera open: " + videoCapture.isOpened());
    if (videoCapture.isOpened()) {
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // Implementation here

                stop();
                System.out.println("Camera released");

            }
        });//end WindowListener
        JPanel cameraJPanel = new JPanel() {
            @Override
            public void paint(Graphics g) {
                g.drawImage(image, 0, 0, this);
            }
        };
        cameraJPanel.setPreferredSize(new Dimension(420, 500));
        add(cameraJPanel, BorderLayout.NORTH);

        Runnable frameGrabber;
        frameGrabber = () -> {
            Mat mat = new Mat();
            videoCapture.read(mat);

            image = mat2BufferedImage(mat);
            //g.drawImage(image, 0, 0,this);
            cameraJPanel.repaint();
            //              rectList.stream().forEach((Rectangle2D rect) -> {
            //                g2d.strokeRect(rect.getMinX(), rect.getMinY(), rect.getWidth(), rect.getHeight());
            //            });
        }; //end frameGrabber
        this.timer = Executors.newSingleThreadScheduledExecutor();
        this.timer.scheduleAtFixedRate(frameGrabber, 0, 133, TimeUnit.MILLISECONDS);
    } else {//if videoCapture.isOpen() is false
        JOptionPane.showMessageDialog(null, "You have no camera installed");
    }
}

From source file:intrudersrecorder.MainFXMLDocumentController.java

@Override
public void initialize(URL url, ResourceBundle rb) {
    vidCap = new VideoCapture();

    previousMatrix = new Mat();
    subtractionResult = new Mat();
    col = new ArrayList<>();

    resolutionWidth = 100;// w  w w  . jav  a2s .  c  o  m
    resolutionHeight = 100;

    sliderInit();
    resSliderListener();
    pixDiffSliderListener();
    objSizeSliderListener();

    cameraSwitch.setStyle("-fx-background-color: red");
}

From source file:Main.Camera.CameraController.java

    private matScale = 1;
       //from   w w w. jav a  2s.com
@Override
public void initialize(URL url, ResourceBundle rb) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    capture = new VideoCapture();
    FaceRect = new Rectangle();
    FaceRect.setX(0);
    FaceRect.setY(0);
    FaceRect.setWidth(150);
    FaceRect.setHeight(150);
        
    System.err.println("CURRENT FRAM WIDTH: " + currentFrame.fitWidthProperty().intValue());
    System.err.println("CURRENT FRAM Height: " + currentFrame.fitHeightProperty().intValue());
}

From source file:objectdetection.VideoCap.java

VideoCap() {
    cap = new VideoCapture();
    //        cap.open(0);
}

From source file:odroidrovone.UVC.java

public UVC() {

    stdCam = new VideoCapture();
    JPanel j;

}