开发平台(Platform): VC++
额外使用到的函数库(Library Used): OpenCV
问题(Question):想使用OpenCV开启两个网络摄影机,但是却开不起来,目前最多只能开启一个。
程式码(Code):
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
using namespace cv;
void onMouse(int Event,int
x,int y,int
flags,void* param);
int main()
{
VideoCapture cap1(0);
VideoCapture cap2(1);
if(!cap1.isOpened()
&& !cap2.isOpened())
{
return 1;
printf("未搜寻到摄影机\n");
}
double
rate=cap1.get(CV_CAP_PROP_FPS);
bool stop(false);
Mat frame1;
Mat frame2;//(480,640,CV_8UC3,Scalar(0,0,0));
namedWindow("camera1");
namedWindow("camera2");
printf("P键:拍照储存\tn键:关闭视窗
\n");
//int delay=1000/rate;
while(!stop)
{
if(!cap1.read(frame1)
&&
!cap2.read(frame2))
{
break;
}
imshow("camera1",frame1);
imshow("camera2",frame2);
if
(waitKey(33)=='p')
{
Mat photo1,photo2;
imwrite("photo1.jpg",frame2);
imwrite("photo2.jpg",frame1);
namedWindow("photo1");
namedWindow("photo2");
//waitKey(100);
photo1=imread("photo1.jpg");
photo2=imread("photo2.jpg");
imshow("photo1",photo1);
imshow("photo2",photo2);
cvSetMouseCallback("photo",onMouse,NULL);
}
if(waitKey(33)=='n')
{
stop=true;
}
}
}
void onMouse(int Event,int
x,int y,int
flags,void* param)
{
if(Event==CV_EVENT_LBUTTONDOWN)
{
printf("(%d,%d)\n",x,y);
}
}