cv :: Mat zu QImage und zurück

//Entschuldigung für mein Englisch.

Sag mir bitte, was mache ich falsch? Ich habe viel darüber gelesen. Und schreiben Sie Code, aber ich habe ein schreckliches Ergebnis.

Wie ich in OpenCV versteheCV_8UC3 ist das gleiche wieQImage :: Format_RGB888 , außer BRG und RGB entsprechend.

cv :: Mat in diesem Format kann ich lesen:

cv::Mat mat1 = cv::imread("bugero.jpg",3); 

Um cv :: Mat in QImage zu konvertieren, kann ich Folgendes tun:

QImage Mat2QImage(cv::Mat const& 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;
}

Ich habe eine temporäre Kopie erstellt, da ich eine Kopie der Daten in QImage haben möchte.

Dann. Um es zurück zu konvertieren, muss ich Folgendes tun:

cv::Mat QImage2Mat(QImage const& 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;
}

Ich habe eingefügtcvtColor (res, res, CV_BGR2RGB); cv Mat mit BGR Farben machen. Ich weiß nicht genau, was in dieser Funktion drin istcvtColor (res, res, CV_BGR2RGB)Aber ich entschied, dass wenncvtColor (res, res, CV_BGR2RGB); ändere die Plätze R und B, das wird die Plätze dieser Farben zurücksetzen, weil ich sie nicht gefunden habeCV_BGR2RGB.

Also habe ich ein kurzes Beispielprogramm geschrieben

#include <QApplication>
#include <QtGui>
#include <cv.h>
#include "opencv2/highgui/highgui.hpp"

QImage Mat2QImage(cv::Mat const& 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& 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(&W1);
     QLabel imlab2(&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();
}

und .pro Datei

INCLUDEPATH += /usr/local/include/opencv /usr/local/include/opencv2
LIBS += -lopencv_core -lopencv_imgproc\
                                       -lopencv_highgui
QT       += gui
QT       += core
SOURCES += \
    QcvMat.cpp \

Und ich habe ein schlechtes Ergebnis !!!

Gibt es einige? Leute, ich brauche Hilfe!

Ich habe einige Debug-Informationen hinzugefügt, um cv :: Mat.step und QImage.bytesPerLine () zu erhalten, und es ist anders.

alex@lenovo /media/Files/Programming/Cpp/tests/QImagecvMat $ ./QcvMat 
cv step  942 
QImage  bytesPerLine  944 
cv step  942 
QImage  bytesPerLine  944 

Was bedeutet es und kann daran ein Problem sein?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage