DEV Community

Cover image for Building OpenCV 4.10.0 with GUI Support in WSL
Eddie Gulay
Eddie Gulay

Posted on

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!

Top comments (0)