Opencv Open the camera

  • View usb Video
eric@eric-PC:~/Documents/work/linux-c/opencv_demo/摄像头$ ls /dev/video*
/dev/video0  /dev/video1
  • Code structure
eric@eric-PC:~/Documents/work/linux-c/opencv_demo/摄像头$ tree
.
├── Makefile
└── src
    └── main.cpp

1 directory, 2 files
  • main.cpp
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>

using namespace cv;
using namespace std;
 
int main()
{
    
    
	Mat frame;
    cout << "Opencv Version:" << CV_VERSION << endl;
	VideoCapture camera(0);    // 打开摄像头
	if(!camera.isOpened()){
    
        // 判断打开成功
		cout << "open camera failed. " << endl;
		return -1;
	}
	while(true){
    
    
		// 读取图像帧至frame
		camera >> frame;    
		if(!frame.empty()){
    
    
			imshow("camera", frame);
		}
		// 等待按键退出 30ms
		if(waitKey(30) > 0)	{
    
    
			cout << "break. " << endl;
			break;
		}
	};
	camera.release();
    return 0;
}

If tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout. appears, just change the USB compatibility of the virtual machine to USB3.0!


Guess you like

Origin blog.csdn.net/pyt1234567890/article/details/109611920