在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