Qt QImage to QPixmap Conversion loses Color Information for UI

好久不见. 提交于 2019-12-24 05:11:56

问题


I am trying to update a QPixmap on a QLabel in my main Qt UI. The following slot is called to do this with the "newImage" variable QImage ( because it's from a different thread ). The QImage is converted to someImage with convertFromImage ( I've also tried ::fromImage ). If I just save the QImage "newImage" to file, I get a green rectangle and red text that I draw with OpenCV earlier on, however, if I save the converted pixmap OR show the converted pixmap I lose the color for the rectangle and text in the image but keep the color for the frame itself. I've posted 2 images below to demonstrate the difference between the QImage I pass this slot and the pixmap that is displayed on the UI in the pixmap. I don't know how to have the pixmap display the rectangle and text with color! What am I doing wrong? Thanks!

void MainWindow::updateImage(QImage newImage, double timeElapsed) {

    QImage someImage = newImage.convertToFormat(QImage::Format_RGB888);

    // Get pixmap from data
    m_NewPixMap.convertFromImage(someImage,Qt::ColorOnly); // Tried various ones of these

    // Debug status
//    qDebug() << "Pixmap received by MainWindow and elapsed time is: " << timeElapsed << " ( Image size is: " << newImage.byteCount() << " )";
//    qDebug() << "Pixmap is null? " << m_NewPixMap.isNull();

    // Update the label
    float hz = 1000.0f / timeElapsed;
    QString status;
    status.sprintf("FrameRate (Hz) = %.02f (%.0f ms)", hz, timeElapsed);

    // Update status label
    m_StatusLabel->setText(status);

    // Update the main view
    m_Label->setPixmap(m_NewPixMap);
    repaint();

    //qDebug() << "Saving QImage now...";
    QFile file(QString("output_detected_images/detected_image_%1.png").arg(m_Counter));
    file.open(QIODevice::WriteOnly);
    bool savedSuccessfully = newImage.save(&file,"PNG"); // This gives proper color in image
    //bool savedSuccessfully =  m_NewPixMap.save(&file, "PNG"); // THIS GIVES A BLACK IMAGE
    qDebug() << "Done saving QPixmap... " << savedSuccessfully;

}

回答1:


From the question, I understand that the rectangles and text are part of the source image ?

I am guessing that the problem may be related to different formats for the color... which ConvertToFormat is not able to solve.

Perhaps try to make sure that in the source image the rectangles and text are saved with the same RGB32 format as the rest of the image ?



来源:https://stackoverflow.com/questions/25478186/qt-qimage-to-qpixmap-conversion-loses-color-information-for-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!