0

I want to create a simple GUI to where on I can apply filters on the image. However I faced a problem: When I want to call filter function, Pyqt does not accept opencv's representation of the image. How can I combine these two libraries and apply the dilation filter?

import sys
import numpy as np
import cv2 as cv
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *


class SpeechEditor(QMainWindow):
    # ---------- Constructor for the mainWindow ----------
    def __init__(self):
        super().__init__()
        # set the window dimensions
        self.uploadedImage = None
        self.setWindowTitle("Editor")
        self.centralWidget = QWidget()
        self.centralWidget.setStyleSheet(
            "background-image: url(editor-background.jpg);"
        )
        self.setCentralWidget(self.centralWidget)
        self.setGeometry(0, 0, 2000, 1000)

        self.titleContainer = QLabel("SPEECH EDITOR",self)
        self.titleContainer.setStyleSheet(
            "border: 3px solid #25BA39;"
            "font-size:40px;"
        )
        self.titleContainer.setGeometry(550, 0, 350, 100)
        self.titleContainer.setAlignment(Qt.AlignmentFlag.AlignCenter)

        self.imageContainer = QLabel(self)
        self.imageContainer.setStyleSheet(
            "border: 3px solid #25BA39;"
        )
        self.imageContainer.setGeometry(350, 120, 800, 600)
        self.imageContainer.setScaledContents(True)

        # ----------- File Menu in toolbar ----------
        # Button to upload the image
        self.fileEvent = QAction(QIcon("file.svg"), "Upload New Image", self)
        # bottom space to help navigating the through program
        self.setStatusBar(QStatusBar(self))
        # create a menu bar for the file menu
        self.menu = self.menuBar()
        # add a menu for the file menu
        self.fileMenu = self.menu.addMenu("File")
        # add open file function to the file menu
        self.fileMenu.addAction(self.fileEvent)
        # call display the image function to show on the screen
        self.fileEvent.triggered.connect(self.imageDisplay)
        # set a shortcut for opening the window
        self.fileEvent.setShortcut(QKeySequence("Ctrl+O"))

        # create a button for dilating the image
        self.dilate = QPushButton(QIcon("shield.svg"), "", self)
        # set dimensions of the button
        self.dilate.setGeometry(1413, 422, 25, 25)
        # set style for the button
        self.dilate.setStyleSheet(
            "background-color : #25BA39;"
            "border : 3px solid #fff;"
        )
        # give a tooltip for better navigation through application
        self.dilate.setStatusTip("Dilate The Image")
        self.dilate.clicked.connect(self.imageDilate)

    # function to display image on the screen
    def imageDisplay(self):
        # get access to the file dialog to upload the desired image
        fileName = QFileDialog.getOpenFileName(self, "Upload New Image", "Macintosh HD:\\Users\\imbarron\\Downloads", "All Files (*);;")
        # get the image from file dialog, and flush it to the screen
        self.uploadedImage = QPixmap(fileName[0])
        # take the uploaded image and place it inside the label in order for it to appear
        self.imageContainer.setPixmap(self.uploadedImage)

    # ------ Dilating image --------
    def imageDilate(self):
        # create an array set of the 5X5 pixels
        node = np.ones((5, 5), np.uint8)
        # apply dilation to the image using every 5 pixels in X and Y directions
        self.uploadedImage = cv.dilate(self.uploadedImage, node, iterations=1)


converter = QApplication(sys.argv)
mainWindow = SpeechEditor()
mainWindow.show()
sys.exit(converter.exec())

This is my code with dilation filter. Could someone please help me with this issue?

Markus
  • 5,976
  • 5
  • 6
  • 21
Hejee
  • 1
  • 1
    In OpenCV an image are stored in NumPy array. Look for converting NumPy array to QPixmap. The [following post](https://stackoverflow.com/questions/34232632/convert-python-opencv-image-numpy-array-to-pyqt-qpixmap-image) may help. – Rotem Nov 06 '22 at 21:52

0 Answers0