前几天安装的OpenCV《小白-《学习OpenCV3》-安装OpenCV》在读取视频的时候一直打开视频文件失败。折腾了一下,发现是在进行配置的时候找不到ffmpeg。 需要在安装依赖的时候多安装几个(不知道具体是缺失哪个依赖导致的问题):
sudo apt-get install libgtk-dev -y
sudo apt-get install build-essential git cmake pkg-config -y
sudo apt-get install libpng-dev -y
sudo apt-get install libjpeg-dev -y
sudo apt-get install libtiff-dev -y
sudo apt-get install libjasper-dev -y
sudo pip3 install numpy
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test
sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libfontconfig1-dev libcairo2-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
cmake配置时,ffmpeg应该会显示YES
opencv.pc需要去掉几个链接库:
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.5.3
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_stitching -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
~
有些版本可能在安装过程中会报错,这里给出安装的源码压缩包: opencv:https://download.csdn.net/download/y1757655788/20666510 opencv_contrib:https://download.csdn.net/download/y1757655788/20666517
example.cpp:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
int main(int argc, char **argv)
{
cv::namedWindow("example",cv::WINDOW_AUTOSIZE);
cv::VideoCapture cap;
cap.open(std::string(argv[1]));
cv::Mat frame;
while(1)
{
cap >> frame;
if(frame.empty())
{
printf("frame is empty!\r\n");
break;
}
cv::imshow("example",frame);
if(cv::waitKey(33)>=0) break;
}
}
编译之后运行
|