opencv错误打开文件找不到包含路径'D://wade//frame%3d.jpg'的文件

时间:2017-02-17 16:10:53

标签: c++ opencv image-processing

我正在研究我的阅读图像序列。有一件事让我很困惑。我得到了这个错误,我不知道如何解决它。而所有其他答案都无法解决我的问题。

  

[image2 @ 0000015ee5876620]找不到路径为“D://wade//frame%3d.jpg”的文件,索引范围为0-4   警告:打开文件时出错(../../ modules / highgui / src / cap_ffmpeg_impl.hpp:537)

任何人都有像我一样的经历吗?你是怎么解决的?谢谢

顺便说一句,这是我按顺序阅读图像的代码。也许是代码问题。

#include <iostream>   
#include <opencv2/opencv.hpp>   
using namespace cv;
using namespace std;

int main()
{
string first_file = "D://wade//frame%3d.jpg";
VideoCapture sequence(first_file);

Mat image;
namedWindow("Image sequence");

while (1){
    sequence >> image;
    if (image.empty()) break;
    imshow("Image sequence", image);
    waitKey(5);
}

cout << "End of Sequence" << endl;
waitKey();

return 0;
}

赞赏帮助:)

3 个答案:

答案 0 :(得分:1)

尝试使用

\\ 

而不是

//

还尝试更改文件名。

答案 1 :(得分:1)

%3d开始寻找001,002,003和004。 如果你有frame100.jpg,它将无法找到。

答案 2 :(得分:-1)

问题是您将无效的文件名传递给VideoCapture构造函数。 //在Windows上不是有效的路径分隔符,而是使用\(在C和C ++中的字符串文字中将其转义为\\)。但更重要的是,%3d仅适用于您没有使用的C风格的printf和scanf函数。

要做你正在尝试的事情,你需要更像这样的东西:

#include <string>
#include <sstream>
#include <iomanip>

std::ostringstream filename;
filename << "frame" << std::setw(3) << std::setfill('0') << SomeNumberHere << ".jpg";

std::string first_file = "D:\\wade\\" + filename.str();

或者这个:

#include <cstdio>

const char *fmt = "frame%3d.jpg";
int sz = std::snprintf(NULL, 0, fmt, SomeNumberHere);
std::string filename(sz, '\0');
std::snprintf(&filename[0], sz, fmt, SomeNumberHere);

std::string first_file = "D:\\wade\\" + filename;