title: cv::Mat to QImage and back
body: <p><em>//Sorry for my english.</em></p>
  
  <p>Tell me please, what I am doing wrong?
  I have read a lot about this. And write some code, but I have a terrible result.</p>
  
  <p>As I understand
  in Opencv <strong>CV_8UC3</strong> is the same as <strong>QImage::Format_RGB888</strong> , except BRG and RGB accordingly.</p>
  
  <p>to read cv::Mat in this format I can do:</p>
  
  <pre><code>cv::Mat mat1 = cv::imread("bugero.jpg",3); 
  </code></pre>
  
  <p>So, to convert cv::Mat to QImage I can do:</p>
  
  <pre><code>QImage Mat2QImage(cv::Mat const&amp; src)
  {
       cv::Mat temp(src.cols,src.rows,src.type());
       cvtColor(src, temp,CV_BGR2RGB);
       QImage dest= QImage((uchar*) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
       return dest;
  }
  </code></pre>
  
  <p>I made temp mat becouse I want to have copy of data in QImage. </p>
  
  <p>Then.
  To convert it back I Have to do:</p>
  
  <pre><code>cv::Mat QImage2Mat(QImage const&amp; src)
  {
       QImage temp = src.copy();
       cv::Mat res(temp.height(),temp.width(),CV_8UC3,(uchar*)temp.bits(),temp.bytesPerLine());
       cvtColor(res, res,CV_BGR2RGB); 
       return res;
  }
  </code></pre>
  
  <p>I have inserted <strong>cvtColor(res, res,CV_BGR2RGB)</strong>; to make cv Mat with BGR colors. I do not exactly know what in inside this function <strong>cvtColor(res, res,CV_BGR2RGB)</strong>;, But I decided 
  that if <strong>cvtColor(res, res,CV_BGR2RGB);</strong> change places R and B, that will chage places of this colors back, because I did not found <strong>CV_BGR2RGB</strong>.</p>
  
  <p>So, I wrote short sample program</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QtGui&gt;
  #include &lt;cv.h&gt;
  #include "opencv2/highgui/highgui.hpp"
  
  QImage Mat2QImage(cv::Mat const&amp; src)
  {
       cv::Mat temp(src.cols,src.rows,src.type()); // make the same cv::Mat
       cvtColor(src, temp,CV_BGR2RGB); // cvtColor Makes a copt, that what i need
       QImage dest= QImage((uchar*) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
       return dest;
  }
  
  cv::Mat QImage2Mat(QImage const&amp; src)
  {
       QImage temp = src.copy(); 
       cv::Mat res(temp.height(),temp.width(),CV_8UC3,(uchar*)temp.bits(),temp.bytesPerLine());
       cvtColor(res, res,CV_BGR2RGB); // make convert colort to BGR ! 
       return res; 
  }
  
  
  int main(int argc, char *argv[])
  {
       QApplication a(argc, argv);
       QWidget W1;
       QWidget W2;
       QLabel imlab1(&amp;W1);
       QLabel imlab2(&amp;W2);
       W1.setWindowTitle("Convert cv::Mat to QImage First time"); 
       W2.setWindowTitle("Convert cv::Mat to QImage Second time");    
  
  
  
  
       cv::Mat mat1 = cv::imread("bugero.jpg",3);
  
       QImage qim1  = Mat2QImage(mat1);
  
       cv::Mat mat2 = QImage2Mat(qim1);
  
       QImage qim2 = Mat2QImage(mat2); 
  
       cv::Mat mat3 = QImage2Mat(qim2);
  
  
  
       cv::imshow("First Mat",mat1);
       imlab1.setPixmap(QPixmap::fromImage(qim1)); 
       W1.setFixedSize(qim1.size()); 
       cv::imshow("Convert QImage to cv::Mat firstly",mat2);
       imlab2.setPixmap(QPixmap::fromImage(qim2));
       W2.setFixedSize(qim2.size()); 
       cv::imshow("Convert QImage to cv::Mat secondly",mat2);
       W1.show();
       W2.show();
  
       return a.exec();
  }
  </code></pre>
  
  <p>and .pro file</p>
  
  <pre><code>INCLUDEPATH += /usr/local/include/opencv /usr/local/include/opencv2
  LIBS += -lopencv_core -lopencv_imgproc\
                                         -lopencv_highgui
  QT       += gui
  QT       += core
  SOURCES += \
      QcvMat.cpp \
  </code></pre>
  
  <p>And I have got a BAD result!!!
  <img src="https://i.stack.imgur.com/9cT2g.jpg" alt="My bad result"></p>
  
  <p>Is there some? People,I need help!</p>
  
  <p>I added some debug info to get cv::Mat.step and QImage.bytesPerLine() and it is different.</p>
  
  <pre><code>alex@lenovo /media/Files/Programming/Cpp/tests/QImagecvMat $ ./QcvMat 
  cv step  942 
  QImage  bytesPerLine  944 
  cv step  942 
  QImage  bytesPerLine  944 
  </code></pre>
  
  <p>What does it means and may be problem in it?</p>