Example usage for org.opencv.videoio VideoCapture set

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

Introduction

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

Prototype

public boolean set(int propId, double value) 

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 .ja  va 2  s  . c o  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:bikecalibration.OpenCvUtils.java

public static Mat getImageFromVideo(int position, VideoCapture cap) {
    Mat frame = new Mat();
    cap.set(Videoio.CAP_PROP_POS_FRAMES, position);
    cap.read(frame);//from  w w  w.  j  a va 2 s. c o  m
    return frame;
}

From source file:com.armeniopinto.stress.control.vision.VisionConfig.java

@Bean(name = "visionDevice", destroyMethod = "release")
public VideoCapture videoCapture() throws VisionException {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    LOGGER.debug("OpenCV native library loaded.");

    final VideoCapture device = new VideoCapture(deviceId) {
        @Override/*from w w w  .j  a v a  2  s .co  m*/
        public void release() {
            super.release();
            LOGGER.debug("Vision device stopped.");
        }
    };
    device.set(CAP_PROP_FRAME_WIDTH, width);
    device.set(CAP_PROP_FRAME_HEIGHT, height);
    try {
        TimeUnit.MICROSECONDS.sleep(2000L);
    } catch (final InterruptedException ie) {
        LOGGER.warn("Failed to sleep!", ie);
    }
    if (!device.isOpened()) {
        throw new VisionException("Unable to open the camera.");
    }

    final int actualWidth = (int) device.get(CAP_PROP_FRAME_WIDTH);
    final int actualHeight = (int) device.get(CAP_PROP_FRAME_HEIGHT);
    if (actualWidth != width) {
        LOGGER.warn(String.format("Requested frame width %d but got %d instead.", width, actualWidth));
    }
    if (actualHeight != height) {
        LOGGER.warn(String.format("Requested frame height %d but got %d instead.", height, actualHeight));
    }

    LOGGER.debug(String.format("Vision device %d started at %dx%d.", deviceId, actualWidth, actualHeight));

    return device;
}

From source file:edu.wpi.first.wpilibj.vision.GripRunner.java

License:Open Source License

/**
 * Make a connection to a camera./*from w  w  w  . j a  va  2  s . c om*/
 * 
 * @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(0);
    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;
}

From source file:FaceRecog.App.java

private void runMainLoop(String[] args) {
    ImageProcessor imageProcessor = new ImageProcessor();
    Mat webcamMatImage = new Mat();
    Image tempImage;//  w w w.  j av  a 2 s  .  com
    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:org.usfirst.frc.team5066.controller2017.GripRunner.java

License:Open Source License

/**
 * Make a connection to a camera./*w  w w .  j a v a2s  .c om*/
 * 
 * @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;
}

From source file:readnumber.ReadNumber.java

/**
 * /*from   w  w w. ja v  a2s.  com*/
 * @param args runMainLoop - VideoCapture
 */

private void runMainLoop(String[] args) {
    imageProcessor = new ImageProcessor();
    webcamMatImage = new Mat();
    Image tempImage;
    VideoCapture capture = new VideoCapture(0);
    capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 320);
    capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 240);

    // Create a face detector from the cascade file                
    faceDetector = new CascadeClassifier(CascadeFile);

    if (capture.isOpened()) {
        while (true) {
            capture.read(webcamMatImage);
            if (!webcamMatImage.empty()) {
                // Output video to form (JLabel)
                //imageLabel.setBounds(0, 61, 320, 240);
                tempImage = imageProcessor.toBufferedImage(webcamMatImage);
                ImageIcon imageIcon = new ImageIcon(tempImage, "Captured video");
                imageLabel.setIcon(imageIcon);
                Buttonl.setBounds(10, 0, 140, 30);
                //Button2.setBounds(140, 0, 120, 30);
                message.setBounds(10, 30, 200, 30);
                inform.setBounds(10, 315, 200, 30);
                caunttext.setBounds(100, 315, 200, 30);
                textVideo.setBounds(10, 295, 120, 30);

                //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.");
    }

}