C++ 读取文件夹下的图片并排序返回列表
- 注:图片名为前缀_数字.后缀
- 如:/home/txt/Desktop/Stereo/work_txt/DATA/Image/Left/Left_213.bmp
int ImgtoDisp::imageNameLists(string filePath, vector<string>& nameArr)
{
vector<cv::String> fn;
cv::glob(filePath, fn, false);
size_t count = fn.size();
vector<string> nametemp;
vector<int> nameflag(count);
for (size_t num = 0; num < count; num++)
{
nameflag[num] = num;
}
if (count==0)
{
std::cout << "file " << filePath << " not exits"<<endl;
return -1;
}
for (int i = 0; i < count; ++i)
{
string::size_type iPos = fn[i].find_last_of('_') + 1;
string filename = fn[i].substr(iPos, fn[i].length() - iPos);
string name = filename.substr(0, filename.rfind("."));
nametemp.emplace_back(name);
}
for (size_t i = 0; i < count-1; i++)
{
for (size_t j = count-1; j > i; j--)
{
if (stoi(nametemp[j-1]) > stoi(nametemp[j]))
{
string temp1 = nametemp[j];
nametemp[j] = nametemp[j-1];
nametemp[j-1] = temp1;
int temp2 = nameflag[j];
nameflag[j] = nameflag[j-1];
nameflag[j-1] = temp2;
}
}
}
for (size_t i = 0; i < count; i++)
nameArr.push_back(fn[nameflag[i]]);
return 0;
}