【问题标题】:Incorrect image display in QGraphicsScene, PyQtQGraphicsScene,PyQt中的图像显示不正确
【发布时间】:2015-02-22 09:11:59
【问题描述】:

当我想在PyQt 中的QGraphicsView 中显示带有QGraphicsScene 的图像时,我遇到了一个奇怪的问题。

对于某些图像文件效果很好,

但对其他人来说似乎是错误的,

我认为这可能与图像的格式有关?如果是这样,我该怎么办?

相关部分代码如下,感谢您的帮助:D

image = cv2.imread(str(file_path))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
height, width = image.shape[:2]

self.scene.clear()
self.frame = QtGui.QImage(image.data, width, height, QtGui.QImage.Format_RGB888)
self.scene.addPixmap(QtGui.QPixmap.fromImage(self.frame))
self.scene.update()
self.graphicsView.setScene(self.scene)

【问题讨论】:

    标签: python qt opencv pyqt qgraphicsscene


    【解决方案1】:

    我遇到了同样的问题,但它是C++ 项目,所以我认为我的解决方案也适用于你。考虑下一个代码:

       QString sss = "path";
       cv::Mat img = cv::imread(sss.toStdString());
    
       cv::cvtColor(img,img,CV_BGR2RGB);//in your code cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
       QImage image1((uchar*)img.data,img.cols,img.rows,QImage::Format_RGB888);
       //in your code QtGui.QImage(image.data, width, height, QtGui.QImage.Format_RGB888)
       //same problem
       QPixmap px1(QPixmap::fromImage(image1));
       ui->label_2->setPixmap(px1);
    

    解决方案:

    //...
    QImage image1((uchar*)img.data,img.cols,img.rows,img.step,QImage::Format_RGB888);
    //...
    

    如您所见,我在Qt 文档中又添加了一个参数:img.step ( int bytesPerLine )。因此,请尝试在您的 python 应用程序中执行相同操作。

    【讨论】:

    • 感谢您的编辑和亲切的回答 :) 经过跟踪和搜索,我认为我已经提供了一个适用于 Format_RGB888 图像的 python 版本解决方案。参数widthStep(每行字节数)设置为3*widthOfImage:widthStep = width * 3self.frame = QtGui.QImage(image.data, width, height, widthStep, QtGui.QImage.Format_RGB888)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多