Here is a sample application that utilizes OpenCV in my customized Debian disto. Sample codes are posted below. The demo simply displays images captured by the camera. This can be extended to utilize other opencv functions.
hello_camera.py
import cv2.cv as cv cv.NamedWindow( "mycamera", 1 ) capture = cv.CreateCameraCapture( 0 ) while True: img = cv.QueryFrame( capture ) cv.ShowImage( "mycamera", img ) if cv.WaitKey(20) > 0: break cv.DestroyWindow( "mycamera" )
Below is the C/C++ equivalent of the above Python script.
hello_camera.cpp
#include <opencv2/highgui/highgui.hpp> using namespace cv; int main( int argc, const char** argv ) { cvNamedWindow( "mycamera", 1 ); // create camera window CvCapture *capture = cvCreateCameraCapture( 0 ); // start capturing frames from camera (i.e. /dev/video0 device) while(1){ IplImage* img = cvQueryFrame( capture ); // grab and retrieve frame cvShowImage( "mycamera", img ); // display image within window if( waitKey(20) > 0 ) // press any key to exit (interval=20ms) break; } // cleanups cvReleaseCapture( &capture ); // stop capturing/reading // not needed in python (?) cvDestroyWindow( "mycamera" ); // end camera window return 0; }
---------------------------------------------------------------------------
- thanks to hilite.me for formating the codes. (best viewed with Firefox browser)
- webcam can also be tested with guvcview (Applications Menu->Multimedia->guvcview)
No comments:
Post a Comment