[英]how to access images in sequence using loop in openCV
伙計們,我遇到了有關按順序訪問圖像的問題。 我有一些圖像,它們的名稱隨編號增加而改變,即cube_0.jpg,cub_1.jpg,....等。 現在我要一張一張地訪問每個圖像並顯示。 以下是我兩天以來一直在玩的代碼,不知道如何處理這種情況或此問題出了什么問題。
ostringstream s;
for (int fileNumber = 0; fileNumber<=40; fileNumber++)
{
s<<"\"cube_"<<fileNumber<<"\.jpg\""<<endl;
string fullfileName(s.str());
images[i] = fullfileName;
}
stringstream ss;
cout<<"file name"<<images[0]<<endl;
for (int file = 0; file<41; file++)
{
string str = images[file];
cout<<"str "<<str<<endl;
img_raw = imread(ss.str(), 1); // load as color image Error
cout<<"Done"<<endl<<"size"<<img_raw.size();
system("pause");
}
直到到達“ img_raw = imread(ss.str())”為止,這段代碼才能正常運行,現在這行基本上已經阻礙了我訪問文件。 由於imread需要“ string&filename”,因此我執行了stringstream操作,但沒有任何效果!
任何幫助將不勝感激!
有一些錯誤。
您的stringstream ss
為空。 您已聲明它,但未填充任何值。 我很確定你的意思是imread(str, 1);
而不是imread(ss.str(), 1);
在第一個for循環中,您不斷將文件名打印到ostringstream,所以它是這樣的:
0: “ cube_0.jpg \\”
1: “ cube_0.jpg \\”“ cube_1.jpg \\”
2: “ cube_0.jpg \\”“ cube_1.jpg \\”“ cube_2.jpg \\”
...
所以ostringstream只是在不斷增長。 需要在循環中聲明ostringstream,以便每次迭代都將其清除。
編輯代碼:
string images[41];
Mat img_raw;
for (int fileNumber = 0; fileNumber < 41; fileNumber++)
{
stringstream ss;
ss << "\cube_" << fileNumber << "\.jpg" << endl;
string fullfileName;
ss >> fullfileName;
images[fileNumber] = fullfileName;
}
for (int file = 0; file < 41; file++)
{
cout << "Loading " << images[file] << endl;
img_raw = imread(images[file], 1);
if (!img_raw.empty())
{
cout << "Successfully loaded " << images[file] << " with size " << img_raw.size() << endl;
}
else
{
cout << "Error loading file " << images[file] << endl;
}
system("pause");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.