如何导航文件夹以读取图像?

时间:2014-11-13 00:31:31

标签: c++ opencv

我试图在我的照片中对一些感兴趣的区域进行抽样。因此,基本上每30个像素生物的投资回报率为30x30,然后尝试获取大量样本图片。我试图搜索相同的问题,我使用VideoCapture得到答案,但他们没有工作。我只是想知道如何导航目录并选择每个图像应用我的方法来采样图像。但是我的新手C ++技能已达到极限。我的代码如下:

这是我想要应用于我本身位于目录中的每个图像的方法" C:\ Projects \ DataSet"

  ********fileName definition (part of ROI class)***********************



  string ROI::fileName(size_t frameNumber)
   {
   const string filePrefix("C:\\Projects\\dataset-checkerboard\\Checkerboard\\");
   const string fileSuffix(".png");
   const size_t fileNumDigits(10);
   stringstream ss;
   ss << filePrefix;
   ss << setw(fileNumDigits) << setfill('0') << frameNumber;
   ss << fileSuffix;
   return ss.str();

   ************sampleROI definition***************

Mat ROI::sampleROI(cv::Mat img,  int width, int length){
cv::Mat sampled;
int limitx = img.cols - length;
int limity = img.rows - width;



for(int x = 0; x < limitx ; x += length){
    for(int y = 0; y < limity  ; y += width){


    sampled = img(Rect(cv::Point(x,y),cv::Size(width,length)));
         stringstream file;
         file << "C:\\Projects\\dataset-checkerboard\\Samples\\img" << i << " atPixel " << Point(x,y) << ".png";
         cv::imwrite(file.str(), sampled);

    }


    }



    return sampled;
   }


   *****************************Main*******************************
   ROI sampling ;

         int main( int argc, char** argv )
            {
                int m = 0;
                size_t t = 166;
                for(size_t i = 4; i < t; ++i) { //0000000004.png is my first image
                Mat i_t = imread(sampling.fileName(i), CV_LOAD_IMAGE_GRAYSCALE);
                sampling.sampleROI(i_t,30,30, i); //samples 30x30 image
                cout << "finished processing image: " << sampling.fileName(i) << endl;
       }
    return 0;
    }

对于单张图片,它的工作效果非常好,上面的代码只是为了让您了解我想要对图像中的图片做些什么。

我想有一种方法可以帮助我浏览每个图像,它们都按照0000000 ###。png的顺序命名,图像计数为166.使用VideoCapture不起作用。

编辑:主方法中的for循环似乎没有重复。

由于

2 个答案:

答案 0 :(得分:0)

所以我不完全理解这个问题,但这是我对它的抨击。 基本的想法只是forlooping通过所有图像,并用它们做你想要的。

#include <iostream>
#include <sstream>


std::string path = "path/to/image/folder/";
std::stringstream ss;
std::string num = "";
for(int i = 0; i < 166; i++){
    ss << i;
    num = ss.str();

    if(num.length() == 1) num = "00" + num;
    if(num.length() == 2) num = '0' + num;

    cv::Mat img = cv::imread((path+num+".png").c_str(), 1);
    cv::Mat result = sampleROI(img,  1024, 1024);
    // do something

    ss.str("");
    ss.clear();
}

答案 1 :(得分:0)

我刚刚解决了类似问题的类似问题:

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

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using std::string;
using std::stringstream;
using std::setfill;
using std::setw;

using cv::imread;
using cv::Mat;

string fileName(size_t frameNumber)
{
  const string filePrefix("office/input/in");
  const string fileSuffix(".jpg");
  const size_t fileNumDigits(6);
  stringstream ss;
  ss << filePrefix;
  ss << setw(fileNumDigits) << setfill('0') << frameNumber;
  ss << fileSuffix;
  return ss.str();
}
...
int main(int argc, const char* argv[])
{
  // initial work, set t to last image number + 1
  // i could start at 0 if your first image is 0
  for(size_t i = 1; i < t; ++i) {
    Mat i_t = imread(fileName(i), CV_LOAD_IMAGE_GRAYSCALE);
    // Do work
  }
  return 0;
}
相关问题