Solve cap_ IMAGES: can’t find starting number (in the name of file)
1. Solutions
1. Confirm that the output file type matches FourCC. For details, please refer to the official website http://www.fourcc.org/codecs.php 。
simply use. Avi with videowriter:: FourCC (‘m ‘,’ J ‘,’ p ‘,’ g ‘)
I found a post on the Internet saying that FourCC was used in the old version, but it’s not used now. It’s wrong. For opencv4.3. X, FourCC is still used. When compiling, add videowriter:: FourCC (‘x’, ‘x’, ‘x’, ‘x’) to the class name.
2. If step 1 is correct but still reports an error:
linux platform: reinstall ffmpeg, then recompile opencv and install it. When compiling opencv, cmake needs to add the parameter – dwith_ FFMPEG=ON。
window platform: the output file type can be changed to. MP4. This method is lazy and does not need to install ffmpeg.
The following explains in detail why the lazy law can take effect.
2. Error code
#define VIDEO_OUTPUT_PATH "D:\\test_project\\output.avi"
int main()
{
Mat frame;
frame = imread("xxx.jpg");
VideoWriter vWriter(VIDEO_OUTPUT_PATH, CAP_OPENCV_MJPEG, 30, Size(frame.cols, frame.rows));
//Using CAP_OPENCV_MJPEG for the above parameters is wrong haha, VideoWriter::fourcc('M', 'J', 'P', 'G') is the correct parameter
int frameNum = 90;
while (frameNum) {
vWriter << dst;
frameNum--;
}
return 0;
}
3. Error 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. Error analysis
The above two sentences are error messages at runtime
the first sentence is the exception received when videowriter:: open is running
the second sentence is the specific exception content:
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’
we can get the following information from the error log:
the error log is generated by the file cap_ Images.cpp line 253 is printed. It seems that there is a problem with the file name. Specifically, it is the </ OL> printed in the CV:: icvextractpattern function
Next, let’s look at the opencv source code and find out what’s wrong with my file name
5. Opencv source code analysis
//The icvExtractPattern function is found in opencv\modules\videoio\src\cap.cpp
std::string icvExtractPattern(const std::string& filename, unsigned *offset)
{
size_t len = filename.size(); //record the length of the filename
。。。。。。 // a bunch of operations, don't care
pos = filename.rfind('/'); //find the last "/" in the file name
#ifdef _WIN32
if (pos == std::string::npos)
pos = filename.rfind('\\'); //for window systems, find the last "\\" in the filename
#endif
if (pos ! = std::string::npos)
pos++;
else
pos = 0;
while (pos < len && !isdigit(filename[pos])) pos++; //key point of filename error
if (pos == len) //throw exception if position is equal to filename length
Translated with www.DeepL.com/Translator (free version)
{
CV_Error_(Error::StsBadArg, ("CAP_IMAGES: can't find starting number (in the name of file): %s", filename.c_str()));
}
}
The above code should find the file type to be output from the output file name
because the key point while (POS & lt; len && amp; ! isdigit(filename[pos])) pos++; The implementation here is that if the character referred to by POS in the file name is not a number, then POS will point to the next character with + +. It seems that the reason is to find the file name with number of children
therefore, an idea occurred. Set the file type of the output file to MP4, and then the problem was solved.
#define VIDEO_ OUTPUT_ PATH “D:\test_ Project \ output. Avi ”
is changed to:
#define video_ OUTPUT_ PATH “D:\test_ project\output.mp4”
If it is helpful, you may wish to praise and pay attention to support.