How to change webcam format in JavaCV?

Go To StackoverFlow.com

2

I finally got JavaCV working with my webcam but I can't find a way to change the output format of the image grabbed.

I have a HP HD (720p) fixed webcam in my HP notebook. The only resolution I get is 640x480. I tried using all available subclasses of FrameGrabber.

I am using this simple code:

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class Webcam {

    public static void main(String[] args) throws Exception {
        FrameGrabber grabber = FrameGrabber.createDefault(0);
        grabber.start();

        IplImage grabbedImage = grabber.grab();

        CanvasFrame frame = new CanvasFrame("Some Title", CanvasFrame.getDefaultGamma() / grabber.getGamma());

        while (frame.isVisible() && (grabbedImage = grabber.grab()) != null) {
            frame.showImage(grabbedImage);
        }

        frame.dispose();
        grabber.stop();
    }

}

Am I missing something or is there another way to capture image from a webcam using JavaCV?

2012-04-04 19:45
by Branislav Kuliha


4

I found another way to capture from webcam and also set some properties like preferred width and height.

This code works for me:

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_highgui;
import com.googlecode.javacv.cpp.opencv_highgui.CvCapture;

public class Webcam {

    public static void main(String[] args) throws Exception {
        CvCapture capture = opencv_highgui.cvCreateCameraCapture(0);

        opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);
        opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);

        IplImage grabbedImage = opencv_highgui.cvQueryFrame(capture);

        CanvasFrame frame = new CanvasFrame("Webcam");

        while (frame.isVisible() && (grabbedImage = opencv_highgui.cvQueryFrame(capture)) != null) {
            frame.showImage(grabbedImage);
        }

        frame.dispose();
        opencv_highgui.cvReleaseCapture(capture);
    }

}

Hope this helps somebody with same problem.

2012-04-05 12:20
by Branislav Kuliha
Calling grabber.setImageWidth() and setImageHeight() before start() should also do what you need - Samuel Audet 2012-04-15 05:16
You're right Samuel, I somehow missed that or I've put it after start() - Branislav Kuliha 2012-04-23 15:43
@BranislavKuliha It worked for me. Thank you for the post - codeDEXTER 2012-07-11 09:54
@BranislavKuliha when I set my Logitech C910 to it's native 5MP, I find that the FrameGrabber's FPS drops quite a bit when compared to 640x480 (buttery smooth). Is this more my system's available resources than anything else? I want to use this as an enterprise solution, and if I just need to set up a very powerful computer, so be it - Davek804 2012-08-03 12:04
@Davek804: I don't your webcam supports 5MP capture at that high fps rate. Check link to see the full specs and system requirements. I think your fps should be smooth at max 1080p that is 2MP capture - Branislav Kuliha 2012-08-04 09:28
Ads