如何阅读大量图像(超过一百万)并有效地处理它们

时间:2016-08-15 00:47:40

标签: c++ image opencv boost large-data

我有一个程序可以对图像执行一些计算以生成其他图像。该系统使用少量图像作为输入,我想知道如何使其适用于大量的输入数据;像一百万或更多的图像。我主要关注的是如何存储输入图像以及如何存储输出(生成)图像。

我有一个函数compute(const std:vector<cv::Mat>&),可以对124个图像进行计算(由于GPU内存限制)。因为算法是迭代的,所以每个图像需要不同的迭代量来产生输出图像。

现在,如果我提供超过124张图像,那么该函数会计算前124张图像,当图像完成计算时,它会与另一张图像交换。我希望该算法能够用于更大的输入,例如一百万或更多的图像。计算函数为每个输入图像返回一个输出图像,其实现方式如下:

std::vector<cv::Mat> compute(std::vector<cv::Mat>& image_vec) {

  std::vector<cv::Mat> output_images(image_vec.size());

  std::vector<cv::Mat> tmp_images(124);

  processed_images = 0;
  while (processed_images < image_vec.size()) {

    // make computations and update the output_images 
    // for the images that are currently in processed

    // remove the images that finished from the tmp_images
    // and update the processed_images variable

    // import new images to tmp_images unless there 
    // are no more in the input vector
  }

  return output_images;
}

我正在使用boost::filesystem从程序开头读取文件夹中的图像(我也使用OpenCV来读取和存储每个图像):

std::vector<cv::Mat> read_images_from_dir(std::string dir_name) {

  std::vector<cv::Mat> image_vec;

  boost::filesystem::path p(dir_name);

  std::vector<boost::filesystem::path> tmp_vec;

  std::copy(boost::filesystem::directory_iterator(p),
            boost::filesystem::directory_iterator(),
            back_inserter(tmp_vec));

  std::vector<boost::filesystem::path>::const_iterator it = tmp_vec.begin();

  for (; it != tmp_vec.end(); ++it) {

    if (is_regular_file(*it)) {
      //std::cout << it->string() << std::endl;
      image_vec.push_back(read_image(it->string()));
    }
  }

  return image_vec;
}

然后主程序看起来像这样:

void main(int argc, char* argv[]) {

  // suppose for this example that argv[1] contains a correct 
  // path to a folder which contains images
  std::vector<cv::Mat> input_images = read_images_from_dir(argv[1]);

  std::vector<cv::Mat> output_images = compute(input_images);

  // save the output_images
}

Here如果您愿意,可以在在线编辑器中找到该程序。 任何澄清这个问题的消息都是受欢迎的。

编辑:一些答案和评论指出了我必须做出的有用的设计决策,以便您能够回答这个问题。我想提一下:

  • 在程序启动之前,图像已存储在磁盘中。
  • 该过程将在没有新数据提交的情况下“离线”完成,并且每隔几小时(或几天)就会完成一次。这是因为程序的参数在完成后会发生变化。
  • 我可以容忍最初没有尽可能快的实现,因为我想让事情有效,然后考虑进行优化。
  • 代码有很多计算,所以到目前为止I / O并没有那么多时间。
  • 我希望这个代码可以在一台机器上运行,但我认为最好不要使用多线程作为第一个版本,以便代码更具可移植性,以便我可以将它集成到另一个程序中不使用mutlithreading,我不想有更多的依赖。

我想到的一个实现是读取批量数据(比如5K图像)并在计算它们的输出后加载新数据。但是,如果没有太多额外的复杂性,我不知道是否有更好的东西。当然,欢迎任何答案。

0 个答案:

没有答案