Python and OpenCV are used to develop services and applications using Computer Vision, Machine Learning (ML), and Artificial Intelligence (AI). It is also used to learn about these technologies.
Once the development environment is set up, you can develop with Python and OpenCV using almost the same code and procedures, regardless of your OS. For example, the book I’m currently referencing for this learning article is based on Ubuntu Linux, but I’m using both Windows and macOS. Nevertheless, the Python code in the books works the same as it is. Also, the way to install the necessary libraries is the same.
This article describes how to set up Python and OpenCV for Computer Vision, ML, and AI.
WSL: Setting up Windows Services for Linux
WSL is required to develop on Windows in the same manner as on Ubuntu or macOS. See the following article on how to set up WSL.
In this article, we will run commands in a shell; on macOS, we will use the Terminal app; on Windows, we will use the WSL shell.
Python3 comes pre-installed on both Windows (WSL) and Ubuntu. However, even if it is not installed, it will be installed as one of the dependencies when
pip3 is installed later.
For macOS, when Xcode is installed, Python, and NumPy are also installed during the setup process when Xcode is launched for the first time.
Install Mesa 3D Graphics Library (Windows and Ubuntu)
The Mesa project is an open-source version of OpenGL. It can be installed with apt as follows.
sudo apt install libgl1-mesa-dev
Install Pip3 (Windows and Ubuntu)
Pip is Python’s official package management tool. It is probably installed by default, but you can install it with
sudo apt install python3-pip
Tkinter is a library for handling GUI from Python. We often use Matplotlib from Python to display images, graphs, etc. You can install it with
sudo apt install python3-tk
Without it installed, you will encounter the following error message, causing Matplotlib’s GUI functions to malfunction.
Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show()
Install OpenCV and subsequently use
pip3 to install Python packages, as we will utilize Python.
Input the following command to obtain a list of available versions.
pip3 install opencv-python==
Defaulting to user installation because normal site-packages is not writeable ERROR: Could not find a version that satisfies the requirement opencv-python== (from versions: 22.214.171.124, 126.96.36.199, 188.8.131.52, 184.108.40.206, 220.127.116.11, 18.104.22.168, 22.214.171.124, 126.96.36.199, 188.8.131.52, 184.108.40.206, 220.127.116.11, 18.104.22.168, 22.214.171.124, 126.96.36.199, 188.8.131.52, 184.108.40.206, 220.127.116.11, 18.104.22.168, 22.214.171.124, 126.96.36.199, 188.8.131.52, 184.108.40.206, 220.127.116.11, 18.104.22.168, 22.214.171.124) ERROR: No matching distribution found for opencv-python==
In this example, the latest version is
Install the latest version:
126.96.36.199 as found in STEP 1.
pip3 install opencv-python==188.8.131.52
Install OpenCV extensions
Install the OpenCV extension module using
pip3 as with OpenCV.
Enter the following to get a list of available versions to install
pip3 install opencv-contrib-python==
Defaulting to user installation because normal site-packages is not writeable ERROR: Could not find a version that satisfies the requirement opencv-contrib-python== (from versions: 184.108.40.206, 220.127.116.11, 18.104.22.168, 22.214.171.124, 126.96.36.199, 188.8.131.52, 184.108.40.206, 220.127.116.11, 18.104.22.168, 22.214.171.124, 126.96.36.199, 188.8.131.52, 184.108.40.206, 220.127.116.11, 18.104.22.168, 22.214.171.124, 126.96.36.199) ERROR: No matching distribution found for opencv-contrib-python==
Install the same version as OpenCV.
188.8.131.52 is installable. It is the same version as OpenCV.
pip3 install opencv-contrib-python==184.108.40.206
Matplotlib is a library used for drawing graphs in Python. If you try to use Matplotlib in an environment where Matplotlib is not installed, you will receive the following error.
from matplotlib import pyplot as plt ModuleNotFoundError: No module named 'matplotlib'
pip3 install matplotlib
You can also specify a version. If you do not specify a version, the latest version will be installed.
Install Python bindings for Qt
When using OpenCV
cv2.imshow(), etc., I get the following error if the Python bindings for Qt are not installed.
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/akira/.local/lib/python3.10/site-packages/cv2/qt/plugins" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: xcb. Aborted
Install the Python bindings for Qt. For example, install the Python3 binding for Qt5 with
apt as follows.
sudo apt install python3-pyqt5