1、提取Qlabel中的图片
qimg=self.showScreenImgLabel.pixmap().toImage()
2、将Qimage转换为mat
def qimage2mat(self,qimg):ptr = qimg.constBits()ptr.setsize(qimg.byteCount())mat = np.array(ptr).reshape(qimg.height(), qimg.width(), 4) # 注意这地方通道数一定要填4,否则出错return matdef slotMedianBl
3、在将mat进行其他处理之前,必须将mat 的BGR转换为RGB。
全部代码如下:
def qimage2mat(self,qimg):ptr = qimg.constBits()ptr.setsize(qimg.byteCount())mat = np.array(ptr).reshape(qimg.height(), qimg.width(), 4) # 注意这地方通道数一定要填4,否则出错return matdef slotMedianBlurAction(self):print("中值滤波")qimg=self.showScreenImgLabel.pixmap().toImage() #获取Qlabel图片mat_img_temp=self.qimage2mat(qimg) #将Qimage转换为mat类型mat_img=cv2.cvtColor(mat_img_temp, cv2.COLOR_BGR2RGB)#在对图像处理前 先转换为RGB类型 切记img_median = cv2.medianBlur(mat_img, 5)gqimg=QImage(img_median.data, img_median.shape[1], img_median.shape[0],img_median.shape[1] * 3,QImage.Format_RGB888).scaled(self.showProcessImgLabel.width(),self.showProcessImgLabel.height())self.showProcessImgLabel.setPixmap(QPixmap.fromImage(gqimg))