⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

[Solved] Qt 4.8 and OpenCV 2.8 new qwidget window not displaying correct colors

  • Well this certainly looks like another topic that is here.. :P

    Hi everyone,

    I'm trying to integrate Qt and OpenCV and until now I've had success with this task, however I'm opening a new QWidget window from my main QWidget and for some reason the colors displayed are not the correct ones.

    In a nutshell what I have is a video player in the main qwidget (everything works well, updates, colors and so on), however when I press a button a new QWidget based window is supposed to appear with a frame from the video... and it does... but the colors are not correct in this new window... I'm using exactly the same code to show in the main window and in this new window... but the colors are different!! because reasons...

    The code I'm using to show the display the images (in a QPaintEvent) is the following:
    QPainter painter(this);
    cv::cvtColor(mainImg, mainImg, CV_BGR2RGB);
    QImage qimgOriginal((uchar*) mainImg.data, mainImg.cols, mainImg.rows, mainImg.step, QImage::Format_RGB888);
    QPixmap pixmap = QPixmap::fromImage(qimgOriginal).scaled(width, height, Qt::KeepAspectRatio);
    painter.drawPixmap(QPoint(0,0), pixmap);

    Again this works in a QWidget and in the other it doesn't...
    Maybe this is even only related with Qt? I don't know

    If anyone has any hint it would be greatly appreciated!!
    Thanks in advance!

    P.S. This is the result

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Where does mainImg come from ? Is it shared between widgets ?

  • The mainImg comes from opencv and is then passed to the different widgets via either cv::Mat or via a QPixmap depending on the case...

    I've gave up using pointers as the memory is not freed. For the QWidget that does not appear in the correct colors I pass a QPixmap, store it locally and then draw it on the paintEvent.

  • Lifetime Qt Champion

    Since you are doing an in-place conversion, you might be "corrupting" the data at some point.

  • AH I forgot to say one interesting fact, in the beginning, where I had only QWidget on the new window, when I selected some other program, the colors would change to correct ones... and when I selected back this new widget window the colors would go wrong again. After this test I created a subWidget that deals only with showing the images that is inside the widget with the layout, this stopped happening and it basically never goes to correct colors...

    Hmmm but the image is the same... would it be possible to corrupt it in such a way? : S

  • Lifetime Qt Champion

    By having e.g. the color conversion called twice on the same data (especially since you share it)

  • :O... I duplicated the cv::Mat and it worked! Thank you very much!

  • Lifetime Qt Champion

    You're welcome !

    Since you have your images working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply