1. Background
The project I am currently maintaining runs on Windows and uses OpenCV version 4.5.2.
My local development environment is a Mac, and I am using the newer OpenCV version 4.8.1. To maintain consistency with my local development environment, I plan to upgrade the OpenCV used in the project, as the project still utilizes some features from the extension modules, thus requiring a custom compilation.
This article serves as a record of the pitfalls I encountered during the compilation process, to avoid repeating them in the future or to help those in need.
2. Required Software
CMake version 3.13 or higher https://cmake.org/
Visual Studio 2022 Community Edition https://visualstudio.microsoft.com/zh-hans/vs/
OpenCV 4.8.1 source package https://github.com/opencv/opencv/releases/tag/4.8.1
OpenCV 4.8.1 extension module source package https://github.com/opencv/opencv_contrib/releases/tag/4.8.1
3. Compilation
3.1 Using CMake
3.1.1 Preparation
Unzip the OpenCV 4.8.1 source package and the extension module source package to a specific location
Open cmake-gui, set the location of the OpenCV 4.8.1 source and the output binary file location
3.1.2 Configure
Click “Configure”, a popup will show that the build directory has not been created, asking if you want to create it.
After clicking “Yes”, CMake will prompt you to select the tool to generate this project. We will use Visual Studio 2022.
After successful configuration, this interface will appear
At this point, we can set the path for the extension modules
When setting the path for the extension modules, make sure to point to the modules folder, otherwise the next “Configure” will fail.
If you need to compile the OpenCV World module, you also need to set it separately here.
The World module is a super-module that combines all other modules selected by the user. It is an all-in-one module with the functionalities of all libraries.
Then click “Configure” again.
At this point, it’s best to check the CMake download log; below is the download log from CMake.

Failure to download certain files may ultimately lead to the OpenCV World module not compiling successfully.
I mainly modified the following places:
a. Navigate to C:/opencv-4.8.1/opencv-4.8.1/3rdparty/ippicv directory and change the https://raw.githubusercontent.com in ippicv.cmake to: https://raw.staticdn.net
b. Navigate to C:/opencv-4.8.1/opencv-4.8.1/3rdparty/fmpeg directory and change the https://raw.githubusercontent.com in ffmpeg.cmake to: https://raw.staticdn.net
c. Navigate to C:/opencv_contrib-4.8.1/opencv_contrib-4.8.1/modules/xfeatures2d/cmake directory and change the https://raw.githubusercontent.com in download_boostdesc.cmake to: https://raw.staticdn.net
d. Navigate to C:/opencv_contrib-4.8.1/opencv_contrib-4.8.1/modules/xfeatures2d/cmake directory and change the https://raw.githubusercontent.com in download_vgg.cmake to: https://raw.staticdn.net
e. Navigate to C:/opencv_contrib-4.8.1/opencv_contrib-4.8.1/modules/face directory and modify CMakeLists.txt to change https://raw.githubusercontent.com to: https://raw.staticdn.net
After that, just re-click “Configure”.
3.1.3 Generate
Next, click “Generate” to create the project built using CMake
3.1.4 Open Project
Finally, click “Open Project” to invoke Visual Studio 2022 to open the project.
You can also go to the output binary file directory (C:/opencv-4.8.1/newbuild) and open the OpenCV.sln file to achieve the same effect.

3.2 Using Visual Studio 2022
Before compiling, first configure the opencv_world module, then choose release or debug.
Find CMakeTargets and select ALL_BUILD to generate.
After successfully generating the above steps, select INSTALL to generate.
After the INSTALL generation is successful, this compilation can be considered a success. We have completed the compilation of OpenCV 4.8.1 and its extension modules.
We can find the install folder in the output binary file directory (C:/opencv-4.8.1/newbuild)
The contents of the install folder are as follows
We have also successfully compiled the opencv_world module.
4. Conclusion
This article serves as a reference for myself in the future.
However, the entire compilation process took over a day to complete. During the compilation, I encountered various issues; as long as I carefully read the logs and analyzed them, I could always find a solution.
Of course, compiling the OpenCV library on your own has many benefits, allowing for customization of the OpenCV library according to your needs, and optimizing the OpenCV library for specific hardware platforms, among other things.