如何使用 opencv 向 PYQT 显示图像
How to show image to PYQT with opencv
你好,我是 python 和 opencv
的新手
我想问一下,如何在 qlabel (pyqt) 中显示我的图像,我想将 qlabel 转换为灰度。
import sys
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class UIProgram(QMainWindow):
def __init__(self):
super(UIProgram,self).__init__()
loadUi("Backpro2.ui",self)
#self.image=None
self.trainLoadImgBtn.clicked.connect(self.loadClicked)
self.image = QImage()
@pyqtSlot()
def loadClicked(self):
fname,filter=QFileDialog.getOpenFileName(self,'Open File','D:\',"Image Files(*.jpg)")
if fname:
self.loadImage(fname)
else:
print('invalid image')
def loadImage(self,fname):
self.image=cv2.imread(fname,cv2.IMREAD_COLOR)
self.displayImage()
def displayImage(self):
qformat =QImage.Format_Indexed8
if len(self.image.shape)==3:
if(self.image.shape[2])==4:
qformat=QImage.Format_RGBA8888
else:
qformat=QImage.Format_RGB888
img=QtGui.QImage(self.image.data,self.image.shape[1],self.image[0],QtGui.QImage.Format_RGB888)
img = img.rgbSwapped()
self.trainOpenImg.setPixmap(QPixmap.fromImage(img))
self.trainOpenImgn.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
if __name__ == "__main__":
app=QApplication(sys.argv)
window=UIProgram()
window.setWindowTitle('Test')
window.show()
sys.exit(app.exec_())
当我点击加载图像按钮时崩溃,图像无法在 qlabel 中显示
这个错误
Process finished with exit code -1073740791 (0xC0000409)
它有 2 个错误:
- 您必须将
bytesPerLine
传递给 QImage
- 当您设置对齐时,您在
trainOpenImg
中有 "n" 个。
def displayImage(self):
qformat =QImage.Format_Indexed8
if len(self.image.shape)==3:
if self.image.shape[2] ==4:
qformat=QImage.Format_RGBA8888
else:
qformat=QImage.Format_RGB888
img = QtGui.QImage(self.image.data,
self.image.shape[1],
self.image.shape[0],
self.image.strides[0], # <--- +++
qformat)
img = img.rgbSwapped()
self.trainOpenImg.setPixmap(QPixmap.fromImage(img))
self.trainOpenImg.setAlignment(QtCore.Qt.AlignCenter)
另一方面,IDE 在处理某些类型的错误时存在问题并且只能启动代码,因此在这些情况下,建议在 CMD 或终端中 运行 它,因为它们会为您提供更多信息,因为在这种情况下的示例错误消息是:
Traceback (most recent call last):
File "test.py", line 20, in loadClicked
self.loadImage(fname)
File "test.py", line 26, in loadImage
self.displayImage()
File "test.py", line 36, in displayImage
img=QtGui.QImage(self.image.data,self.image.shape[1],self.image[0],QtGui.QImage.Format_RGB888)
TypeError: arguments did not match any overloaded call:
QImage(): too many arguments
QImage(QSize, QImage.Format): argument 1 has unexpected type 'memoryview'
QImage(int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
QImage(bytes, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(bytes, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(List[str]): argument 1 has unexpected type 'memoryview'
QImage(str, format: str = None): argument 1 has unexpected type 'memoryview'
QImage(QImage): argument 1 has unexpected type 'memoryview'
QImage(Any): too many arguments
Aborted (core dumped)
你好,我是 python 和 opencv
的新手我想问一下,如何在 qlabel (pyqt) 中显示我的图像,我想将 qlabel 转换为灰度。
import sys
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class UIProgram(QMainWindow):
def __init__(self):
super(UIProgram,self).__init__()
loadUi("Backpro2.ui",self)
#self.image=None
self.trainLoadImgBtn.clicked.connect(self.loadClicked)
self.image = QImage()
@pyqtSlot()
def loadClicked(self):
fname,filter=QFileDialog.getOpenFileName(self,'Open File','D:\',"Image Files(*.jpg)")
if fname:
self.loadImage(fname)
else:
print('invalid image')
def loadImage(self,fname):
self.image=cv2.imread(fname,cv2.IMREAD_COLOR)
self.displayImage()
def displayImage(self):
qformat =QImage.Format_Indexed8
if len(self.image.shape)==3:
if(self.image.shape[2])==4:
qformat=QImage.Format_RGBA8888
else:
qformat=QImage.Format_RGB888
img=QtGui.QImage(self.image.data,self.image.shape[1],self.image[0],QtGui.QImage.Format_RGB888)
img = img.rgbSwapped()
self.trainOpenImg.setPixmap(QPixmap.fromImage(img))
self.trainOpenImgn.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
if __name__ == "__main__":
app=QApplication(sys.argv)
window=UIProgram()
window.setWindowTitle('Test')
window.show()
sys.exit(app.exec_())
当我点击加载图像按钮时崩溃,图像无法在 qlabel 中显示 这个错误
Process finished with exit code -1073740791 (0xC0000409)
它有 2 个错误:
- 您必须将
bytesPerLine
传递给 QImage - 当您设置对齐时,您在
trainOpenImg
中有 "n" 个。
def displayImage(self):
qformat =QImage.Format_Indexed8
if len(self.image.shape)==3:
if self.image.shape[2] ==4:
qformat=QImage.Format_RGBA8888
else:
qformat=QImage.Format_RGB888
img = QtGui.QImage(self.image.data,
self.image.shape[1],
self.image.shape[0],
self.image.strides[0], # <--- +++
qformat)
img = img.rgbSwapped()
self.trainOpenImg.setPixmap(QPixmap.fromImage(img))
self.trainOpenImg.setAlignment(QtCore.Qt.AlignCenter)
另一方面,IDE 在处理某些类型的错误时存在问题并且只能启动代码,因此在这些情况下,建议在 CMD 或终端中 运行 它,因为它们会为您提供更多信息,因为在这种情况下的示例错误消息是:
Traceback (most recent call last):
File "test.py", line 20, in loadClicked
self.loadImage(fname)
File "test.py", line 26, in loadImage
self.displayImage()
File "test.py", line 36, in displayImage
img=QtGui.QImage(self.image.data,self.image.shape[1],self.image[0],QtGui.QImage.Format_RGB888)
TypeError: arguments did not match any overloaded call:
QImage(): too many arguments
QImage(QSize, QImage.Format): argument 1 has unexpected type 'memoryview'
QImage(int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
QImage(bytes, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(bytes, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(List[str]): argument 1 has unexpected type 'memoryview'
QImage(str, format: str = None): argument 1 has unexpected type 'memoryview'
QImage(QImage): argument 1 has unexpected type 'memoryview'
QImage(Any): too many arguments
Aborted (core dumped)