0

I want to display images using cv2 and combine QLabel with SetPixmap method to display images in my GUI. I use CV2 because of image processing purpose in Tumor Segmentation using UNET architecture. can anyone solve this? or any alternate code to make my program work as well.

Here is the code

class Autosegmentation(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.open_image_button.clicked.connect(self.opendialog)

    def opendialog(self):
        file_filter = "Images (*.png *.jpg *.tiff *.jpeg)"
        filename = QFileDialog.getOpenFileName(
            parent = self,
            caption = 'select a File',
            filter = file_filter)[0]
        
        self.setImage(filename)


    def setImage(self, image_path):
        self.cvimage = cv2.imread(image_path, cv2.IMREAD_COLOR)
        if self.cvimage is None:
            print('error')
            return
        
        self.cvimage = cv2.cvtColor(self.cvimage, cv2.COLOR_BGR2RGB)
        self.cvimage = self.cvimage/255.0
        self.cvimage = self.cvimage.astype(np.float32)

        
        height, width, channel = self.cvimage.shape
        bytes_per_line = 3 * width
        self.qt_image = QImage(self.cvimage.data, width, height, bytes_per_line * channel, QImage.Format.Format_RGB888)
        self.pixmap = QtGui.QPixmap.fromImage(self.qt_image)
        self.image_view.setPixmap(self.pixmap)
        self.image_view_2.setPixmap(self.pixmap)

and the output like this as you can see, the images output really bad and it shows only small grid.

if anyone have an idea to solve this or any alternative code. please Help me. Thankyou!

  • Are you sure about the /255 and float conversion? – Guimoute Aug 30 '23 at 13:46
  • @Guimoute i think that the/255 is a common preprocessing step when working with image data. am i wrong? – Pandu Hafizh Ananta Aug 30 '23 at 15:01
  • 2
    Sure, but the whole conversion to float here seems rather pointless, and seems to lead you to shooting your own foot. `bytes_per_line = 3 * width` seems rather wrong, given that you have 4-byte floating point elements, and later on you do `bytes_per_line * channel` where `channel == 3`. That means you claim each pixel takes up 9 bytes, when in fact it takes up 12 bytes in floating point RGB. Then you tell `QImage` constructor that the data format is actually `Format_RGB888` ("The image is stored using a 24-bit RGB format (8-8-8)")... and that's definitely not the case given it's 3x float32... – Dan Mašek Aug 30 '23 at 15:42
  • Oh i see,Thanks for the explanation, I already understand a little bit – Pandu Hafizh Ananta Aug 30 '23 at 17:36
  • So yeah, drop the conversion to float, and just feed it uint8 data. One thing to keep in mind doing that, is that rows have to be aligned at 4-byte boundaries. If your width is divisible by 4, then you're fine, otherwise you have to do some work. A workaround could be to convert to RGBA (and change the format argument appropriately) -- since each pixels takes 4 bytes in this case (as opposed to 3), correct alignment shouldn't be an issue. – Dan Mašek Aug 31 '23 at 02:19
  • Yes, thank you for your suggestion @relent95 – Pandu Hafizh Ananta Sep 02 '23 at 18:23

0 Answers0