在将此问题作为重复问题排除之前,请注意:
- 使用 OpenCV 第 3 版(不是第 2 版)
- 使用 Python 3(非 C++)
- 使用 PyQt 5(不是 PyQt4)
目标:尝试在 PyQt5 GUI 窗口中流式传输 OpenCV 网络摄像头视频帧
# For testing, I'm simply reading in a single image instead of a video frame
frame = cv2.imread('Koala.jpg',0) # this is a numpy.ndarray object
height, width = frame.shape
my_image = QImage(frame.tostring(), width, height, QImage.Format_RGB888)
# By here, things seem okay. print(mimage) basically says <QImage object at 0x32243>
# my_image.rgbSwapped() # This line crashes program
pixmap = QPixmap.fromImage(mimage) # This line crashes program as well
我不确定如何调试它了。没有错误回溯打印到控制台。任何帮助表示赞赏。提前致谢。
最佳答案
你需要先将openCv图像转换为QImage,他们使用pixmap显示图像如下,这是因为pixmap支持QImage格式。
height, width, bytesPerComponent = Img.shape
bytesPerLine = 3 * width
cv2.cvtColor(Img, cv2.COLOR_BGR2RGB, Img)
QImg = QImage(Img.data, width, height, bytesPerLine,QImage.Format_RGB888)
现在使用这个 QImg 来进行像素映射
pixmap = QPixmap.fromImage(QImg)
关于python - 在 Python 中将 OpenCV 3 (iplImage) 转换为 PyQt5 QImage/QPixmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33741920/