OpenCV Seting and working with a camera
Working in opencv with cameras is very easy, we only need a camera and our opencv libraries.
The structure that we store the camera link is CvCapture, here we store this link, then to get this link we need call to cvCaptureFromCAM function, this function need only one param, the index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed.
Then this is our basic line to retrive our camera link
CvCapture* capture = cvCaptureFromCAM( 0 );
Then we only need get each frame we can capture with cam, to do this we can use the cvQuetyFrame passing as param our camera link and it return a IplImage* struct that we can show.
IplImage* frame = cvQueryFrame( capture );
With this easy lines we can get all frames we need from our camera.
31 Comments to “OpenCV Seting and working with a camera”
Leave a Reply






easy and beautiful, thank you very much!
hi
i am using ur code but the frame it is returning is NULL.
i am using proslica firewire camera
pls help me.
Are you on Linux or windows? If you are on linux please, check if your camera run under v4l.
Regards
can can i check if the cam’s working under v4l? i am getting the same problem as muthu…
how can i check if the cam is working under v4l under ubuntu? i am getting the same error!
Hi All,
I need a help..
I have a 1394 camera connected to my PC.Iam not able to capture the image from the camera.I was able to run the sample code of opencv.
$testlibraw
successfully got handle
current generation number: 1
1 card(s) found
nodes on bus: 2, card name: ohci1394
using first card found: 2 nodes on bus, local ID is 1, IRM is 1
doing transactions with custom tag handler
trying to send read request to node 0… completed with value 0xd911fe78
trying to send read request to node 1… completed with value 0×5819fe78
using standard tag handler and synchronous calls
trying to read from node 0… completed with value 0xe924fe78
trying to read from node 1… completed with value 0×4629fe78
testing FCP monitoring on local node
got fcp command from node 1 of 8 bytes: 01 23 45 67 89 ab cd ef
got fcp response from node 1 of 8 bytes: 01 23 45 67 89 ab cd ef
testing config rom stuff
get_config_rom returned 0, romsize 64, rom_version 4
here are the first 10 quadlets:
0. quadlet: 0xc8850404
1. quadlet: 0×34393331
2. quadlet: 0×32a264e0
3. quadlet: 0×087a0200
4. quadlet: 0×0f820100
5. quadlet: 0×32b90300
6. quadlet: 0×7a020003
7. quadlet: 0×02000081
8. quadlet: 0xc083000c
9. quadlet: 0×2a2c0600
update_config_rom returned 0
polling for leftover messages
The libdc version is ..
libdc1394-13 1.1.0-5ubuntu1
libdc1394-13-dev 1.1.0-5ubuntu1
opencv i got it from..
svn co https://opencvlibrary.svn.sourceforge.net/svnroot/opencvlibrary/trunk
———-program output————–
$gcc `pkg-config –cflags –libs opencv` -o my-opencv-prgm camdemo.c
In file included from /usr/local/include/opencv/cxcore.h:70,
from /usr/local/include/opencv/cv.h:58,
from camdemo.c:6:
/usr/local/include/opencv/cxtypes.h: In function ‘cvRound’:
/usr/local/include/opencv/cxtypes.h:240: warning: incompatible implicit declaration of built-in function ‘lrint’
Pls let me know if any other infl is required..
[...] [1.] http://blog.damiles.com/?p=61 [...]
I have already been successful by using prosilica GIGE cam and OpenCV. Infact now I am viewing real time edge detected video. However the computation seems to overload and freeze my processor. Hence I am looking for ways to ensure previous frame get captured and computation is done successfully before display. Presently I am using delay through timers. but thats not the best way I know So I am thinking of POSIX threads based realization or have flags check. Anyone has tried this before?
Hey i wanted to know if all cameras are compatible with openCV so that i can decide on which one to buy.
Pls reply on my id shadowabhishek@gmail.com..
Thanks
Hi, opencv have support to v4l and firewire cameras, if in your system works your camera sure it works in opencv.
If you are looking for for profesional camera, i can’t help to select one.
Regards.
Hi I’m trying to get OpenCV recognise multiple cameras. My code is below.
When I run the code, two windows pops up and captures seem to work. But the windows blank out later and the program hanged. And I got the following error message
“HIGHGUI ERROR: V4L: Could not SYNC to video stream. Input/output error”
Do you know what might be the problem?
thanks,
Hsu
************************************************
#include “highgui.h”
#include “cv.h”
#include “cvaux.h”
#include “stdio.h”
int main(int argc, char** argv) {
int done = 0;
cvNamedWindow(“Left Frame”,CV_WINDOW_AUTOSIZE);
cvMoveWindow(“Left Frame”, 100, 100);
cvNamedWindow(“Right Frame”,CV_WINDOW_AUTOSIZE);
cvMoveWindow(“Right”, 420, 100);
//CvCapture* capture;
CvCapture* captureLeft;
CvCapture* captureRight;
if(argc == 1) {
//capture = cvCreateCameraCapture(0);
captureLeft = /*cvCaptureFromCAM(0);*/cvCreateCameraCapture(0);
captureRight = /*cvCaptureFromCAM(1);*/cvCreateCameraCapture(1);
cvSetCaptureProperty(captureLeft, CV_CAP_PROP_FRAME_WIDTH, 320);
cvSetCaptureProperty(captureLeft, CV_CAP_PROP_FRAME_HEIGHT, 240);
cvSetCaptureProperty(captureRight, CV_CAP_PROP_FRAME_WIDTH, 320);
cvSetCaptureProperty(captureRight, CV_CAP_PROP_FRAME_HEIGHT, 240);
if( !captureLeft )
{
printf( “Cannot open initialize WebCam 1\n” );
return 1;
}
if( !captureRight )
{
printf( “Cannot open initialize WebCam 2\n” );
return 1;
}
} else {
captureLeft = cvCreateFileCapture(argv[1]);
}
IplImage* leftFrame;
IplImage* rightFrame;
while(1) {
leftFrame = cvQueryFrame(captureLeft);
rightFrame = cvQueryFrame(captureRight);
if(!leftFrame) break;
if(!rightFrame) break;
cvShowImage(“Right Frame”,rightFrame);
cvShowImage(“Left Frame”,leftFrame);
char c = cvWaitKey(10);
if(c==27){ done = 1; break;}
if(done == 1) {
cvWaitKey(0);
cvReleaseImage(&leftFrame);
cvReleaseImage(&rightFrame);
cvDestroyWindow(“Left Frame”);
cvDestroyWindow(“Right Frame”);
}
}
cvWaitKey(0);
cvReleaseCapture(&captureLeft);
cvReleaseCapture(&captureRight);
return(0);
}
Hello,
I am trying to work with 2 cameras. But I am working in windows and open cv 1.1. It doesn’t have cvcam library and I have no clue how to get it working!!! Do any one have an idea how to call two cameras in windows in opencv 1.1
Thank you
Hi, Hsu, I think your code should work. May be an USB host bandwith problem? Try rising the delay on your first cvWaitKey from 10 to 30.
Also, check the location of the break sentence on your program exit code. I think it is misplaced and you are not releasing resources properly. I think it would be easier to break on ESC key and release all resources out of the while brackets.
By the way, left and right cameras? Are you on some stereo vision aplication?
hi i seem to be having the same problem as Hsu.desptire the fact that am using python.
when i change the device parameter from cvCaptureFromCAM(0) to cvCaptureFromCAM(1).
the program crashes.
is there anyway that i can make it use more than one webcamera.
Hello , I am doing a project , I am using a webcam for object detection
the object is a company logo , I am doing it in my laptop . Now when I
use an USB web camera it is working properly , but when I am going to
access the internal web cam of the laptop then nothing is showing on
the screen but I have assured that it is recognizing the camera . I am
using cvCaptureFromCAM function . PLEASE HELP ME.
If there are no usb camera it works?
This works great for me,actually I am working on a project;its objective is to capture and also store the frames as .jpg so i can then do image recognition on the required image.
I’ll appreciate if you could help me out and I will elaborate more.
Hello!
I have a sumix camera (smx-150M) that cannot work with OpenCV!!! It woked in linux with xawtv(v4l) and in windows with its own installed application! i cant figure out what’s the matter! please help me!!
Hi, what’s the error you get? if it work with v4l it must work under opencv linux…
Hello damiles!
I got the error! incoming pixel formats is unsupported in OpenCV! Bad File descriptor! cannot end stream.. and some times when running my sources from shell it gives another error: /dev/video0 doesnt support memory mapping:s..
Thank you for interst!
I would be very grateful if you can answer me a little soon! I’m stuck in my project!
Please, check camera run correctly under v4l, if it go ok and don’t have any problem as memory mapping or similar, if all is ok then check the input format, there are opencv functions for camera settings, then try to chek the camera settings and then try run with correct settings for your project. The incoming pixel format can be you try read a camera image 8 bit gray pixels and try to store in 32 pixel gray image.
I checked it! it works with some V4L utilities and fails with others. The error seems to talk about some grey format (My cam is a professional USB camera high resolution 1280×1024 pixel!). For OpencCv I dont store any thing i just try to capture from cam and it gives those errors!
Ok then you must check this error, and check this gray format, opencv over linux works with v4l then perhaps your camera don’t work, or need a driver for work correctly with v4l, if you can run correctly with xawtv and it don’t have error then sure you can run in opencv. but if you have error try to get a corret driver for your camera or create your v4l access…
The camera is profesional, then sure have an sdk, check and try to use it instead opencv camera settings, then only need convert image camera sdk to opencv image.
The manufacturer doesnt offer a lot of support for Linux, i just got a small driver from them.Trying to capture with VLC or tools like gives this error with dmesg:”smxcam_do_ioctl(): v4l2_standard.index=1 != 0
smxcam_do_ioctl(): v4l2_fmtdesc.fmt.pix.pixelformat=877807426 != V4L2_PIX_FMT_GREY or V4L2_PIX_FMT_RGB24 ”
Knowing that function smxcam-do-ioctl is in the driver implements V4L2 ant tests for palettes! Any idea please?
Im’t not expert in v4l, you must send mail to v4l users to help in this because you have a bad driver or using one you don’t need because you don’t have a correct pixel format from camera.
I cant help in this. Sorry.
i just gone through your blog..its amazing…i am building a protocol model of hawk-eye as my university project.my question is that can i save the individual frame to a disk..?i tried out with cvSaveImage but its saving only one frame,and that to starting of the video.please can you help me in this??thanks in advance…..
Hi. With cvSaveImage you can save all frames you want. In loop you must call to
frame = cvQueryFrame( capture );
And in loop save this frame with cvSaveImage
cvSaveImage(“name_”<< frame_num << “.jpg”, frame);
Regards.
David.
Hey thanx alot for help…it works fine now.
u may view my captured frame at :-
http://s848.photobucket.com/albums/ab44/neerajvenator/
now my task is to get the co-ordinates of the ball and line and judge whether the ball is inside the line or outside.how can i get the co-ordinates?i am not able to get it.please can you help me?
You must determine what is background and wath is not background for detect the ball. Then you can get the ball positions.
For the line you can search with simple image segmentation looking for white colors.
Regards. David.
Hi
I’m workin with 2 cameras, but I can’t identify with one…
for example, how can I know that index 0 references camera A and index 1 references the camera B?
Someone help?