VS2010 + OpenCV2.4.9打開攝像頭:
方法一:
參考博文:OpenCV 獲取攝像頭,新建窗口顯示攝像頭視頻,內含幾個函數的介紹~
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main()
{
//declare IplImage pointer
IplImage *pFrame = NULL;
//obtain the camera
CvCapture *pCapture = cvCreateCameraCapture(0);
//create a window
cvNamedWindow("video",1);
//show the video
while(1)
{
pFrame = cvQueryFrame(pCapture);
if(!pFrame)
break;
cvShowImage("video",pFrame);
//char c = waitKey(33);
char c = cvWaitKey(33);
if(c == 27) //ASCII Esc key
break;
}
cvReleaseCapture(&pCapture);
cvDestroyWindow("video");
return 0;
}
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main()
{
VideoCapture cap(0);
Mat frames;
while(1)
{
cap >> frames;
imshow("video",frames);
char c = cvWaitKey(33);
if(c == 27) //ASCII Esc key
break;
}
cvDestroyWindow("video");
return 0;
}
第一種方法應該是一種比較經典和保險的辦法。方法二較簡單,但是應該存在缺陷。另外,通過IPLImage* pFrame=cvQueryFrame(pCaptrue) 所獲取圖像pFrame是倒着的,雖然cvShowImage顯示爲正着的,以及cvSaveImage都爲正着的,是因爲這兩個函數先進行了數據排列判斷,但實際數據是倒着排列的。可以查看pFrame.origin是1。而正常cvLoadImage進來的圖像pFrame.origin是0。因此,對於cvQueryFrame後獲取的圖像pFrame先要進行flip,即:cvFlip(pFrame);此時圖像數據爲直觀所顯示的圖像一致了。