title: Loading images takes very long in C++ using OpenCV on Windows 8.1
body: <p>I am currently working on a data-driven learning application in C++. I have a huge amount of data, over 300.000 images needing around 3 GB in total.</p>
  
  <p>Regarding my <strong>working environment</strong>:</p>
  
  <ul>
  <li>Windows 8.1, 64bit</li>
  <li>Visual Studio 2013</li>
  <li>OpenCV</li>
  <li>OpenMP</li>
  </ul>
  
  <p>My <strong>hardware</strong>:</p>
  
  <ul>
  <li>i7-3770</li>
  <li>8GB RAM</li>
  <li>SSD (System and Visual Studio)</li>
  <li>HDD</li>
  </ul>
  
  <p>My <strong>problem in short</strong> is, that loading only the images, so 3 GB, takes over 3 hours, which I would like to improve.</p>
  
  <p>The <strong>implementation</strong> is as follows: At first, I am loading some information regarding the images (not the images themselves) from a file. Internally I am using a standard vector, which holds 300.000 pointers to my class Item. Item holds the information loaded from the file and the image (OpenCV Mat), which is NOT loaded yet. Some independent intermediate steps follow. After that, I am iterating through my vector - parallelized using OpenMP - and load the image for each Item using:</p>
  
  <pre><code>imread(PATH_TO_FILE, CV_LOAD_IMAGE_UNCHANGED);
  </code></pre>
  
  <p><strong>What is really strange</strong> to me is, that increasing the amount of images is not a linear increase for the loading time of the images. Using 22.000 images takes around 22 seconds, using 44.000 images takes 1 minute and 43 seconds, 66.000 images takes around 4 minutes and so on.</p>
  
  <p>I am unsure, if this problem is due to a hardware bottleneck (which I assumed at first) or due to an implementation flaw on my side. I have <strong>already considered</strong> a lot, like:</p>
  
  <ul>
  <li>halving the image bit depth and thus halving the memory size does not decrease the time taken</li>
  <li>my Virtual Memory for my application is maxed at around 4GB, so there should not be too much swapping involved</li>
  <li>there is no difference in loading the data from my system SSD to loading the data from my HDD</li>
  <li>giving the process a higher priority (I chose the highest, which is realtime) improved the runtime a little, however, the runtimes given above do use this improvement already</li>
  <li>even though I am using OpenMP, RessourceManager states that during loading of my images, my application only uses 15% of my CPU. Using prints, I can tell that there are all 8 workers sharing the loading though.</li>
  <li>shrinked the vector size using vector.shrink_to_fit()</li>
  </ul>
  
  <p>These facts seem to me as there is not a hardware problem, but an implementation flaw going on. Is it inefficient to use a huge vector holding over 300.000 pointers? Or anything regarding OpenCV Mat I have not considered? Any hints on how I can pinpoint the problem further?
  I appreciate any suggestions on what is causing this behaviour and how I probably could get around it.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT</strong>: How I load the images into the vector. Note that i renamed some things so there might be typos.</p>
  
  <pre><code>void LoadAllImages()
  {
      for (int i = 0; i &lt; data-&gt;size(); i++)
      {
          Item* cur_item = data-&gt;at(i);
          cur_item-&gt;setImage(cur_item-&gt;loadImage());
      }
  }
  
  
  Mat Item::loadImage()
  {
      return imread(IMAGES_PATH + image_name_, CV_LOAD_IMAGE_UNCHANGED);
  }
  
  
  void Item::setImage(Mat img)
  {
      img_ = img;
  }
  </code></pre>
  
  <p><strong>EDIT2</strong>: How I setup the vector without images. Note that I used boosts multithreading for this part. Also note, that this parts execution time increases linear with the data.</p>
  
  <pre><code>void foo(vector&lt;Item*&gt;* data, const string file_path, const string file_name)
  {
      //open file
  
      string image_name;
      boost::mutex data_mutex;
      boost::thread_group thread_group;
      while (file &gt;&gt; image_name)
      {
          //reading other data regarding the current image
  
          thread_group.add_thread(new boost::thread(addDataToVectorThread, data, image_name, other_data_read, &amp;data_mutex));
      }
      thread_group.join_all();
  }
  
  
  void FileHandler::addDataToVectorThread(vector&lt;Item*&gt;* data, string image_name, vector&lt;float&gt; other_data, boost::mutex* data_mutex)
  {
      Item* item = new Item(other_data, image_name);
      data_mutex-&gt;lock();
      data-&gt;push_back(item);
      data_mutex-&gt;unlock();
  }
  </code></pre>
  
  <p><strong>EDIT3</strong>: I tried the code provided by SSteve and was able to narrow down my problem. This code generates random images of the same size as mine are, so 96x96 with a color depth of 8bit. Note that I changed his code to only generate grayscale images as my images are. Loading 300.000 images on my laptop took like 10 minutes, which is fine.</p>
  
  <p>I simplified my code as much as possible and removed ALL multi-threading. I have changed my code as such, that the images are directly loaded to the Items, so during vector creation.</p>
  
  <p>Watching the Resource Monitor I noticed that my images are taking a LOT of memory. Loading 10.000 images already takes 1 GB. Using CV_LOAD_IMAGE_GRAYSCALE instead of CV_LOAD_IMAGE_UNCHANGED halves the memory consumption. I don’t get it, my images are definitely 96x96x8 bit and it is still way too much.</p>
  
  <p>Using my full code but loading the random images created by the code of SSteve using only one color channel takes 100 MB of memory for 10.000 images and some additional stuff. The images alone should take ~90 MB, so it should be fine. This is only a little fraction compared to my images.</p>
  
  <p><strong>In short</strong>: My images seem to cause the problem, but I don’t get why.</p>
  
  <p><strong>How I get to these images</strong>: The images I use for the problematic part of my algorithm are preprocessed by me. This preprocessing step is independent and basically scaling down the images. So the images I got to work do have a size of 240x320 and a 16 bit depth. Then I scaled those images to 96x96 and an 8 bit depth.</p>
  
  <p>Is there any possibility that for some reason the images I scaled down are stored in the correct size and this size is correctly displayed in the image properties by Windows, but the images still include some information which should be “removed”? So that they take up more memory than they should? It does not make any sense to me.</p>
  
  <p>Thanks so far for all the help.</p>