DelphiPythonPython GUIWindows

Learn To Build A Python GUI For Computer Vision Tasks With Powerful OpenCV Library In A Delphi Windows App

blogbanner3 29

Are you looking for a powerful computer vision library and build a nice GUI for them? Try OpenCV library for Python. And for the GUI part, you can run it seamlessly with Python4Delphi (P4D). P4D is a free and simple tool that allows you to run Python scripts as well as create new Python modules and types in Delphi.

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products.

This post will guide you on how to run the OpenCV library to solve computer vision problems and use Python for Delphi to display it in the Delphi Windows GUI app.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1 6813476

OpenCV is an open-source library for computer vision and machine learning that supports various programming languages including Python. 

OpenCV has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high-resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc.

This post will introduce you to some basic computer vision operations using OpenCV and we will run it in Python GUI.

1. Loading and Displaying an Image

Let’s begin by running the following code in our Python4Delphi VCL:

The code above will display the image on our screen using OpenCV, and will print the width, height, and depth data about our image:

1 1 8173422

 

2. Converting an Image to Grayscale

Here is the code to convert our image to grayscale:

The result:

1 2 2177692

 

3. Edge Detection

Edge detection is useful for finding boundaries of objects in an image—it is effective for segmentation purposes.

Let’s perform edge detection to see how the process works, using the following code:

This operation was performed using the popular Canny algorithm (developed by John F. Canny in 1986) to find the edges in the image.

We provide three parameters to the cv2.Canny function:

  • img: The gray image.
  • minVal: A minimum threshold, in our case we set it to 175.
  • maxVal: The maximum threshold which is 200 in our example.

This is the result of edge detection for our image:

1 3 7444491

 

4. Perspective Transformation of an Image

With this operation, we want to zoom-in Daenerys Targaryen face using perspective transformation:

To perform perspective transformation with an image, we use the warpPerspective() function. The parameters of this function are the original image, the transformation matrix, and the size of the output image. Use getPerspectiveTransform() function to get the transformation matrix. You need to pass four points of the input image and the corresponding four points of the output image to this function. Important to note that three of the four points should not be on the same straight line.

Let’s see the result:

1 4 7782366

Congratulations, now you have learned how to run the OpenCV library to solve computer vision problems and use Python for Delphi to display it in the Delphi Windows GUI app! Now you can make various modifications to your images or learn more computer vision operations using OpenCV library and Python4Delphi.

Check out the OpenCV computer vision library for Python and use it in your projects: https://pypi.org/project/opencv-python/ and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *