opencvcvVideoCapture_0">摄像头原始数据读取——opencv(cv::VideoCapture)
测试代码test.cpp
#include <iostream>
#include <string>#include <opencv2/opencv.hpp>std::string pixeformatcodec2string(int codec)
{char pixeformat_name[5] = { (char)((codec >> 0) & 0xFF),(char)((codec >> 8) & 0xFF),(char)((codec >> 16) & 0xFF),(char)((codec >> 24) & 0xFF), '\0' };return std::string(pixeformat_name);
}int main(int argc, const char** argv)
{cv::VideoCapture cap; //打开video0设备,即/dev/video0 cap.open(0,cv::CAP_V4L2);std::cout<<"video width="<<cap.get(cv::CAP_PROP_FRAME_WIDTH)<<std::endl<<"video height="<<cap.get(cv::CAP_PROP_FRAME_HEIGHT)<<std::endl<<"video fps="<<cap.get(cv::CAP_PROP_FPS)<<std::endl<<"video pixeformat="<<pixeformatcodec2string((unsigned int)cap.get(cv::CAP_PROP_FOURCC))<<std::endl;//设置视频设备格式cap.set(cv::CAP_PROP_FRAME_WIDTH, 1280);cap.set(cv::CAP_PROP_FRAME_HEIGHT, 720);cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('Y', 'U', 'Y', 'V'));cap.set(cv::CAP_PROP_FPS, 10);std::cout<<std::endl;std::cout<<"video width="<<cap.get(cv::CAP_PROP_FRAME_WIDTH)<<std::endl<<"video height="<<cap.get(cv::CAP_PROP_FRAME_HEIGHT)<<std::endl<<"video fps="<<cap.get(cv::CAP_PROP_FPS)<<std::endl<<"video pixeformat="<<pixeformatcodec2string((unsigned int)cap.get(cv::CAP_PROP_FOURCC))<<std::endl;if (!cap.isOpened()){std::cerr<<"video open failed"<<std::endl;return -1;}cv::namedWindow("Video", 1);cv::resizeWindow("Video",cv::Size(1280,720));for (;;){cv::Mat frame;//获取视频帧数据cap >> frame; if (frame.empty()){return 0;}//显示图像 cv::imshow("Video", frame);if (cv::waitKey(25) >= 0){break;}}cv::destroyAllWindows();cap.release();return 0;
}