Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Installation (C++)

OpenCV Installation (C++)

OpenCV (Open Source Computer Vision Library) is an open-source software library for computer vision and machine learning, containing hundreds of computer vision algorithms widely used in image processing, video analysis, object detection, face recognition, and other fields.

C++ is the original development language of OpenCV and is suitable for high-performance applications.

Before using OpenCV in a C++ project, you first need to install the OpenCV library.

Download OpenCV

Visit the official OpenCV download page:https://opencv.org/releases/。

Select the version suitable for your operating system (e.g., Windows, Linux, macOS) and download it, for example, the Windows precompiled package of OpenCV 4.x. .

Install OpenCV on Windows

Configure Visual Studio

1. Open Visual Studio and create a C++ project.

2. Configure the include directories

Right-click the project -> "Properties" -> "VC++ Directories" -> "Include Directories", add:



C:\opencv\build\include

3. Configure the library directories

Right-click the project -> "Properties" -> "VC++ Directories" -> "Library Directories", add:

C:\opencv\build\x64\vc15\lib

4. Configure the linker

Right-click the project -> "Properties" -> "Linker" -> "Input" -> "Additional Dependencies", add:

opencv_world4xx.lib

4xx is the OpenCV version number, for example opencv_world450.lib。


Install by compiling from source

Preparations before installation

Before installing OpenCV, make sure the following tools are installed on your system:

Download the OpenCV source code

We can download the source code from OpenCV's GitHub repository , or directly download a precompiled version from OpenCV's Official Website website.

Clone the source code using Git:



git clone https://github.com/opencv/opencv.git

git clone https://github.com/opencv/opencv_contrib.git  # 可选,包含额外的模块

Configure OpenCV using CMake

1. Create a build directory and enter it:



mkdir build

cd build

2. Use CMake to generate build files:

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

If you need additional modules (such as opencv_contrib), you can add the following parameters:



cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..

3. Compile and install OpenCV:



make -j4  # 使用 4 个线程进行编译

sudo make install

4. Configure environment variables (optional)

On Linux or macOS, you may need to add the OpenCV library path to the environment variables:



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

On Windows, you can add the OpenCV library path through System Properties -> Advanced System Settings -> Environment Variables.


Using OpenCV in a C++ project

After installation, you can use OpenCV in a C++ project.

The following is a simple example program showing how to use OpenCV to load and display an image.

1. Create a C++ project

Create a new C++ source file, for example main.cpp。

Write the following code:

Example

#include

#include

int

main

(

)

{

   

// read the image

    cv

::

Mat

image

=

cv

::

imread

(

"example.jpg"

)

;

   

// check whether the image was loaded successfully

   

if

(

image.

empty

(

)

)

{

        std

::

cout

<<

"Unable to load image!"

<<

std

::

endl

;

       

return

1

;

   

}

   

// display the image

    cv

::

imshow

(

"Display Image"

, image

)

;

   

// Wait for a key press

    cv

::

waitKey

(

0

)

;

   

return

0

;

}

2. Compile and run

Compile the code using the following command:



g++ main.cpp -o main `pkg-config --cflags --libs opencv4`

3. Run

Run the generated executable file:



./main

If everything goes well, you should see the image displayed in a window.


Common problems and solutions

1. OpenCV library not found

If you encounter an error that the OpenCV library cannot be found during compilation, make sure you have installed OpenCV correctly and that the environment variables are configured properly.

We can use the pkg-config to check the OpenCV installation path:



pkg-config --cflags --libs opencv4

2. Image cannot be loaded

If the image cannot be loaded, check whether the image path is correct and whether the image file exists. You can use an absolute path to ensure the path is correct.

3. Window cannot be displayed

If the window cannot be displayed, make sure your system supports a graphical interface and that the OpenCV GUI module is correctly installed.