Saturday, May 11, 2019

Image Filter with OpenCV and PyQt

In this tutorial, you will learn how to use OpenCV module to filter image using kernels and to display the resulting image with PyQt. 

Follow these steps below:
  1. Open Qt Designer. Put four Label widgets dan four Push Button widgets onto form.
  2. Set objectName property of the four Label widgets by name labelImage, labelIdentitylabelKernel3x3, and labelKernel5x5.
  3. Set objectName property of the four Push Button widgets by name pbOriginalImagepbIdentityImagepbKernel3x3, and pbKernel5x5.
  4. Name form as image_filter.ui.
  5. Form now looks as follows:


  6. Write this script and name it as image_filter.py:

  7. import sys
    import cv2
    import numpy as np
    from PyQt5.QtWidgets import QApplication, QWidget, QLabel
    from PyQt5.QtWidgets import QDialog, QFileDialog
    from PyQt5.QtGui import QIcon, QPixmap, QImage
    from PyQt5.uic import loadUi
    
    fname = ""
        
    class FormImageFilter(QDialog):
    
        def __init__(self):
            QDialog.__init__(self)
            loadUi("image_filter.ui",self)
    
            self.setWindowTitle("Image Filter")
            self.pbOriginalImage.clicked.connect(self.display_image)
            self.pbIdentityImage.clicked.connect(self.image_identity)
            self.pbKernel3x3.clicked.connect(self.kernel3x3)
            self.pbKernel5x5.clicked.connect(self.kernel5x5)
    
        def display_image(self):
            global fname
            fname = QFileDialog.getOpenFileName(self, 'Open file', 
               'd:\\',"Image Files (*.jpg *.gif *.bmp *.png *.tiff)")
            pixmap = QPixmap(fname[0])
            self.labelImage.setPixmap(pixmap)
            self.labelImage.setScaledContents(True);
    
        def image_identity(self, b=None):
            read_img = cv2.imread(fname[0], cv2.IMREAD_COLOR)
            kernel_identity = np.array([[0,0,0], [0,1,0], [0,0,0]])
            
            # To filter image using identity kernel
            output = cv2.filter2D(read_img, -1, kernel_identity)
            
            # To convert back from BGR to RGB space color
            cv2.cvtColor(output, cv2.COLOR_BGR2RGB, output)
            
            # To display image in label widget
            height, width, channel = output.shape
            bytesPerLine = 3 * width
            qImg = QImage(output.data, width, height, bytesPerLine, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qImg)
            self.labelIdentity.setPixmap(pixmap)
            self.labelIdentity.setScaledContents(True);
    
        def kernel3x3(self):
            read_img = cv2.imread(fname[0], cv2.IMREAD_COLOR)
            
            # To filter image using kernel 3x3
            kernel_3x3 = np.ones((3,3), np.float32) / 9.0
            output = cv2.filter2D(read_img, -1, kernel_3x3)
            
            # To convert back from BGR to RGB space color
            cv2.cvtColor(output, cv2.COLOR_BGR2RGB, output)
            
            # To display image in label widget
            height, width, channel = output.shape
            bytesPerLine = 3 * width
            qImg = QImage(output.data, width, height, bytesPerLine, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qImg)
            self.labelKernel3x3.setPixmap(pixmap)
            self.labelKernel3x3.setScaledContents(True);
    
        def kernel5x5(self):
            read_img = cv2.imread(fname[0], cv2.IMREAD_COLOR)
            
            # To filter image using kernel 5x5
            kernel_5x5 = np.ones((5,5), np.float32) / 25.0
            output = cv2.filter2D(read_img, -1, kernel_5x5)
            
            # To convert back from BGR to RGB space color
            cv2.cvtColor(output, cv2.COLOR_BGR2RGB, output)
            
            # To display image in label widget
            height, width, channel = output.shape
            bytesPerLine = 3 * width
            qImg = QImage(output.data, width, height, bytesPerLine, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qImg)
            self.labelKernel5x5.setPixmap(pixmap)
            self.labelKernel5x5.setScaledContents(True);
            
    if __name__=="__main__":
        app = QApplication(sys.argv)    
        w = FormImageFilter()
        w.show()
        sys.exit(app.exec_())
    

  8. Run the program and you now can see the resulting images (identity image and two kernel images):



No comments:

Post a Comment