欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pyqt5笔记——QLable显示cv2读取的图片

程序员文章站 2022-03-20 18:04:29
...

在做pyqt5编程的时候,需要用QLable显示图片,QLable可以直接显示QPixmap读取的图片,但是显示numpy模块读取的图片时,需要做如下转换:

    def load_image(self,resize_height, resize_width,normalization=False):
        '''
            读取图片数据,默认返回的是uint8,[0,255]
            :param resize_height:
            :param resize_width:
            :param normalization:是否归一化到[0.,1.0]
            :return: 返回的图片数据
            '''
        bgr_image = cv2.imread(self.path)
        if len(bgr_image.shape) == 2:  # 若是灰度图则转为三通道
            print("Warning:gray image", self.path)
            bgr_image = cv2.cvtColor(bgr_image, cv2.COLOR_GRAY2BGR)

        self.rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)  # 将BGR转为RGB
        # show_image(filename,rgb_image)
        # rgb_image=Image.open(filename)
        if resize_height > 0 and resize_width > 0:
            self.rgb_image = cv2.resize(self.rgb_image, (resize_width, resize_height))
        self.rgb_image = np.asanyarray(self.rgb_image)
        if normalization:
            # 不能写成:rgb_image=rgb_image/255
            self.rgb_image = self.rgb_image / 255.0
        # show_image("src resize image",image)
        return self.rgb_image  
     #显示图片 
     def show_image(self):
        index = self.image_list.currentRow()
        #加载rgb图片
        rgb_image = self.images[index].load_image(self.pannel_height, self.pannel_width)
        label_image = QImage(rgb_image.data, rgb_image.shape[1], rgb_image.shape[0], QImage.Format_RGB888)#转化为QImage
        self.image_pannel.setPixmap(QPixmap(label_image))#设置显示图片