1.虚拟机用指令打开USB摄像头及使用cheese工具
参考:虚拟机内Ubuntu如何打开摄像头+解决cheese工具黑屏问题_ubuntu cheese-CSDN博客
2.在代码中同时使用两个摄像头时遇到 can't open camera by index
VideoCapture camera0(0);
if( !camera0.isOpened() )
{cout << "camera0 Open failed \n";return 1;
} VideoCapture camera1(1);
// camera1.set(CAP_PROP_FRAME_WIDTH,320);
// camera1.set(CAP_PROP_FRAME_HEIGHT,240);
if( !camera1.isOpened() )
{cout << "camera1 Open failed \n";return 1;
}
在上述代码中把 VideoCapture camera1(1);改成VideoCapture camera1(2);就不会出现这样的问题;至于是什么原因还未得知(猜测:1个USB摄像头对应2个/dev/videox,第二个摄像头开始正好是/dev/video2);
3.从两个摄像头中取出画面帧,组合两个帧画面并显示出来
代码:
#include <opencv2/opencv.hpp>
#include<iostream>using namespace cv;
using namespace std;int main()
{VideoCapture camera0(0);if( !camera0.isOpened() ){cout << "camera0 Open failed \n";return 1;} VideoCapture camera1(2);if( !camera1.isOpened() ){cout << "camera1 Open failed \n";return 1;} while(true) {//grab and retrieve each frames of the video sequentiallyMat frame1,frame0,blended_img;bool bSuccess0 , bSuccess1;//camera1 >> frame1;bSuccess1 = camera1.read(frame1);if (!bSuccess1){cout << "111 不能从视频文件读取帧" << endl;break;}//camera0 >> frame0;bSuccess0 = camera0.read(frame0);if (!bSuccess0){cout << "000 不能从视频文件读取帧" << endl;break;}addWeighted(frame0, 0.3, frame1, 0.7, 0.0, blended_img);imshow("Video0", frame0);imshow("Video1", frame1);imshow("blended_img", blended_img);int c = waitKey(27);if(27 == char(c)) break;} return 0;
}