Ask Your Question

capture image to slow with java

asked 2013-09-05 13:54:56 -0500

a242133 gravatar image

updated 2013-09-13 11:47:58 -0500

open webcam, read and releas takes about 600ms. Is there a way to speed-up? Using it on Windows 7 with latest java and recent opencv:

VideoCapture webcam = new VideoCapture();
boolean opened = webcam.opened();
if (opened) {
    return null;
try {;
} catch (Exception e) {
    return null;

if (!webcam.opened()) {
    return null;
try {
    Mat frame = new Mat();;
    //do somethinig
} catch (Exception e) {
return null;
edit retag flag offensive close merge delete


So many information about your system, camera etc.

Moster gravatar imageMoster ( 2013-09-05 16:25:26 -0500 )edit

@Monster You're right. I did add more information

a242133 gravatar imagea242133 ( 2013-09-06 10:45:26 -0500 )edit

Does it speed up if you dont use try catch?

Moster gravatar imageMoster ( 2013-09-08 09:39:29 -0500 )edit

No, it doesn't help.

a242133 gravatar imagea242133 ( 2013-09-09 14:22:57 -0500 )edit

Would it be possible for you to try the same on c++? If it takes the same time, you wont really be able to speed it up I guess

Moster gravatar imageMoster ( 2013-09-12 13:23:42 -0500 )edit

Never used c++, so it gonna take a while

a242133 gravatar imagea242133 ( 2013-09-13 02:39:09 -0500 )edit

What might bring a little speed is to directly call new VideoCapture(0) and then only check once webcam.opened(), but maybe only a little bit.

Moster gravatar imageMoster ( 2013-09-13 02:51:11 -0500 )edit

Well, camera initialization does take a while, even in pure C/C++.

Daniel Baggio gravatar imageDaniel Baggio ( 2014-01-18 21:56:10 -0500 )edit

(sorry, initially posted as answer, now posting as comment, I'm a little new to this site)

Can you confirm which part of the code is slow? Is it opening the camera, or reading frames?

I'm asking because I have discovered that opening a connection to an IP camera in Java is horribly slow (have to wait a whole minute), but getting frames from the camera afterwards runs at reasonable speed.

arp gravatar imagearp ( 2014-09-29 12:13:59 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-09-28 02:45:00 -0500

rocksean30 gravatar image

updated 2014-09-28 02:46:04 -0500

Try lower resolution

VideoCapture capture = new VideoCapture(0);
int WIDTH = 320;
int HEIGHT = 240;
if (capture.isOpened())
    // to get the actual width of the camera
    System.out.print("Width: " + capture.get(Highgui.CV_CAP_PROP_FRAME_WIDTH));
    // to get the actual height of the camera
    System.out.println(", Height: " + capture.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT));

    boolean wset = capture.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, WIDTH);
    boolean hset = capture.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
    if (!hset || !wset)
        System.out.println("Width Changed: "+wset);
        System.out.println("Height Changed: "+hset);

        // to get the actual width of the camera
        // to get the actual height of the camera
edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-09-05 13:54:56 -0500

Seen: 1,420 times

Last updated: Sep 29 '14