在centos7下验证VideoCapture功能。
1 opencv处理视频时要使用ffmpeg,这里使用添加源的方式安装,分为3步
1.1 先安装EPEL Release,使用其他的repo源,所以需要EPEL支持
yum install -y epel-release
#如果出现缺少Code提示,可以:
sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
#安装完成之后,可以查看是否安装成功
yum repolist
1.2 安装Nux-Dextop源
#导入一个Code
sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
#安装nux-dextop 源
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
#查看repo源是否安装成功
yum repolist
1.3 yum安装ffmpeg
yum install -y ffmpeg
yum install -y ffmpeg-dev
2 还要安装gtk库
yum install gtk+-devel gtk2-devel
3 此时可以编译opevcv源码,在源码根目录下建立一个build目录,进入build目录执行
cmake ..
make
make install
到此处安装完成
4 验证程序
摄像头
#include <opencv2/opencv.hpp> #include <iostream> int main() { cv::VideoCapture capture; capture.open(0);//open 根据编号打开摄像头 std::cout<<"-------------"<<std::endl; if (!capture.isOpened()) { std::cout << "Read video Failed !" << std::endl; return 0; } cv::Mat frame; cv::namedWindow("video test"); int frame_num = 800; for (int i = 0; i < frame_num - 1; ++i) { capture >> frame; //capture.read(frame); imshow("video test", frame); if (cv::waitKey(30) == 'q') { break; } } cv::destroyWindow("video test"); capture.release(); return 0; }
本地文件
#include <opencv2/opencv.hpp> #include <iostream> int main() { cv::VideoCapture capture; capture.open("test.mp4"); std::cout<<"-------------"<<std::endl; if (!capture.isOpened()) { std::cout << "Read video Failed !" << std::endl; return 0; } cv::Mat frame; cv::namedWindow("video test"); capture.get(cv::CAP_PROP_FRAME_COUNT); std::cout << "total frame number is: " << frame_num << std::endl; for (int i = 0; i < frame_num - 1; ++i) { capture >> frame; //capture.read(frame); imshow("video test", frame); if (cv::waitKey(30) == 'q') { break; } } cv::destroyWindow("video test"); capture.release(); return 0; }
5 说明
运行时出现
Unable to stop the stream: Inappropriate ioctl for device
是因为没有安装ffmpeg-dev导致。
不能播放时确认是否安装yum install gtk+-devel gtk2-devel
来源:https://www.cnblogs.com/mingzhang/p/7460465.html