0

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?

Tal
  • 1,145
  • 2
  • 14
  • 32
  • `VideoInputFrameGrabber` usually works better on Windows. What does that one give? – Samuel Audet Aug 27 '15 at 01:09
  • I don't know. you have any suggestion how to open a camera with opencv on matlab? If so, please write alternative code. I will prefer it will be cross platform code, and it will work on linux too. – Tal Aug 27 '15 at 06:10
  • For cross-platform purposes, try `FrameGrabber.createDefault()` as in the Demo class. – Samuel Audet Aug 27 '15 at 13:12
  • Thanks! How I convert from Frame to Matlab image? – Tal Aug 27 '15 at 19:59

1 Answers1

0

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();
anshulkatta
  • 2,044
  • 22
  • 30
Tal
  • 1,145
  • 2
  • 14
  • 32