# Using ImageCapture to access a USB webcam in linux

Is it possible to access a USB webcam through Mathmatica on Linux? When I run

ImageCapture[]


I get the following output

ImageCapture::notsupported: Image acquisition is not supported on Unix. >>


A quick search on Google does not return any useful solutions :(

-

Yes it is possible, but it is not natively supported. Therefore, the way through ImageCapture will not work here. Nevertheless, no one prevents you to use a library like opencv to access the webcam. With a MathLink wrapper you can write a routine to catch frames from the cam and transfer them as Image to Mathematica.

When opencv is initialized and has opened your cam, the pure catching routine is in the simplest case very short:

void catchFrame(void){
if(!frame){
return;
}
long dims[3],size,mu;
int *bm;

dims[0]=frame->height;
dims[1]=frame->width;
dims[2]=frame->nChannels;
size=dims[0]*dims[1];
mu=size*dims[2];
bm=new int[mu];
for(long i=0; i<size; i++) {
bm[3*i]=(unsigned char)frame->imageData[3*i+2];
bm[3*i+1]=(unsigned char)frame->imageData[3*i+1];
bm[3*i+2]=(unsigned char)frame->imageData[3*i];
}

delete[] bm;
}


I admit that this approach is not as simple as calling ImageCapture, but if you are willing to install opencv and cweb I could send you an implementation. It was once written by Jens-Peer Kuska and I only made it work for Mathematica 8.0 and its Image-framework.

Update

I made a pure C version (without CWeb) and added a detailed how-to-use comment. You can download the file from here and with a bit of luck you can use images from your webcam in a few minutes.

-
Thanks Patrick. will give it a try and post the results here! – Ram Mar 10 '12 at 6:56
It is unfortunate you still have to do this... still no camera support in v9.0 as well. I'm not a programmer by trade, I'm not even a good programmer; but I've hacked a little code together that used v4l devices (web cam, tuner cards, etc). It really wasn't that hard - you just have to want to do it and read a little documentation. A few hundred lines of code and I had a 3D rotating cube with input from a tuner card playing on each face. I don't think you can even buy a laptop without a camera these days. – cjpembo Dec 13 '12 at 23:35
The irony is that Wolfram likes to tout v9's face recognition algorithms, improved image processing, etc. Wolfram should just hire someone to write the code for them. Yu-Sung Chang talked about supporting Linux 18 months ago - never happened: blog.wolfram.com/2011/06/23/… – cjpembo Dec 13 '12 at 23:35
@cjpembo I understand Wolfram here. It's difficult to support hardware like a webcam when there are so many different Linux distributions and systems plus Linux is not the main target system plus there are usually no native drivers for Linux. Everything is only supported though things like opencv. – halirutan Dec 14 '12 at 2:40
@cjpembo Currently, there are bigger issues to work on. The Documentation Center bugs since version 8. The GUI of the frontend is far from being as nice as on Win or OSX. These are the things you use all the time. – halirutan Dec 14 '12 at 2:43

## protected by R. M.♦Jan 27 '14 at 2:33

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.