|
| 1 | +--- |
| 2 | +title: Build OpenCV Applications with MSVC |
| 3 | +weight: 3 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Build OpenCV with MSVC |
| 10 | + |
| 11 | +### Clone OpenCV repo |
| 12 | + |
| 13 | +Open up a Windows Powershell and checkout the source tree: |
| 14 | + |
| 15 | +```bash |
| 16 | +git clone https://github.com/opencv/opencv |
| 17 | +cd opencv |
| 18 | +git checkout tags/4.10.0 |
| 19 | +``` |
| 20 | + |
| 21 | +{{% notice Note %}} |
| 22 | +You might be able to use a later version. These steps have been tested with the version 4.10.0. |
| 23 | +{{% /notice %}} |
| 24 | + |
| 25 | +### Build with MSVC |
| 26 | + |
| 27 | +Here, you will use CMake from the command line. First, Run the following command to run the pre-build configuration. |
| 28 | + |
| 29 | +```bash |
| 30 | +mkdir build_msvc |
| 31 | +cd build_msvc |
| 32 | + |
| 33 | +cmake ` |
| 34 | +-S .. ` |
| 35 | +-B . ` |
| 36 | +-G "Visual Studio 17 2022" ` |
| 37 | +-DCMAKE_BUILD_TYPE=Release ` |
| 38 | +-DBUILD_opencv_world=ON ` |
| 39 | +-DWITH_ITT=OFF ` |
| 40 | +-DWITH_OPENCL=OFF ` |
| 41 | +-DWITH_OPENCLAMDBLAS=OFF ` |
| 42 | +-DWITH_OPENCLAMDFFT=OFF ` |
| 43 | +-DWITH_OPENCL_D3D11_NV=OFF ` |
| 44 | +-DWITH_DIRECTML=OFF ` |
| 45 | +-DWITH_DIRECTX=OFF ` |
| 46 | +-DWITH_ADE=OFF ` |
| 47 | +-DWITH_CAROTENE=OFF |
| 48 | +``` |
| 49 | + |
| 50 | +The given options specify the following: |
| 51 | +- The source code is located one level above the current directory. |
| 52 | +- The build will be performed in the current directory. |
| 53 | +- The Visual Studio 2022 MSVC compiler will be used as the compiler. |
| 54 | +- The built library is generated as a single file that includes all of OpenCV's functionality. |
| 55 | +- Disable unnecessary options, assuming processing on Arm CPU.Unnecessary options have been disabled to execute processing on Arm CPUs. |
| 56 | + |
| 57 | + |
| 58 | +If the configuration is successful, a message similar to the following should be displayed at the end of the execution: |
| 59 | + |
| 60 | +``` |
| 61 | +-- General configuration for OpenCV 4.10.0 ===================================== |
| 62 | +-- Version control: 4.10.0 |
| 63 | +-- |
| 64 | +-- Platform: |
| 65 | +-- Timestamp: 2024-11-06T17:47:31Z |
| 66 | +-- Host: Windows 10.0.22631 ARM64 |
| 67 | +-- CMake: 3.28.1 |
| 68 | +-- CMake generator: Visual Studio 17 2022 |
| 69 | +-- CMake build tool: C:/Program Files/Microsoft Visual Studio/2022/Professional/MSBuild/Current/Bin/arm64/MSBuild.exe |
| 70 | +-- MSVC: 1941 |
| 71 | +-- Configuration: Release |
| 72 | +-- |
| 73 | +-- CPU/HW features: |
| 74 | +-- Baseline: NEON |
| 75 | +-- requested: NEON FP16 |
| 76 | +-- Dispatched code generation: NEON_DOTPROD NEON_FP16 |
| 77 | +-- requested: NEON_FP16 NEON_BF16 NEON_DOTPROD |
| 78 | +-- NEON_DOTPROD (1 files): + NEON_DOTPROD |
| 79 | +-- NEON_FP16 (2 files): + NEON_FP16 |
| 80 | +-- |
| 81 | +-- C/C++: |
| 82 | +-- Built as dynamic libs?: YES |
| 83 | +-- C++ standard: 11 |
| 84 | +-- C++ Compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.41.34120/bin/Hostarm64/arm64/cl.exe (ver 19.41.34123.0) |
| 85 | +[...] |
| 86 | +-- C Compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.41.34120/bin/Hostarm64/arm64/cl.exe |
| 87 | +[...] |
| 88 | +-- Install to: C:/Users/kokmit01/work/opencv/build_msvc/install |
| 89 | +-- ----------------------------------------------------------------- |
| 90 | +-- |
| 91 | +-- Configuring done (93.6s) |
| 92 | +-- Generating done (2.8s) |
| 93 | +-- Build files have been written to: C:/Users/kokmit01/work/opencv/build_msvc |
| 94 | +``` |
| 95 | + |
| 96 | +Now run the following command to build and install: |
| 97 | + |
| 98 | +```bash |
| 99 | +cmake --build . --config Release |
| 100 | +cmake --build . --target INSTALL --config Release |
| 101 | +``` |
| 102 | + |
| 103 | +{{% notice Note %}} |
| 104 | +The build takes approximately 20 mins on Lenovo X13s |
| 105 | +{{% /notice %}} |
| 106 | + |
| 107 | +When the build and the install is complete, confirm the shared library have been created: |
| 108 | + |
| 109 | +```bash { output_lines = "2-12,14-22" } |
| 110 | +ls ./install/x64/vc17/bin |
| 111 | + Directory: C:\Users\kokmit01\work\opencv\build_msvc\install\x64\vc17\bin |
| 112 | +Mode LastWriteTime Length Name |
| 113 | +---- ------------- ------ ---- |
| 114 | +-a---- 07/11/2024 21:56 43008 opencv_annotation.exe |
| 115 | +-a---- 07/11/2024 21:56 143872 opencv_interactive-calibration.exe |
| 116 | +-a---- 07/11/2024 21:56 41984 opencv_model_diagnostics.exe |
| 117 | +-a---- 07/11/2024 22:03 36864 opencv_version.exe |
| 118 | +-a---- 07/11/2024 22:03 35328 opencv_version_win32.exe |
| 119 | +-a---- 07/11/2024 16:11 26391552 opencv_videoio_ffmpeg4100_64.dll |
| 120 | +-a---- 07/11/2024 22:03 56320 opencv_visualisation.exe |
| 121 | +-a---- 07/11/2024 21:56 27175936 opencv_world4100.dll |
| 122 | +ls ./install/x64/vc17/lib |
| 123 | + Directory: C:\Users\kokmit01\work\opencv\build_msvc\install\x64\vc17\lib |
| 124 | +Mode LastWriteTime Length Name |
| 125 | +---- ------------- ------ ---- |
| 126 | +-a---- 07/11/2024 16:11 434 OpenCVConfig-version.cmake |
| 127 | +-a---- 07/11/2024 16:11 15260 OpenCVConfig.cmake |
| 128 | +-a---- 07/11/2024 16:11 972 OpenCVModules-release.cmake |
| 129 | +-a---- 07/11/2024 16:11 3879 OpenCVModules.cmake |
| 130 | +-a---- 07/11/2024 21:56 2849862 opencv_world4100.lib |
| 131 | +``` |
| 132 | + |
| 133 | +{{% notice Note %}} |
| 134 | +The directory name in the middle is "x64," but there is no need to worry as the generated libraries and executable files will definitely run as ARM64. |
| 135 | +{{% /notice %}} |
| 136 | + |
| 137 | +```bash { output_lines = "2" } |
| 138 | +./install/x64/vc17/bin/pencv_version.exe |
| 139 | +4.10.0 |
| 140 | +``` |
| 141 | + |
| 142 | +## Build OpenCV Applications |
| 143 | + |
| 144 | + |
| 145 | +Add C:\Users\kokmit01\work\opencv\build\install\x64\vc17\bin to PATH environment variable (for access to dll) |
| 146 | +put the dll to the directory with exe file if it doesn't work. |
| 147 | + |
| 148 | +Set include dir/linker path |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +```cpp |
| 153 | +#include <opencv2/opencv.hpp> |
| 154 | +#include <iostream> |
| 155 | +int main() { |
| 156 | + cv::Mat image = cv::Mat::zeros(100, 100, CV_8UC3); |
| 157 | + if (image.empty()) { |
| 158 | + std::cout << "Failed to create an image!" << std::endl; |
| 159 | + return -1; |
| 160 | + } |
| 161 | + cv::circle(image, cv::Point(50, 50), 30, cv::Scalar(255, 0, 0), -1); |
| 162 | + cv::imwrite("test_image.png", image); |
| 163 | + cv::waitKey(0); |
| 164 | + return 0; |
| 165 | +} |
| 166 | +``` |
0 commit comments