Wsl安装opencv

8 min read Oct 01, 2024
Wsl安装opencv

Installing OpenCV on Windows Subsystem for Linux (WSL)

OpenCV is a powerful open-source computer vision library that provides a wide range of tools for image and video processing, analysis, and manipulation. If you're using Windows and want to leverage the power of OpenCV, installing it on the Windows Subsystem for Linux (WSL) is a great option.

This article will guide you through the process of installing OpenCV on WSL, providing a comprehensive step-by-step walkthrough to ensure a smooth installation.

Prerequisites

Before we begin, ensure you have the following prerequisites in place:

  • Windows 10 or 11: WSL is supported on these Windows versions.
  • Enabled WSL: WSL needs to be enabled on your system. You can enable it by searching for "Turn Windows features on or off" in the Windows search bar and checking the box for "Windows Subsystem for Linux."
  • Linux distribution: Choose a Linux distribution of your preference from the Microsoft Store. Popular choices include Ubuntu, Debian, and Kali Linux.
  • Basic Linux knowledge: Familiarity with basic Linux commands will be helpful for navigating the terminal and installing software.

Step-by-Step Installation

1. Update Your Linux Distribution:

sudo apt update && sudo apt upgrade -y 

This command updates your system's package lists and upgrades existing packages.

2. Install Necessary Dependencies:

sudo apt install build-essential cmake pkg-config libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libwebp-dev python3-dev python3-numpy

These packages include essential libraries and tools required for building OpenCV from source, including:

  • build-essential: A collection of tools for compiling software.
  • cmake: A build system generator used to configure OpenCV.
  • pkg-config: A utility for finding and managing libraries.
  • libgtk-3-dev: Libraries for graphical user interface development (optional for using OpenCV's GUI features).
  • libavcodec-dev, libavformat-dev, libswscale-dev: Libraries for video encoding and decoding.
  • libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libwebp-dev: Libraries for image format support.
  • python3-dev, python3-numpy: Libraries for Python bindings (optional for using OpenCV with Python).

3. Download the OpenCV Source Code:

OpenCV's official website provides a variety of download options. Choose a suitable release version based on your needs. For example, you can download the latest stable release using the following commands:

cd /tmp
wget https://github.com/opencv/opencv/archive/refs/heads/4.x.zip
unzip 4.x.zip
cd opencv-4.x

Replace 4.x with the specific version you want to download.

4. Configure and Build OpenCV:

mkdir build
cd build
cmake -D WITH_OPENGL=ON -D WITH_CUDA=OFF -D WITH_TBB=ON ..
make -j4  # Adjust the -j flag for your CPU's core count
sudo make install

This process involves:

  • Creating a build directory for compiling.
  • Using CMake to configure OpenCV with your system's libraries and options.
  • Building OpenCV with the make command.
  • Installing OpenCV to the system's default location.

Important Notes:

  • -D WITH_OPENGL=ON: This option enables support for OpenGL, which can improve performance for certain operations.
  • -D WITH_CUDA=OFF: This option disables support for CUDA, NVIDIA's GPU acceleration framework. If you have an NVIDIA GPU and want to utilize CUDA, set this option to ON.
  • -D WITH_TBB=ON: This option enables Intel's Threading Building Blocks, which can improve performance for multi-threaded operations.
  • make -j4: This command specifies the number of cores to use for parallel compilation. Adjust the number based on your CPU's core count.

5. Verify the Installation:

pkg-config --cflags opencv
pkg-config --libs opencv

These commands will output the necessary flags for compiling and linking OpenCV with your programs.

6. Test OpenCV:

Create a simple C++ program to test if OpenCV is installed correctly:

#include 

int main()
{
    cv::Mat image = cv::imread("path/to/your/image.jpg"); 
    if (!image.empty())
    {
        cv::imshow("Image", image);
        cv::waitKey(0);
    } else {
        std::cout << "Failed to load image." << std::endl;
    }
    return 0;
}

Compile and run the program:

g++ your_program.cpp -o your_program `pkg-config --cflags --libs opencv`
./your_program

If you see a window displaying your image, the installation is successful!

Using OpenCV in Python

If you want to use OpenCV with Python, you need to install the Python bindings:

pip3 install opencv-python

You can then import OpenCV into your Python scripts:

import cv2

# Load an image
image = cv2.imread("path/to/your/image.jpg")

# Display the image
cv2.imshow("Image", image)
cv2.waitKey(0)

Troubleshooting

  • CMake Errors: If CMake fails to find necessary dependencies, double-check that you installed the required libraries and their development packages.
  • Build Errors: If you encounter build errors, review the output carefully for clues about the issue. Refer to OpenCV's documentation or online forums for help.
  • "No module named 'cv2'" in Python: Make sure you installed opencv-python correctly.

Conclusion

Installing OpenCV on WSL provides you with a powerful environment for computer vision projects. By following these steps, you can successfully set up OpenCV and start exploring its capabilities. Remember to update your WSL distribution and dependencies regularly to ensure compatibility and avoid potential issues.