opencv读取视频及打不开视频的解决方法

  • 1、 安装ffmpeg
sudo apt install ffmpeg
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavutil-dev
  • 2、重新编译安装opencv
cmake -DCMAKE_BUILD_TYPE=RELEASE -D WITH_FFMPEG=ON ..
make -j4
sudo make install
  • 3、读取视频
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main(int argc, char **argv) {
    VideoCapture capture;
    Mat frame;
    frame = capture.open(argv[1]);
    if (!capture.isOpened())
    {
        return -1;
    }
    while (capture.read(frame))
    {
        cv::imshow("res", frame);
        int key = waitKey(10) & 0xff;
        if(key == 27) {
            break;
        }
    }
    destroyAllWindows();
    
    return 0;
}

你可能感兴趣的:(环境配置)