幾個程序使用的基本函數如下:
*******************************************************************
cvCreateCameraCapture
初始化從攝像頭中獲取視頻
CvCapture* cvCreateCameraCapture( int index );
- index 要使用的攝像頭索引。如果只有一個攝像頭或者用哪個攝像頭也無所謂,那使用參數-1便可以。
函 數cvCreateCameraCapture給從攝像頭的視頻流分配和初始化CvCapture結構。目前在Windows下可使用兩種接 口:Video for Windows(VFW)和Matrox Imaging Library(MIL); Linux下也有兩種接口:V4L和FireWire(IEEE1394)。
釋放這個結構,使用函數cvReleaseCapture。
返回值為一個
CvCapture
************************************************************************************************
CvCapture
視頻獲取結構
typedef struct CvCapture CvCapture;
結構CvCapture 沒有公共接口,它只能被用來作為視頻獲取函數的一個參數。
******************************************************************************
cvQueryFrame
如果抓取幀為空 break 打破循環
否則將抓取的那一幀顯示在創建的窗口上
cvShowImage("video",pFrame);
當前幀顯示后
char c=cvWaitKey(33);
if(c==27)break;
我們等待33ms
如果用戶觸發了按鍵,將按鍵的ASCII值給C
如果C為ESC(ASCII 為27)循環退出
最后釋放capture 和window
cvReleaseCapture(&pCapture);
cvDestroyWindow("video");
最后釋放占用資源
程序代碼如下:
======================================================================
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
int main(int argc, char** argv)
{
//聲明IplImage指針
IplImage* pFrame = NULL;
//獲取攝像頭
CvCapture* pCapture = cvCreateCameraCapture(-1);
//創建窗口
cvNamedWindow("video", 1);
//顯示視屏
while (1)
{
pFrame = cvQueryFrame(pCapture);
if (!pFrame)break;
cvShowImage("video", pFrame);
char c = cvWaitKey(33);
if (c == 27)break;
}
cvReleaseCapture(&pCapture);
cvDestroyWindow("video");
}
運行結果如下:
