Note for English Readers

If I write the articles in Indonesian, I will write a summary in English so that you can read my articles too. After you read the summary and you feel that you need more information about that, please do not hesitate to contact me via e-mail that can be found in my profile.

Thank you for reading my blogs.

Wednesday, September 19, 2007

Capturing Frames from a USB Camera using OpenCV on Linux

Next step after loading image and manipulating it, I try to access a Linux-USB-registered USB Camera using a simple camera capture framework written in C with OpenCV Library. Here is the code:

/////////////////////////////////////////////////////
// trialcam1a.cpp
// A Simple Camera Capture Framework
// This program will connect to a camera then
// show the frames in a window
/////////////////////////////////////////////////////

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main()
{
IplImage *frame = NULL; //Preparing frame pointer
int key;


//Allocates and initializes cvCapture structure

// for reading a video stream from the camera.
//Index of camera is -1 since only one camera

// connected to the computer or it does not
// matter what camera to use.
CvCapture *input_camera = cvCaptureFromCAM(-1);

//Grabs and returns a frame from camera

frame = cvQueryFrame
(input_camera);

//Creates window for displaying the frames
//Flag is reset (0) --> change window size
// manually

cvNamedWindow("Capturing Image ...", 0);

//Change to the appropriate size. In GTK, the
// inappropriate size will return a segmentation
// fault. I don't know why ...
//Gets the appropriate size using cvGetCaptureProperty
// with CV_CAP_PROP_FRAME_HEIGHT and CV_CAP_PROP_FRAME_WIDTH
// as property_id
cvResizeWindow("Capturing Image ...",
(int) cvGetCaptureProperty(input_camera, CV_CAP_PROP_FRAME_HEIGHT),

(int) cvGetCaptureProperty(input_camera, CV_CAP_PROP_FRAME_WIDTH));

while(frame != NULL)
{
//Shows a frame
cvShowImage("Capturing Image ...", frame);


//Checks if ESC is pressed and gives a delay
// so that the frame can be displayed properly

key = cvWaitKey(10);
if(key == 27)

break;
//Grabs and returns the next frame
frame = cvQueryFrame(input_camera);
}

//Release cvCapture structure
cvReleaseCapture(&input_camera);

//Destroy the window
cvDestroyWindow("Capturing Image ...");


return 0;
}

Compiling (in the working directory):
$ g++ -Wall trialcam1a.cpp -o trialcam1a `pkg-config --cflags --libs opencv`

Running (in the working directory):
$ ./trialcam1a

Screenshot:


dmesg when I plugged USB Camera:
usb 3-1: new full speed USB device using uhci_hcd and address 2
usb 3-1: configuration #1 chosen from 1 choice
Linux video capture interface: v2.00
pwc: Philips webcam module version 10.0.13 loaded.
pwc: Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.
pwc: Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,
pwc: the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.
pwc: Logitech QuickCam 4000 Pro USB webcam detected.
pwc: Registered as /dev/video0.
usbcore: registered new interface driver Philips webcam
usbcore: registered new interface driver snd-usb-audio

Camera driver: pwc.
Camera interface: v4l (Video for Linux).

References:
  • OpenCV wiki (http://opencvlibrary.sourceforge.net)
  • Introduction to programming with OpenCV, by Gady Agam (http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html)
  • Linux-uvc-devel mailing list, January 2007 archieve, e-mail posting by Jose Luis Landabaso

3 comments:

Anonymous said...

Pak Henry, ketika saya coba program diatas muncul peringatan sbb:

Linking...
coba.obj : error LNK2001: unresolved external symbol _cvDestroyWindow
coba.obj : error LNK2001: unresolved external symbol _cvReleaseCapture
coba.obj : error LNK2001: unresolved external symbol _cvWaitKey
coba.obj : error LNK2001: unresolved external symbol _cvShowImage
coba.obj : error LNK2001: unresolved external symbol _cvResizeWindow
coba.obj : error LNK2001: unresolved external symbol _cvGetCaptureProperty
coba.obj : error LNK2001: unresolved external symbol _cvNamedWindow
coba.obj : error LNK2001: unresolved external symbol _cvQueryFrame
coba.obj : error LNK2001: unresolved external symbol _cvCreateCameraCapture
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/contoh.exe : fatal error LNK1120: 10 unresolved externals
Error executing link.exe.

kira2 kenapa ya pak?
terima kasih sebelumnya..

Unknown said...

Halo,
Anda pakai Visual C++ ya? Anda harus memasukkan OpenCV Library ke environment VC++. Caranya sudah ada di wikinya OpenCV: http://opencv.willowgarage.com/wiki/VisualC%2B%2B

hii said...

how to analyse a video from webcamera in opencv on ubuntu