How to Integrate PyQt5 with OpenCV

In this PyQt5 OpenCV article we are going to talk about How to Integrate PyQt5 with OpenCV, PyQt5 is popular Python library used for creating graphical user interfaces (GUIs). on the other hand OpenCV is widely used computer vision library that provides tools for image and video processing. by integrating these two libraries we can create powerful applications for video and image processing, In this article, we will discuss how to integrate PyQt5 with OpenCV.

 

 

 

How to Integrate PyQt5 with OpenCV

First of all we need to install OpenCV and PyQt5

 

 

 

So now this is the complete code for this article

 

 

In the above code cv2 library is imported to read and process images using OpenCV. QPixmap and QImage classes are imported from PyQt5.QtGui to handle image display, and QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout and QWidget are imported from PyQt5.QtWidgets to create the GUI.

ImageWidget class is defined to display the processed image in the GUI. set_image method sets the image displayed in the QLabel to the processed image by converting NumPy ndarray to QImage object and then creating QPixmap object from the QImage.

process_image function reads an image file using cv2.imread, converts it to grayscale using cv2.cvtColor, and returns the processed image as NumPy ndarray.

MainWindow class is defined to create the main window of the application. process_image method is called when the button is clicked, and it sets the processed image in the ImageWidget using the set_image method.

an at the end if __name__ == __main__ block creates QApplication and MainWindow objects, shows main window and starts the event loop with app.exec_().

 

 

 

Run the code and this will be the result

How to Integrate PyQt5 with OpenCV
How to Integrate PyQt5 with OpenCV

 

 

 

 

Learn More on Python

 

Leave a Comment