随笔 - 156  文章 - 0  评论 - 1  阅读 - 12万

opencv读视频失败的原因

示例代码

#include <opencv2/core/core.hpp>      
#include <opencv2/highgui/highgui.hpp>      
#include <opencv2/imgproc/imgproc.hpp>     
#include <opencv2/imgcodecs.hpp>  
#include <opencv2/videoio.hpp>   
#include <iostream>    
using namespace std;
using namespace cv;

int main()
{
#if 0
    //-------------【1】读取视频文件-------------
    VideoCapture capture;
    capture.open("xxxpath/input.h264");
    //方式二:VideoCapture capture("xxxpath/input.h264");
 
    //-------------【2】判断视频读取是否正确-------------
    if (!capture.isOpened())
    {
        cout << "打开视频文件失败,请重新输入正确路径!" << endl;
        system("pause");
        return -1;
    }
 
    //-------------【3】获取视频相关信息-------------
    //获取视频相关信息——帧数
    long nTotalFrame = capture.get(CV_CAP_PROP_FRAME_COUNT);
    cout << "帧数 = " << nTotalFrame << endl;
    //获取视频相关信息——帧像素宽/高
    int frameHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
    int frameWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);
    cout << "帧像素高 = " << frameHeight << endl;
    cout << "帧像素宽 = " << frameWidth << endl;
    //获取视频相关信息——帧率
    double FrameRate = capture.get(CV_CAP_PROP_FPS);
    cout << "帧率 = " << FrameRate << endl;
 
    //--------------【4】循环显示每一帧---------------
    long nCount = 1;    //计数,当前帧号

    bool flag = true;
    while (flag)
    {
        //输出当前帧号
        cout << "当前帧号: " << nCount << endl;
        Mat frameImg;//定义一个Mat变量,用于存储每一帧的图像
        capture >> frameImg;
        //判断当前读取文件
        if (frameImg.empty())
        {
            break;
        }
        imshow("读取视频", frameImg);    //显示当前帧    
        //按下键盘上Q或q键退出
        if (char(waitKey(40)) == 'q' || char(waitKey(40)) == 'Q') //每帧画面存在40ms,即1秒25帧
        {
            break;
        }
        nCount++;

        flag = false;
    }
    //视频释放
    capture.release();
#endif

    std::string path = "xxxpath";
    cv::VideoCapture * m_Cap = new cv::VideoCapture(path);
    if (m_Cap) {
        std::cout << "video: " << path << " is opened !" << std::endl;
    }
    else {
        std::cout << "video: " << path << " opened failed !" << std::endl;
    }

    if( !m_Cap->isOpened() )
    {
        std::cout << "***Could not initialize capturing...***\n";
    }


    cv::Mat frame;

    bool flag = true;
    while (m_Cap->read(frame) && flag) {
		
          //flag = false;
    }
    m_Cap->release();


    return 0;
}

以上程序在ubuntu电脑上能够跑通,正常读取视频,但是在开发板上不能读取,isOpened()函数返回false

查看原因是由于电脑上的libopencv_videoio库编译时携带了ffmpeg,而板子上没有,具体查看下图
电脑上的

板子上的

利用ffmpeg和opencv进行视频的解码播放
参考
https://www.jianshu.com/p/6ef3c18d61b0

问题参考
https://bbs.huaweicloud.com/forum/thread-99217-1-1.html

posted on   JJ_S  阅读(1472)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示