我是PyQt的新手。我想建立一个简单的图像处理。我已经将RGB图像变成了灰色,如何将图像变成二进制(使用OpenCV)并显示在标签上? 我无法在PyQt上实现代码
将图像转换为灰色:
class UIProgram(QMainWindow):
def __init__(self):
super(UIProgram,self).__init__()
loadUi("Backpro2.ui",self)
#self.image=None
self.trainLoadImgBtn.clicked.connect(self.loadClicked)
self.trainPreproBtn.clicked.connect(self.preproClicked)
self.image = QImage()
@pyqtSlot()
def preproClicked(self):
gray = cv2.cvtColor(self.image, cv2.IMREAD_COLOR)
self.image = cv2.cvtColor(gray,cv2.COLOR_BGR2GRAY)
#(thresh, self.im_bw) = cv2.threshold(self.image, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#self.resize_image = cv2.resize(self.im_bw, (180, 180))
self.displayImage(2)
def loadImage(self, fname):
self.image = cv2.imread(fname,cv2.IMREAD_COLOR)
#self.image2 = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
self.displayImage(1)
在Qlabel中显示
def displayImage(self,window=1):
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()
if window==1:
pixmap = QtGui.QPixmap(img)
pixmap = pixmap.scaled(self.trainOpenImg.width(), self.trainOpenImg.height(), QtCore.Qt.KeepAspectRatio)
self.trainOpenImg.setPixmap(pixmap)
self.trainOpenImg.setAlignment(QtCore.Qt.AlignCenter)
if window==2:
pixmap = QtGui.QPixmap(img)
pixmap = pixmap.scaled(self.trainGrayImg.width(), self.trainGrayImg.height(), QtCore.Qt.KeepAspectRatio)
self.trainGrayImg.setPixmap(pixmap)
self.trainGrayImg.setAlignment(QtCore.Qt.AlignCenter)
程序设计: