directshow+opencv显示usb的摄像头代码(并保存视频中的图片)

<span style="font-size:18px;">#include "camerads.h"
#include <highgui.h>
#include<cv.h>
#include<cxcore.h>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
	int cam_count;

	//仅仅获取摄像头数目
	cam_count = CCameraDS::CameraCount();
	printf("There are %d cameras.\n", cam_count);
	char imgname1[25];
	char imgname2[25];

	//获取所有摄像头的名称
	for (int i = 0; i < cam_count; i++)
	{
		char camera_name[1024];
		int retval = CCameraDS::CameraName(i, camera_name, sizeof(camera_name));

		if (retval >0)
			printf("Camera #%d's Name is '%s'.\n", i, camera_name);
		else
			printf("Can not get Camera #%d's name.\n", i);
	}


	if (cam_count == 0)
		return -1;

	CCameraDS camera;
	CCameraDS camera1;
	//打开第一个摄像头
	//if(! camera.OpenCamera(0, true)) //弹出属性选择窗口
	if (!camera.OpenCamera(0, false, 320, 240)) //不弹出属性选择窗口,用代码制定图像宽和高
	{
		fprintf(stderr, "Can not open camera.\n");
		return -1;
	}
	camera1.OpenCamera(2, false, 320, 240);
	
	cvNamedWindow("camera");
	cvNamedWindow("camera1");
	int i = 0;
	int j = 0;
	while (1)
	{
		//获取一帧
		IplImage *pFrame = camera.QueryFrame();
		IplImage *pFrame1 = camera1.QueryFrame();//提取两个摄像头的部分,,,,
		//下面是保存图片的额过程
		if (j % 20 == 0)//每隔20次去
		{
			sprintf(imgname1, "%s%d%s", "D:\\imgl\\imgleft", ++i, ".jpg");
			sprintf(imgname2, "%s%d%s", "D:\\imgr\\", ++i, ".jpg");
			cvSaveImage(imgname1, pFrame);
			cvSaveImage(imgname2, pFrame1);
		}
		j++;
		//显示
		cvShowImage("camera", pFrame);
		cvShowImage("camera1", pFrame1);
		if (cvWaitKey(20) == 'q')
			break;
	}
	camera.CloseCamera(); //可不调用此函数,CCameraDS析构时会自动关闭摄像头
	camera1.CloseCamera(); //可不调用此函数,CCameraDS析构时会自动关闭摄像头

	cvDestroyWindow("camera");
	cvDestroyWindow("camera1");
	return 0;
}</span>

你可能感兴趣的:(directshow+opencv显示usb的摄像头代码(并保存视频中的图片))