gpt4 book ai didi

c++ - 使用 opencv 同时显示多个摄像头提要?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:59:37 27 4
gpt4 key购买 nike

我有两个摄像头,需要同时在不同的窗口或同一窗口中显示视频。但是,使用以下代码,仅显示一个摄像头源 (camera(1))。有人可以指出我的代码中需要更改的内容,或者链接到可以达到预期效果的其他代码吗?

注意这不适用于立体视觉。

int main()
{

//initialize and allocate memory to load the video stream from camera
CvCapture *capture1 = cvCaptureFromCAM(0);

if( !capture1 ) return 1;

//create a window with the title "Video1"
cvNamedWindow("Video1");

while(true) {
//grab and retrieve each frames of the video sequentially
IplImage* frame1 = cvQueryFrame( capture1 );

if( !frame1 ) break;

//show the retrieved frame in the "Video1" window
cvShowImage( "Video1", frame1 );

//wait for 40 milliseconds
int c = cvWaitKey(40);

//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if((char)c==27 ) break;
}

//initialize and allocate memory to load the video stream from camera
CvCapture *capture2 = cvCaptureFromCAM(1);

if( !capture2 ) return 1;

//create a window with the title "Video2"
cvNamedWindow("Video2");

while(true) {
//grab and retrieve each frames of the video sequentially
IplImage* frame2 = cvQueryFrame( capture2 );

if( !frame2 ) break;

//show the retrieved frame in the "Video2" window
cvShowImage( "Video2", frame2 );

//wait for 40 milliseconds
int c = cvWaitKey(40);

//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if((char)c==27 ) break;
}

//destroy the opened window
cvDestroyWindow("Video1");
cvDestroyWindow("Video2");
//release memory
cvReleaseCapture( &capture1 );
cvReleaseCapture( &capture2 );

return 0;

//VideoCapture1();
//VideoCapture2();


}

最佳答案

澄清一下,您使用的是 C 还是 C++?如果您使用 C++,请使用 OpenCV 的 C++ 接口(interface)。

下面的例子对我有用:

#include <opencv2/opencv.hpp>

int main()
{
//initialize and allocate memory to load the video stream from camera
cv::VideoCapture camera0(0);
cv::VideoCapture camera1(1);

if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;

while(true) {
//grab and retrieve each frames of the video sequentially
cv::Mat3b frame0;
camera0 >> frame0;
cv::Mat3b frame1;
camera1 >> frame1;

cv::imshow("Video0", frame0);
cv::imshow("Video1", frame1);

//wait for 40 milliseconds
int c = cvWaitKey(40);

//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}

return 0;
}

关于c++ - 使用 opencv 同时显示多个摄像头提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13925867/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com