Logo

dev-resources.site

for different kinds of informations.

Building OpenCV 4.10.0 with GUI Support in WSL

Published at
12/29/2024
Categories
opencv
wsl
ubuntu
make
Author
eddiegulay
Categories
4 categories in total
opencv
open
wsl
open
ubuntu
open
make
open
Author
10 person written this
eddiegulay
open
Building OpenCV 4.10.0 with GUI Support in WSL

If you’re working with OpenCV on WSL and hit the infamous cv2.error stating GUI: NONE, you’ve probably realized that your OpenCV installation lacks GUI backend support. This guide will walk you through building OpenCV 4.10.0 from source with full GUI support, ensuring functions like cv2.imshow work seamlessly. Let’s dive in!


Prerequisites

Before we begin, make sure you have WSL set up (preferably WSL2) with a Linux distribution like Ubuntu. We’ll also need to install several dependencies.

Step 1: Install Required Dependencies

Open your terminal and run the following commands to install the necessary development tools and libraries:

sudo apt update
sudo apt install -y build-essential cmake git pkg-config
sudo apt install -y libjpeg-dev libpng-dev libtiff-dev
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev
sudo apt install -y libv4l-dev v4l-utils
sudo apt install -y libxvidcore-dev libx264-dev
sudo apt install -y libgtk2.0-dev libgtk-3-dev libcanberra-gtk-module libcanberra-gtk3-module
sudo apt install -y python3-dev python3-pip python3-numpy
sudo apt install -y libopenblas-dev libatlas-base-dev liblapack-dev gfortran
sudo apt install -y libhdf5-dev libprotobuf-dev protobuf-compiler
Enter fullscreen mode Exit fullscreen mode

These libraries ensure that OpenCV can handle image processing, video decoding, and GUI rendering.


Step 2: Download OpenCV and Contrib Modules

To build OpenCV from source, we need both the main OpenCV repository and the additional contrib modules for extended functionality.

Clone OpenCV:

git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 4.x
Enter fullscreen mode Exit fullscreen mode

Clone the Contrib Modules:

cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 4.x
Enter fullscreen mode Exit fullscreen mode

Step 3: Build OpenCV

Create a Build Directory

Navigate back to the OpenCV directory and create a build directory:

cd ../opencv
mkdir build
cd build
Enter fullscreen mode Exit fullscreen mode

Configure the Build

Run cmake to configure the OpenCV build, ensuring GUI support is enabled:

cmake -D CMAKE_BUILD_TYPE=Release \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D WITH_GTK=ON \
      -D WITH_OPENGL=ON \
      -D BUILD_EXAMPLES=ON ..
Enter fullscreen mode Exit fullscreen mode

Compile OpenCV

Use the following command to compile OpenCV. This process can take some time depending on your system:

make -j$(nproc)
Enter fullscreen mode Exit fullscreen mode

Here, $(nproc) ensures all available CPU cores are used for compilation.

Install OpenCV

Once the compilation is complete, install OpenCV on your system:

sudo make install
sudo ldconfig
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify the Installation

Let’s check if OpenCV is installed correctly and GUI support is enabled:

Run a Python Script

Open Python and run the following:

import cv2
print(cv2.getBuildInformation())
Enter fullscreen mode Exit fullscreen mode

Look for the GUI section in the output. It should list GTK or similar. If it says NONE, ensure the required libraries were installed before running cmake and repeat the build process.


Step 5: Test cv2.imshow

To confirm everything works as expected, try displaying an image:

Sample Python Script:

import cv2

# Load an image
image = cv2.imread('path_to_your_image.jpg')

# Display the image
cv2.imshow('Test Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

If a window pops up showing your image, congratulations! You’ve successfully built OpenCV with GUI support.


Troubleshooting Tips

  1. GUI: NONE Still Appears:

    • Ensure you installed libgtk2.0-dev and libgtk-3-dev before running cmake.
    • Delete the build directory and re-run the cmake and make steps.
  2. Errors During Compilation:

    • Check for missing dependencies in the error messages and install them.
    • Ensure your system has enough memory; close other programs if necessary.
  3. Display Issues in WSL:

    • Use an X server like VcXsrv on Windows if you’re not using WSLg.
    • Export your display:
     export DISPLAY=$(hostname).local:0
    

Highlight

Building OpenCV 4.10.0 from source may seem daunting, but it’s worth the effort for the flexibility and customization it offers. Whether you’re working on image processing, computer vision, or machine learning projects, having a fully functional OpenCV installation will unlock countless possibilities.

Got stuck? Drop your errors or questions in the comments—I’m here to help!
Happy coding!


Buy Eddie a coffee

wsl Article's
30 articles in total
Favicon
Update WSL Ubuntu password
Favicon
Building OpenCV 4.10.0 with GUI Support in WSL
Favicon
Video: Install Ubuntu using WSL 2
Favicon
Install Zellij on WSL
Favicon
Work from anywhere with VSCode Remote Tunnels
Favicon
SSL verification error at depth 2 - Zscaler | WSL
Favicon
Accelerate 1-bit LLM Inference with BitNet on WSL2 (Ubuntu)
Favicon
Just start using WSL
Favicon
Change WSL/Docker files to another disk
Favicon
Setup your laravel 11 in windows
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
WSL in AWS Windows Server 2022 Core instance
Favicon
Docker Installation Log for WSL 2 on Windows
Favicon
Enable WSL shell in GitHub Desktop
Favicon
Installing Ruby using rbenv on your WSL Ubuntu system
Favicon
How to Install Redis on Windows Using WSL2
Favicon
PowerShell Development in Neovim
Favicon
WSL: Gerenciando o disco da distro
Favicon
Docker
Favicon
Share Postgresql from Windows to WSL Linux
Favicon
Add Manjaro into WSL 2
Favicon
Using WSL2 as primary driver for 3 months with Fedora
Favicon
Wsl installation error
Favicon
Install Debian in Different Location -WSL Windows
Favicon
Monitor GPU Usage in WSL debian
Favicon
Dicas e configurações para seu sistema linux
Favicon
Instalando de maneira rápida e eficiente suas ferramentas no WSL. Pt-3
Favicon
Arch install azure cli
Favicon
Melhorando e configurando seu novo Shell linux. Pt-2

Featured ones: