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 mat
def 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 mat
def 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))