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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!