OpenCV打开摄像头(二)

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);此时图像数据为直观所显示的图像一致了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章