Using OpenCV C++ on Windows Subsystem for Linux (WSL)

Setting up OpenCV C++ on Windows Subsystem for Linux (WSL) involves several steps, including installing WSL, installing necessary dependencies, downloading the OpenCV source code, and compiling and installing OpenCV. This blog post provides a step-by-step guide to help you through the process.

Summary

The process of using OpenCV C++ on Windows Subsystem for Linux (WSL) involves enabling WSL on your Windows system, installing a Linux distribution (like Ubuntu), updating and installing required dependencies, downloading the OpenCV and OpenCV Contrib source code, configuring the build environment with CMake, compiling and installing OpenCV, and finally verifying the installation by running a simple OpenCV C++ program.

Step-by-Step Guide

  1. Install WSL: Ensure that WSL is enabled on your Windows 10 or higher system. You can run the following command in PowerShell to enable WSL and install Ubuntu (or any other Linux distribution of your choice):

    1
    wsl --install

    After restarting your computer, open the Microsoft Store, search for Ubuntu, and install your preferred version.

  2. Update and Install Dependencies: Launch your Linux distribution (e.g., Ubuntu), and use the following commands to update your package manager and install the required dependencies for compiling OpenCV:

    1
    2
    3
    4
    5
    6
    sudo apt update && sudo apt upgrade -y
    sudo apt install -y build-essential cmake git pkg-config libgtk-3-dev \
    libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
    gfortran openexr libatlas-base-dev python3-dev python3-numpy \
    libtbb2 libtbb-dev libdc1394-22-dev
  3. Download OpenCV and OpenCV Contrib Source Code: Download the OpenCV and OpenCV Contrib source code from the OpenCV GitHub repository:

    1
    2
    3
    mkdir ~/opencv_build && cd ~/opencv_build
    git clone https://github.com/opencv/opencv.git
    git clone https://github.com/opencv/opencv_contrib.git

    Ensure both repositories are on the same version, e.g., OpenCV 4.x:

    1
    2
    3
    4
    cd ~/opencv_build/opencv
    git checkout 4.x
    cd ../opencv_contrib
    git checkout 4.x
  4. Compile and Install OpenCV: Create a build directory, configure the build environment using CMake, and then compile and install OpenCV:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    cd ~/opencv_build/opencv
    mkdir build && cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..
    make -j$(nproc)
    sudo make install
  5. Configure Environment Variables: To ensure the compiler can find the OpenCV libraries, you need to update the LD_LIBRARY_PATH environment variable. Add the following line to your ~/.bashrc or ~/.profile file:

    1
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

    Then, execute source ~/.bashrc or restart your terminal application to apply the changes.

  6. Verify Installation: Compile and run a simple OpenCV program to verify the successful installation. You can create a simple C++ program that uses OpenCV to read and display an image.

By following these steps, you should be able to successfully set up and use OpenCV C++ on the Windows Subsystem for Linux (WSL).


Using OpenCV C++ on Windows Subsystem for Linux (WSL)
http://jingmengzhiyue.top/2024/03/16/OpenCV-WSL/
作者
Jingmengzhiyue
发布于
2024年3月16日
许可协议