How to Integrate PyQt6 with OpenCV

In this PyQt6 OpenCV article we are going to learn How to Integrate PyQt6 with OpenCV, PyQt6 is popular Python binding for the Qt GUI toolkit and it allows developers to create rich desktop applications with user friendly interface. on the other hand OpenCV is powerful computer vision library that provides different algorithms for image and video processing. we can combine these two libraries and enable developers to build advance desktop applications that require image and video processing, in this article we want to learn how to integrate PyQt6 with OpenCV.

 

 

 

How to Integrate PyQt6 with OpenCV

First of all we need to install PyQt6 and OpenCV in Python

 

 

So now after installation let’s create simple GUI window with PyQt6, also we will have PyQt6 QPushButton in the GUI window,  this is the complete code.

In the above code we have created QMainWindow object, which is the main window of the GUI. also we have added QLabel object to display the image and QPushButton object to trigger the image processing function. we have also defined a skeleton for the image processing function, which we will implement later.

 

 

Run the code and this is the result

How to Integrate PyQt6 with OpenCV
How to Integrate PyQt6 with OpenCV

 

 

 

Now let’s the add functionality to load and display an image on the GUI. we are going to use QFileDialog object to allow the user to select an image file. once the user selects an image file, we will load it using OpenCV and display it on the QLabel object.

In the above code we have created menu bar, also we have added Open menu item to the Menubar, when the user click on the Open we are going to show a file dialog which allows the user to select an image file. when the user selects an image file, we read it using OpenCV’s imread function and display it on the QLabel object using a QPixmap object.

 

 

Run the code and open an image.

How to Integrate PyQt6 with OpenCV
How to Integrate PyQt6 with OpenCV

 

 

 

Now let’s implement the image processing in OpenCV with PyQt6, by clicking of the button we are going to convert the loaded image to gray scale.

In the above code we have converted loaded image to grayscale using OpenCV’s cvtColor function. after that we convert the grayscale image to QImage object and create QPixmap object from it. at the end we display the converted image on the QLabel object.

 

 

 

If you run the complete code this will be the result.

How to Integrate PyQt6 with OpenCV
How to Integrate PyQt6 with OpenCV

 

 

 

Learn More on Python GUI

 

Leave a Comment