title: Speeding up writing images into hard disk in OpenCV
body: <p>I am working with a 50 fps camera (in Ubuntu environment and Qt framework) and every 20 ms I get a frame to process.</p>
  
  <p>I wrote a code to read images from camera and then store them in hard drive.</p>
  
  <pre><code>while(3.14)
  {
   cv::Mat Camera_Image = Capture_Image();
   double T1 = (double)cv::getTickCount();
   cv::imwrite (STORE_ADDRESS,Camera_Image);
   T1 = (((double)cv::getTickCount() -T1)*1000)/cv::getTickFrequency();
   print(T1);
  }
  </code></pre>
  
  <p>when I see the output the time to write a single image into hard disk is around 30 ms for a 2048*1080 image size. each image is single channel (gray scale) but I'm writing them in .jpg format in hard disk. size of each image in hard disk is approximately 500Kbytes.</p>
  
  <p>Since I'm capturing a frame in around 20 ms, I'm not able to write them all into hard disk in real time. I've written my code using Qthread and created a queue to see if there's any improvement but the results were the same and it was only a memory overhead. </p>
  
  <p>Is it possible to change this situation, or use some other library to write these images into hard disk much faster? I would also prefer a Qt solution if available ...</p>
  
  <p>Plus I need to write every single frame into hard disk so please do not propose to use Motion compression algorithms since they don't apply to my situation ....</p>
  
  <p>Code:
  Mainwindow.cpp</p>
  
  <pre><code> Qlist&lt;cv::Mat&gt; FINAL_IM_VEC;
  MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
  {
    ui-&gt;setupUi(this);
  
    IMREAD *IMR = new IMREAD(this);   // an instance of IMREAD Class which reads camera frames
    IMWRITE *IMW = new IMWRITE(this);  // an instance of IMWRITE Class which Writes camera frames into hard disk
    QThread *IMAGE_READ_Thread = new QThread(this);
    QThread *Image_Store_Thread = new QThread(this);
    connect(IMAGE_READ_Thread,SIGNAL(started()),IMR,SLOT(IMREAD_Process()));
    connect(Image_Store_Thread,SIGNAL(started()),IMW,SLOT(IMWrite_Process()));
    IMR.moveToThread(IMAGE_READ_Thread);
    IMW.moveToThread(Image_Store_Thread);
    IMAGE_READ_Thread-&gt;start();
    Image_Store_Thread-&gt;start();
  }
  </code></pre>
  
  <p>imread.hpp</p>
  
  <pre><code>class IMREAD : public QObject
  {
      Q_OBJECT
  public:
      explicit IMREAD(QObject *parent = 0);
  
  signals:
  
  public slots:
      void IMREAD_Process();
  private:
      bool Stop;
  };
  </code></pre>
  
  <p>imread.cpp</p>
  
  <pre><code>IMREAD::IMREAD(QObject *parent) :
      QObject(parent)
  {
    this-&gt;Stop = false;
  }
  
  void IMREAD::IMREAD_Process()
  {
  
    while(!Stop)
      {
            cv::Mat Image = CAM::Campture_F(25);//wait a maximum of 25 milisecond to grab a new frame
            if(Image.data())
              {
            FINAL_IM_VEC.push_back(Image);
              }
        }
      }
  
  }
  </code></pre>
  
  <p>imwrite.hpp</p>
  
  <pre><code>#ifndef IMWRITE_H
  #define IMWRITE_H
  #pragma once
  #include &lt;QObject&gt;
  class IMWRITE : public QObject
  {
      Q_OBJECT
  public:
      explicit IMWRITE(QObject *parent = 0);
  signals:
  
  public slots:
      void IMWrite_Process();
  private:
      bool Stop;
  };
  </code></pre>
  
  <p>imwrite.cpp</p>
  
  <pre><code>IMWRITE::IMWRITE(QObject *parent) :
      QObject(parent)
  {
    this-&gt;Stop =false;
  }
  void IMWRITE::IMWrite_Process()
  {
      static int counter = 0;
      while(!Stop)
        {
          for(int i = 0 ; i &lt; FINAL_IM_VEC.size() ; i++)
              {
                  QString address = "/home/Provisioner/ThreadT/Results/" + QString::number(counter++) + ".jpg";
                  cv::imwrite(address.toUtf8().constData(),FINAL_IM_VEC[i]);
                  FINAL_IM_VEC.erase(FINAL_IM_VEC.begin() + i);
                  i--;
              }
        }
  
  }
  </code></pre>
  
  <p>Since this is just part of the whole project, I've removed some of its irrelevant parts ...but it shows how I wrote my multithreaded code in a big picture... so if there's any thing wrong with please inform me.</p>
  
  <p>Thanks in advance.</p>