Search code examples
javamatlabopencvjavacvjavacpp

OpenCVFrameGrabber javacv in MATLAB doesnt work


I am using this code in MATLAB R2015a:

javaaddpath('javacv.jar')
import org.bytedeco.javacv.*
grabber = OpenCVFrameGrabber(0)
grabber.start()
img = grabber.grab()

The first time I use this code, it works, opens the camera and grub some image.
After I use grabber.stop(), the code doesn't work any more.
Even if restart MATLAB, and make sure I can open the camera in other programs and made sure the camera is available.

When I run this line: grabber.start(), a new window 'Video Source' is opened.
It does not open when I use the code for the first time.

Video Source
Then I press ok, and there is exception:

Java exception occurred:
org.bytedeco.javacv.FrameGrabber$Exception:
cvCreateCameraCapture() Error: Could not create
camera capture.

    at
    org.bytedeco.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:179)

How I can solve it?


Solution

  • as @Samuel Audet as Mention, I switch to VideoInputFrameGrabber( for windows )

    for linux we need to use FrameGrabber.createDefault(0) , 0 is device index as webcam is at 0 by default

    Now the code is look like that, and it work. (Maybe It what i was using in the first time??)

    javaaddpath('javacv.jar')
    import org.bytedeco.javacv.*
    grabber = VideoInputFrameGrabber(0)
    grabber.start()
    img = grabber.grab()
    

    EDIT:

    as @Samuel Audet as Mention,
    For cross platform code, I switch to FrameGrabber.createDefault()

    javaaddpath('javacv.jar');
    import org.bytedeco.javacv.*
    grabber = FrameGrabber.createDefault(0);
    grabber.start();
    img = grabber.grab();