Monday, November 14, 2011

33

OPENCV: STEREO WEBCAM

Today, an easy one :) A small program in OpenCV to implement a cheap stereo webcam and visualize the left and right images. The only thing you need are two USB webcams plugged in your computer (better if both are same brand and model):



 Download the source code:


Compile it:

tar xzvf OpenCVStereoWebcam-1.0.tgz
cd OpenCVStereoWebcam
make
./OpenCVStereoWebcam

And there you go, your cheap USB stereo webcam.


Have fun!

33 comments:

Anonymous said...

Hi Martin,

I tried to download your source code for stereo cams. But the link seems to be broken. Could you please fix it? Thank you.

Martin Peris said...

Hi! Sorry about that. The problem is that many people is downloading the contents on my hosting service and the bandwidth limit for this month has been exceeded :(

The blog still works because it is hosted on blogger, but the rest will be down until January 1st.

I am really sorry for the inconvenience, please understand that I do this as a hobby and my resources are limited, I don't charge anything to anybody for the software I post or the advice I offer on posts, comments and e-mails. I am working on getting funds for a better web hosting , so far google adds has been useless because no cliks on adds = no earnings, and the readers of this blog seem to be more interested about the contents than about the adds (which is logic).

Anyway, if you are in a hurry and don't want to wait the hosting service to be up again (January 1st) you can always let me know your e-mail and the software you need and I will send you a copy as soon as possible.

Again, my apologies.
Martin

Vicente Daniel Exposito Funes said...

Martin you are doing a very good job here. I've already downloaded the stereo vision calibration software and it have ilustrated me a lot, really really thank you. I'm working with robotics and artificial vision and I was interested to download this software "OpenCV: STEREO WEBCAM" but I realize now (I think I downloaded it about 2 weeks ago) that I downloaded an empty file. Would you mind looking for the file which was uploaded in order to check that it's ok??

And if you don't mind could you send me a copy of this software to take a look?

vdexposito at gmail.

Thanks and Merry Christmas

Martin Peris said...

Hi vd_exposito!

Thanks a lot for your comment, I really appreciate it. I will check the file, but it is likely that what happened is that you tried to download the sourcecode after the hosting ran out of bandwidth :S

Anyway, I already sent you a copy via e-mail.

Enjoy it an Merry Christmas!

Vicente Daniel Exposito Funes said...

Well, it seems empty again the copy you sent me. I will check it under an other OS and I write you again.

;)

Martin Peris said...

mmm it might be that your decompressor doesn't support tgz? I will try sending you a zipped version.

Vicente Daniel Exposito Funes said...

Yo aqui mal hablando inglés y eres de los pirineos para abajo... :D lo miro ahora ;)

Martin Peris said...

Jajajaja soy de Valencia :D Veo en twitter que eres de la Palma? Que pequenyo es intenne xD

Vicente Daniel Exposito Funes said...

He estado probando el código, no he tenido mucho tiempo últimamente, y me da la siguiente excepción:


VIDIOC_STREAMON: No hay espacio libre en el dispositivo
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file cxcore/cxarray.cpp, line 2470
An exception occurred. Ignoring frame. Unrecognized or unsupported array type
VIDIOC_QBUF: Argumento inválido
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file cxcore/cxarray.cpp, line 2470
An exception occurred. Ignoring frame. Unrecognized or unsupported array type


Tengo imagen en una de las pantallas (left creo recordar) y la otra no tiene imagen.

Como comentabas que las camaras fueran iguales (y yo las tenía pero una se rompió asi que de momento tengo dos distintas hasta que me lleguen dos nuevas que he pedido) no se si el error puede estar ahi.

Un saludo!

Martin Peris said...

Hola vd_exposito!

Que las camaras sean iguales en principio no deberia importar, siempre y cuando cumplan con los estandares pertinentes.

El error "VIDIOC_STREAMON: No hay espacio libre en el dispositivo" parece indicar que no hay suficiente ancho de banda en el hub USB para las dos camaras, por eso una va y la otra no. En realidad si que deberia haber suficiente ancho de banda para las dos camaras, pero por lo visto algunos fabricantes no se preocupan por eso y en el firmware de la webcam solicitan TODO el ancho de banda (y si no me equivoco esto va en contra del estandar).

Parece ser que no haya una solucion sencilla, pero si tu ordenador tiene varios HUB USB prueba a conectar una de las camaras a otro HUB, deberia funcionar.

Espero que esto te pueda ayudar.

Saludos.

Anonymous said...

Hi,

I am a big fan of your works =D

I am switching from Visual C++ windows to Linux Ubuntu. In windows, I can easily operate 2 cameras but using simple operations such as below:

CvCapture*camera0 = cvCaptureFromCAM(0);
IplImage*img1 = cvQueryFrame(camera0);
img1 = cvQueryFrame(camera0);

However, when I tried the same thing in Ubuntu, I got a null image as output. The same problem occur using ur code, it could not detect any cameras.

I tried to search for answer by googling but the answers are diverging. Is this a common issue caused by Ubuntu kernel and is there any solution ?

Martin Peris said...

Hi!

Thanks a lot for your kind comment :) I am sorry to hear that you are having problems with Ubuntu. It could be that your webcam is not supported by the kernel, but it is strange.

There is several things you could try to find out what is going on, for example: check the value of camera0 after calling cvCaptureFromCAM(0) it should not be NULL.

Also, the device /dev/video0 should exist (you can check it with "ls /dev/video0")

The information returned by the command "lsusb" would be also helpful.

I hope we can figure it out.
Best regards,
Martin.

Anonymous said...

Hola, Martin:
When I ./OpenCVStereoWebcam your program, my computer always said that the HIGHGUI ERROR: V4L: index 1 is not correct! ERROR: Could not open cameras. Do you know what happen to the program or my computer?
OpenCV: 2.3.1a Ubuntu11.04
Muchas gracias!

Martin Peris said...

Hola Anonymous,

It looks like for some reason the V4L driver is not detecting the second camera.
Make sure that both webcams are correctly detected. They should appear under the names:
/dev/video0
/dev/video1

I hope this helps.
Martin

Anonymous said...

Sir i am doing project in opencv 2.3 using angsrtom LINUX ,on ARM CORTEX @A8 PROCESing .We are having problem related to camera interfacing(calibaration)so plz help us.u CAN MAIL ME AT-harish_up50@yahoo.co.in

Anonymous said...

How would you ensure that both of the cameras are fixed?,I have miccrosoft webcams available with me and facing problems to join both of them ensuring that they dont move with respect to each other.

Anonymous said...

I'm using Ubuntu 12.04 version,I tried a code for capturing images from a camera but when I run it, it throws me an error-"Segmentation fault(core dumped)"
I tried varying the indices in the line cvCaptureFromCAM(0),but it didn't helped.
How to fix this?

Unknown said...

Hello Martin
Could you tell me about your hardware USB version, Webcam USB version, OS please.
Because I

For I myself. My system is
GNU/Linux Debian Wheezy 3.2.0-2-amd64
Opecv2.3.
HP Compaq 6515b

I tried to openup 2 cams at the same time only, using your code as a base program.

VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
libv4l2: error turning on stream: No space left on device
VIDIOC_STREAMON: No space left on device
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /tmp/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482
An exception occurred. Ignoring frame. Unrecognized or unsupported array type
VIDIOC_QBUF: Invalid argument
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /tmp/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482
An exception occurred. Ignoring frame. Unrecognized or unsupported array type
VIDIOC_QBUF: Invalid argument
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /tmp/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482
An exception occurred. Ignoring frame. Unrecognized or unsupported array type


This error will repeat itself and flood my terminal

VIDIOC_QBUF: Invalid argument
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /tmp/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482


Do you have any advise ?
Regards
Sarit

Anonymous said...

Hi martin...
can u send me the zipped file to my email...i having some problem with my decompressor...
bipbop75@yahoo.com...tq

Unknown said...

Hi Martin,

Thank you for all the information you are providing.
I have some issues with OpenCV and would like to ask you some advices.
I am working with 2 cameras, a Minoru (http://www.minoru3d.com/) and the Fuji W3-3D.

In OpenCV, I am using the following to open both left/right cameras:
cv::VideoCapture capLeft(0); // open the Left camera
cv::VideoCapture capRight(1); // open the Right camera

I do not have any problem for opening and running the Minoru camera (both left/right camera can be opened and view in live).

Problem is with the Fuji W3-3D camera. The method .isOpened() always return false.
As you used this Fuji camera, I would like to know if you had same issue and if you solved it...
Note that I did not install any specific driver/SDK for the Fuji camera; windows "nicely" recognize it.

Thank you for your help,
Ben.

Jaysin Koregaokar said...

hi martin,
there is a delay between the right and left image updating. can this me eliminated or lessenned. it's a one second delay.
thanks.

Unknown said...

I have a stereo camera... (ie a 3d camera) now can anybody suggest me a code to calculate the distance of the object using open cv...??!!! plz

Mail me at jamakhandihari@gmail.com

Unknown said...

I have a stereo camera... (ie a 3d camera) now can anybody suggest me a code to calculate the distance of the object using open cv...??!!! plz

Mail me at jamakhandihari@gmail.com

Rasoul said...

Hi Martin,
What is the model of the webcam cameras you used?

Unknown said...

Hello,

If you have only one webcam image open, or see a message about not enought space on device, this is because your system does not have enough USB bandwidth to handle both cameras. I have altered the code to lower the resolution to 320x240;

--->8---
int main(int, char**)
{
cv::VideoCapture capLeft(0); // open the Left camera
capLeft.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capLeft.set(CV_CAP_PROP_FRAME_HEIGHT, 240);

cv::VideoCapture capRight(1); // open the Right camera
capRight.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capRight.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
if(!capLeft.isOpened() || !capRight.isOpened()) // check if we succeeded
{
std::cerr << "ERROR: Could not open cameras." << std::endl;
return -1;
}
---8<---

Feel free to include this in your archive as a command line switch or something.

Cheers

K

Calin Dragos said...

very good guide to calibrate and use a stereo vision camera with a powerful vision software like Opencv. I add a link of your article to my post about stereo vision systems uses in robotics, tutorials and resources http://www.intorobotics.com/fundamental-guide-for-stereo-vision-cameras-in-robotics-tutorials-and-resources/

Earl J. Russell said...

Out of curiosity does the distance between the left and right camera matter? Or is it flexible so long as you know the value for calibration purposes inside of OpenCV?

Martin Peris said...

Hi Earl,

Yes, the baseline distance does matter. It will rule the minimum and maximum distance at which points can be matched between both cameras. As a rule of thumb: smaller basline -> closer points can be matched. bigger baseline -> further points can be matched.

The opencv calibration procedure will tell you the baseline distance between your cameras.

Anonymous said...

Hi Martin,
Very nice tutorial. I have implemented the stereo calibration and 3d distance estimation in opencv C++ api. It is very easy to code. Your steps and codes are helpful.

I am getting the distance relatively accurate ratio, but I don't know, which scale(multiplying factor) and unit(m or inches). Kindly let me know that. I tried multiplying by the square size of the checkerboard, but it didn't give the accurate distance, but my depth image is very accurate.

Unknown said...

Hi Martin,

I am experiencing a delay of 1 second between the images in the left and right cameras, the same as what Jaysin Koregaokar said above. Any idea's on why this would be and how to work around this?

Unknown said...

Hello Martin,
I love your site and thank you for your contribution - here with OPENCV - some amazing stuff. At any rate, I am new to Linux - stemming from a back-ground from the visual arts as a practicing artist. However, as of recently I've been integrating some technology into my artistic. And so, I am following your recipe for setting up my two webcams on Ubuntu 12.04. However when I launch the program - only my left webcam comes on and the right webcam comes on as a small screen colored black with no image.
I believe my situation relates to what Karl Sumpter mentions above. Which I have tried his code - but I have no idea how to implement or where to plug-in this code.
(--->8---
int main(int, char**)
{
cv::VideoCapture capLeft(0); // open the Left camera
capLeft.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capLeft.set(CV_CAP_PROP_FRAME_HEIGHT, 240);

cv::VideoCapture capRight(1); // open the Right camera
capRight.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capRight.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
if(!capLeft.isOpened() || !capRight.isOpened()) // check if we succeeded
{
std::cerr << "ERROR: Could not open cameras." << std::endl;
return -1;
}
---8<---)
Any feedback would be greatly appreciated.
Please contact me my email mngr5@msn.com
Saludos!
Luis Ferrel

Steph said...

I'm trying to figure out how to get frames saved as PPM format to use in your other tutorial about calibrating stereo cameras.

How can I augment this code to save every N'th frame out to PPM?

Unknown said...

Hi Martin,

I am doing stereo camera, too. However, it seemed my calibration code got some problem so the rectified image was distorted. Could you please share you code again ? The link was broken (404)

Thank you so much!

Post a Comment