Frage

I'm working with opencv and I have to integrate it to a Qt Gui, but I have some issue for showing the image in Qt ...

Here is the code that I'm using

#include <QApplication>
#include <QtGui>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

QImage const Mat2QImage(const cv::Mat& src){
  return QImage((unsigned char*)src.data, src.cols, src.rows, src.step, QImage::Format_RGB888);
}

int main(int argc, char **argv){

  QApplication app(argc, argv);
  cv::Mat src = cv::imread("lena.jpg");

  QLabel aLabel;
  QImage img = Mat2QImage(src);
  aLabel.resize(src.rows, src.cols);
  aLabel.setPixmap(QPixmap::fromImage(img));
  aLabel.show();

  return app.exec();

} 

And Here is the result : enter image description here

Note that if I change the format to QImage::FormatRGB32 I will get an empty window, I've also tried all the formats and that wasn't what I'm expacting ... Any idea about how to solve the issue ?

Thanks !

War es hilfreich?

Lösung

OpenCV saves the image in bgr format. This means the color values of the pixels are swapped. If you add this line to your program the image gets displayed correctly:

  img = img.rgbSwapped();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top