【笔记】Opencv 遍历文件夹:substr && std::string::find_last_of

注:

substr

注:

 

 

 

正文:

1 获取完整路径:

#include <opencv2\opencv.hpp>
#include <string>
 
int main(int argc, char* argv[]) {
 
    std::string folder_path = "D:\\database\\test\\*.*"; //path of folder, you can replace "*.*" by "*.jpg" or "*.png"
    std::vector<cv::String> file_names;  
    cv::glob(folder_path, file_names);   //get file names
 
    cv::Mat img;   //try show it when file is a picture
 
    for (int i = 0; i < file_names.size(); i++) {
        std::cout << file_names[i] << std::endl;
        img = cv::imread(file_names[i]);
        if (!img.data) {
            continue;        
        }
        //do some work with the img you readed
        cv::imshow("img", img);
        cv::waitKey(300);
    }
    std::getchar();  //check your output on terminal
}

2 获取文件名称

#include <opencv2\opencv.hpp>
#include <string>
 
int main(int argc, char* argv[]) {
 
    std::string folder_path = "D:\\database\\test\\*.*"; //path of folder, you can replace "*.*" by "*.jpg" or "*.png"
    std::vector<cv::String> file_names;  
    cv::glob(folder_path, file_names);   //get file names
 
    cv::Mat img;   //try show it when file is a picture
 
    for (int i = 0; i < file_names.size(); i++) {
 
        int pos = file_names[i].find_last_of("\\");
        std::string img_name(file_names[i].substr(pos + 1));
        std::cout << img_name << std::endl;
 
        img = cv::imread(file_names[i]);
        if (!img.data) {
            continue;        
        }
        //do some work with the img you readed
        cv::imshow("img", img);
        cv::waitKey(300);
    }
    std::getchar();  //check your output on terminal
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在编程中,当我们使用OpenCV库的imread函数时,遇到错误信息"undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'"时,通常是因为编译器无法找到对应的函数定义。 这个问题通常是由于链接错误引起的。链接错误意味着编译器找不到对应的函数定义,可能是由于库文件没有正确链接或者链接顺序不正确。 解决这个问题的一种方法是确保在编译时正确链接OpenCV库。首先,检查OpenCV库的路径是否正确配置,并在编译选项中添加正确的库文件路径。例如,在使用g++编译器时,可以使用"-L"选项指定库文件的路径。其次,检查是否将正确的OpenCV库加入到链接器命令中。对于g++编译器,可以使用"-lopencv_core -lopencv_highgui -lopencv_imgproc"等选项来链接对应的库文件。 另外,还有可能是在编程过程中忽略了头文件的引用。使用imread函数之前,需要在代码的开头引用OpenCV的头文件,例如"#include <opencv2/opencv.hpp>"。 此外,还有一种可能是编译器使用了不匹配的C++标准库版本。错误信息中出现的"std::__cxx11::"可能表明编译器正在使用C++11的标准库。可能的解决方法是确保编译器和库文件使用相同的C++标准库版本。 总之,当出现"undefined reference"错误时,需要检查是否正确链接库文件、正确引用头文件以及编译器和库文件的C++标准库版本是否一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值