如何从OpenCV中的目录中依次读取文件并用于处理?
How to read files in sequence from a directory in OpenCV and use it for processing?
当我尝试编译这段代码时出现此错误-
error: no matching function for call to 'glob(std::__cxx11::string&, std::vector >&, bool)'
glob (folder, filename, false);
^
这是我使用的代码:
#include <vector>
#include <glob.h>
string folder = "/home/ragesh/C++ /calibration_laptop/images/*.jpg";
vector <string> filename;
glob(folder, filename, false);
if(count < filename.size())
{
img = imread(filename[count]);
if(img.empty())
{
cout << "\nimage loading failed.....!"<<"\n";
return 0;
}
imshow("image", img);
cvtColor ( img,gray, COLOR_BGR2GRAY );// gray scale the source image
vector<Point2f> corners; //this will be filled by the detected corners
bool patternfound = findChessboardCorners(gray, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK);
count ++;
}
尝试使用 CV::String
而不是 std::string
。
添加#include "cvstd.hpp"
,然后
cv::String new_folder_string(folder).
std::vector<cv::String> new_filename_vector.
glob(new_folder_string, new_filename_vector, false);
一些信息here(glob function), here(String class in OpenCV), here(example of use of glob with cv::String) and here(berak 的建议)。
编辑:我忘了提到您还需要将 filename
从 std::string
切换到 cv::String
。
函数 cv::glob
具有以下签名:
void cv::glob(cv::String pattern, std::vector<cv::String>& result, bool recursive = false)
由于pattern
是按值传递的,而一个cv::String
可以是constructed from a std::string
,所以第一个参数不是问题。一个临时的 cv::String
是自动构造的。
然而,第二个 -- result
-- 是,因为它被用作 non-const 参考。为了解决您的问题,您需要将 filename
设为 std::vector<cv::String>
。因为它代表了collection个文件名,我也建议用复数来命名:filenames
.
示例代码:
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
int main()
{
std::string folder("/home/ragesh/C++ /calibration_laptop/images/*.jpg");
std::vector<cv::String> filenames;
cv::glob(folder, filenames, false);
// Do something with filenames...
return 0;
}
更新:
我从签名和您问题上的标签得出结论,您正在寻找 OpenCV glob
(不完整的代码示例使这有些困难)。但是,请注意,您包含的 header 用于 Posix glob
函数。
这很可能是问题的另一个原因。
为了完整起见,让我列出我在您的代码示例中发现的其余问题:
- 缺少包含(opencv、字符串)
- 缺少名称空间限定符(
std::
或 cv::
-- avoid using namespace
at top level)
- 没有
main()
- 函数外的代码
- 相关位之后是一堆其他不相关的代码,我不会进一步分析(但大约 80% 的代码示例是不相关的)。
当我尝试编译这段代码时出现此错误-
error: no matching function for call to 'glob(std::__cxx11::string&, std::vector >&, bool)' glob (folder, filename, false); ^
这是我使用的代码:
#include <vector>
#include <glob.h>
string folder = "/home/ragesh/C++ /calibration_laptop/images/*.jpg";
vector <string> filename;
glob(folder, filename, false);
if(count < filename.size())
{
img = imread(filename[count]);
if(img.empty())
{
cout << "\nimage loading failed.....!"<<"\n";
return 0;
}
imshow("image", img);
cvtColor ( img,gray, COLOR_BGR2GRAY );// gray scale the source image
vector<Point2f> corners; //this will be filled by the detected corners
bool patternfound = findChessboardCorners(gray, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK);
count ++;
}
尝试使用 CV::String
而不是 std::string
。
添加#include "cvstd.hpp"
,然后
cv::String new_folder_string(folder).
std::vector<cv::String> new_filename_vector.
glob(new_folder_string, new_filename_vector, false);
一些信息here(glob function), here(String class in OpenCV), here(example of use of glob with cv::String) and here(berak 的建议)。
编辑:我忘了提到您还需要将 filename
从 std::string
切换到 cv::String
。
函数 cv::glob
具有以下签名:
void cv::glob(cv::String pattern, std::vector<cv::String>& result, bool recursive = false)
由于pattern
是按值传递的,而一个cv::String
可以是constructed from a std::string
,所以第一个参数不是问题。一个临时的 cv::String
是自动构造的。
然而,第二个 -- result
-- 是,因为它被用作 non-const 参考。为了解决您的问题,您需要将 filename
设为 std::vector<cv::String>
。因为它代表了collection个文件名,我也建议用复数来命名:filenames
.
示例代码:
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
int main()
{
std::string folder("/home/ragesh/C++ /calibration_laptop/images/*.jpg");
std::vector<cv::String> filenames;
cv::glob(folder, filenames, false);
// Do something with filenames...
return 0;
}
更新:
我从签名和您问题上的标签得出结论,您正在寻找 OpenCV glob
(不完整的代码示例使这有些困难)。但是,请注意,您包含的 header 用于 Posix glob
函数。
这很可能是问题的另一个原因。
为了完整起见,让我列出我在您的代码示例中发现的其余问题:
- 缺少包含(opencv、字符串)
- 缺少名称空间限定符(
std::
或cv::
-- avoidusing namespace
at top level) - 没有
main()
- 函数外的代码
- 相关位之后是一堆其他不相关的代码,我不会进一步分析(但大约 80% 的代码示例是不相关的)。