解决 CAP_IMAGES: can't find starting number (in the name of file)

  • 1. 解决方法
  • 2.报错代码
  • 3.报错Log
  • 4.错误分析
  • 5.opencv源码分析

1. 解决方法

1.确认输出文件类型与fourcc需匹配。具体可参考官网http://www.fourcc.org/codecs.php。
简单点就用 .avi 配 VideoWriter::fourcc(‘M’, ‘J’, ‘P’, ‘G’)。
网上搜到一个贴说什么fourcc是旧版本用的,现在不用了,这种说发的是错的哈。对于Opencv4.3.x,依然采用fourcc。当编译不过时应把类名加上VideoWriter::fourcc(‘X’, ‘X’, ‘X’, ‘X’)。

2.第1步无误但还是报错的话:
Linux平台:重新安装ffmpeg,然后重新编译opencv并安装。opencv编译时 cmake需加上参数-DWITH_FFMPEG=ON。
Window平台:可将输出文件类型改为.mp4。此方法为偷懒法,无需安装ffmpeg。

以下详细解释偷懒法能生效的原因。

2.报错代码

#define VIDEO_OUTPUT_PATH "D:\\test_project\\output.avi" //我原来代码输出文件类型为.aviint main()
{Mat frame;frame = imread("xxx.jpg");// Mat数据保存为视频VideoWriter vWriter(VIDEO_OUTPUT_PATH, CAP_OPENCV_MJPEG, 30, Size(frame.cols, frame.rows));//上面参数使用CAP_OPENCV_MJPEG是错误的哈,VideoWriter::fourcc('M', 'J', 'P', 'G')才是正确的参数int frameNum = 90;  //定义我要存多少帧while (frameNum) {vWriter << dst;frameNum--;}return 0;
}

3.报错Log

[ERROR:0] global C:\build\master_winpack-build-win64-vc14\opencv\modules\videoio\src\cap.cpp (563) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:OpenCV(4.5.1) C:\build\master_winpack-build-win64-vc14\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): D:\\test_project\\output.avi in function 'cv::icvExtractPattern'

4.错误分析

上面两句是运行时的报错信息。
第一句就是VideoWriter::open运行时收到异常。
第二句是具体的异常内容:

OpenCV(4.5.1) C:\build\master_winpack-build-win64-vc14\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): D:\test_project\output.avi in function ‘cv::icvExtractPattern’
我们可以从报错log中得出以下信息:

  1. 错误log由文件cap_images.cpp 第253行打印
  2. 貌似是文件名有点问题
  3. 具体是cv::icvExtractPattern函数中打印的

那么接下来就看下opencv源码,查一查我的文件名到底有什么问题

5.opencv源码分析

//在opencv\modules\videoio\src\cap.cpp 中找到 icvExtractPattern 函数
std::string icvExtractPattern(const std::string& filename, unsigned *offset)
{size_t len = filename.size(); //记录文件名长度。。。。。。//一堆操作,不用关心pos = filename.rfind('/');      //找到文件名中最后一个"/"的位置
#ifdef _WIN32if (pos == std::string::npos)pos = filename.rfind('\\');      //对于window 系统,找到文件名中最后一个"\\"的位置
#endifif (pos != std::string::npos)pos++;elsepos = 0;while (pos < len && !isdigit(filename[pos])) pos++;  //文件名错误的关键点if (pos == len)     //如果位置等于文件名长度,则抛出异常{CV_Error_(Error::StsBadArg, ("CAP_IMAGES: can't find starting number (in the name of file): %s", filename.c_str()));}。。。。。。//一堆操作,不用关心
}

以上代码实现的应该是从输出文件名找到需要输出的文件类型。
因为关键点 while (pos < len && !isdigit(filename[pos])) pos++; 这里实现的是如果文件名中 pos所指的字符不是数子,那么pos就++,指向下一个字符。看起来因该就是想要找到带数子的文件名。
因此灵机一动,将输出文件的文件类型设置为mp4,然后问题就解决了。

#define VIDEO_OUTPUT_PATH “D:\test_project\output.avi”
改为:
#define VIDEO_OUTPUT_PATH “D:\test_project\output.mp4”

如有帮助,不妨点赞关注以支持。

解决 CAP_IMAGES: can‘t find starting number (in the name of file)相关推荐

  1. CV报错:CAP_IMAGES: can‘t find starting number (in the name of file): x in function ‘icvExtractPattern‘

    在linux arm下报错: OpenCV(4.5.4-dev) /home/yg/share/prj/opencv/modules/videoio/src/cap_images.cpp:253: e ...

  2. openCV4.2.0 error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file)

    OpenCV(4.2.0-dev) ~/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAG ...

  3. 成功解决An error ocurred while starting the kernel

    成功解决An error ocurred while starting the kernel 目录 解决问题 解决思路 解决方法 解决问题 An error ocurred while startin ...

  4. 解决ubuntu18.04卡在“starting Gnome Display Manager“

    解决ubuntu18.04卡在"starting Gnome Display Manager" 问题场景: 在ubuntu安装NVIDIA驱动后,重启电脑后卡在"star ...

  5. 解决IntelliJ IDEA报错Failed to read candidate component class: file [ ]; nested exception is org.

    解决IntelliJ IDEA报错Failed to read candidate component class: file [ ]; nested exception is org.springf ...

  6. 解决微信返回码为40113,错误信息errmsg:unsupported file type

    解决微信返回码为40113,错误信息"errmsg":"unsupported file type" // PHP代码 $file_name = md5(mic ...

  7. 报错解决:SyntaxError: Non-UTF-8 code starting with ‘\xe7‘

    今天抓取数据时使用re对数据进行提取时遇到的问题:syntaxError: Non-UTF-8 code starting with '\xe7',意思是有的中文字符无法转成utf-8的形式,如图所示 ...

  8. 解决java.lang.UnsupportedClassVersionError: Bad version number in .class file问题

    在启动tomcat服务器时, 报如下的错误: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading ...

  9. php deprecated是什么意思,解决:PHP Deprecated: Comments starting with '#' are deprecated in ……...

    错误信息:PHP Deprecated:  Comments starting with '#' are deprecated in /usr/local/php/etc/php.ini on lin ...

  10. 解决:SyntaxError: Non-UTF-8 code starting with '\xe6' in file

    pycharm加注释报错SyntaxError: Non-UTF-8 code starting with '\xe6' in file 处理 代码最上面加上编码格式 #coding:utf-8

最新文章

  1. 在PyPI上发布自己的python包
  2. ping 工具开发日记(1)
  3. html5如何绘制饼图,如何在HTML5中创建“饼图”?
  4. oracle 更新数据更新不进去_关于微软及Oracle安全更新汇总的通知
  5. web.xml filter 不包含_Elasticsearch 之 Filter 与 Query 有啥不同?
  6. Linux之pwd命令
  7. [CATARC_2017] 第三周 残四周
  8. 23种经典设计模式UML类图汇总
  9. java smali_如何把java代码转换成smali代码
  10. 安装nodejs时:The error code is 2503.
  11. 海康威视摄像头 rtsp 地址格式
  12. 砸蛋程序php,砸金蛋:jQuery+PHP实现的砸金蛋中奖程序
  13. cursor android,Android Cursor用法
  14. idea中javaweb的jsp页面图片加载不出来的解决办法
  15. 关于判别分析的学习-距离判别法
  16. msp430流水灯c语言程序,MSP430单片机流水灯程序
  17. Tiny4412 在linux下的dnw使用
  18. 2022登高架设理论题库及答案
  19. 用7000字长文带你分析深圳二手房市场现况!
  20. 几个常见的js插件安装配置

热门文章

  1. 使用pyppeteer或者selenium时,chrome窗口最大化
  2. 电驴无法增加服务器怎么办,电驴连接不上服务器怎么办?
  3. 2018校招-挂面-H3C-技术支持工程师
  4. android studio sdk samples,qcloud-sdk-android-samples
  5. 520送男朋友什么礼物最好、送男友礼物清单
  6. 网盘上传文件服务器失败原因,百度网盘上传文件一直失败是什么情况?具体解决方法...
  7. 同频共振数据时代,AntDB数据库与永洪科技完成产品互认证
  8. mysql调整specified key_MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法...
  9. iOS开发:唯一标志符
  10. 算法篇-----粒子群算法