How to use OpenCV to capture and display images from a camera

http://opencv.willowgarage.com/wiki/CameraCapture

How to use OpenCV to capture and display images from a camera

 


 

  • Here is a simple framework to connect to a camera and show the images in a Window.

 

 #include "cv.h" 
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}


 


 

How to control camera parameters using Opencv and videoinput lib (only for windows)

 


Sarin Sukumar A DSP Engineer - sarinsukumar@gmail.com

  • Information to control the camera parameters from program.

User can control Output format of the camera (YUV2, RGB etc), Brightness, exposure, autofocus, zoom, white balance etc. I have done it for USB cam and OpenCV 2.0. Opencv uses “cvCaptureFromCAM” to access camera. In windows it uses directshow or Video-for-Windows (VFW). For my USB cam “cvCaptureFromCAM” was using directshow and the function call goes to “cvCreateCameraCapture_DShow” in the file ” cvcap_dshow.cpp”. Include this file in your source and use the function “cvCreateCameraCapture_DShow” directly. Sometime you may need to rename the function. This function uses videoinput library to control camera in windows. http://muonics.net/school/spring05/videoInput/. You can use camera parameter setting functions from “cvCreateCameraCapture_DShow”, see “videoinput.h” for reference which is in “OpenCV2.0\3rdparty\include”. I modified it like following

 

bool CvCaptureCAM_DShow::open( int _index ) { '' * ''
bool result=false;
long min=0, max=0, currentValue=0, flags=0, defaultValue=0, stepAmnt=0;
close();
#ifdef DEFAULT
VI.deviceSetupWithSubtype(_index,640,480,_YUY2);
#endif
#ifdef MEDIUM
VI.deviceSetupWithSubtype(_index,1280,1024,_YUY2);
#endif
#ifdef ABOVE_MEDIUM
VI.deviceSetupWithSubtype(_index,1600,1200,_YUY2);
#endif
#ifdef HIGH
VI.deviceSetupWithSubtype(_index,2048,1536,_YUY2);
#endif
//VI.showSettingsWindow(_index);
//custome code
if( !VI.isDeviceSetup(_index) ) { '' . ''o return false; } '' ''VI.setVideoSettingFilter(_index,VI.propBrightness,6,2,false);
VI.setVideoSettingFilter(_index,VI.propContrast,5,2,false);
VI.setVideoSettingFilter(_index,VI.propHue,0,2,false);
VI.setVideoSettingFilter(_index,VI.propSaturation,6,2,false);
VI.setVideoSettingFilter(_index,VI.propSharpness,9,2,false);
VI.setVideoSettingFilter(_index,VI.propGamma,8,2,false);
VI.setVideoSettingFilter(_index,VI.propZoom,4,2,false);
VI.setVideoSettingFilter(_index,VI.propIris,4,2,false);
'' ''VI.setVideoSettingFilter(_index,VI.propPan,6,2,false);
VI.setVideoSettingFilter(_index,VI.propRoll,-2,1,false);
VI.setVideoSettingFilter(_index,VI.propTilt,5,1,false);
'' * ''index = _index; return true; '' ''} ''


There are other settings functions also refer “videoinput.h” for reference which is in “OpenCV2.0\3rdparty\include” If you want to get information about the setting parameters you can use

“getVideoSettingFilter()”

Make this line of code like this

 

struct SuppressVideoInputMessages { * . SuppressVideoInputMessages() { videoInput::setVerbose(true); } };

So that you will get all the debugging information in the DOS terminal If you want to set specific subtype then use the library that I modified Otherwise the library will automatically select the available subtype.

VI.deviceSetupWithSubtype(_index,1600,1200,_YUY2);

Camera property Settings.rar

posted @ 2012-04-08 11:12  lawdong  Views(820)  Comments(0Edit  收藏  举报
http://home.cnblogs.com/ing/