|
| 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 | +### Pre-build configuration |
| 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 | +- Unnecessary options have been disabled, assuming processing on Arm CPUs. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +If the configuration is successful, a message similar to the following should be displayed at the end of the execution: |
| 60 | + |
| 61 | +``` |
| 62 | +-- General configuration for OpenCV 4.10.0 ===================================== |
| 63 | +-- Version control: 4.10.0 |
| 64 | +-- |
| 65 | +-- Platform: |
| 66 | +-- Timestamp: 2024-11-08T08:50:24Z |
| 67 | +-- Host: Windows 10.0.22631 ARM64 |
| 68 | +-- CMake: 3.28.1 |
| 69 | +-- CMake generator: Visual Studio 17 2022 |
| 70 | +-- CMake build tool: C:/Program Files/Microsoft Visual Studio/2022/Professional/MSBuild/Current/Bin/arm64/MSBuild.exe |
| 71 | +-- MSVC: 1941 |
| 72 | +-- Configuration: Debug Release |
| 73 | +-- |
| 74 | +-- CPU/HW features: |
| 75 | +-- Baseline: NEON |
| 76 | +-- requested: NEON FP16 |
| 77 | +-- Dispatched code generation: NEON_DOTPROD NEON_FP16 |
| 78 | +-- requested: NEON_FP16 NEON_BF16 NEON_DOTPROD |
| 79 | +-- NEON_DOTPROD (1 files): + NEON_DOTPROD |
| 80 | +-- NEON_FP16 (2 files): + NEON_FP16 |
| 81 | +-- |
| 82 | +-- C/C++: |
| 83 | +-- Built as dynamic libs?: YES |
| 84 | +-- C++ standard: 11 |
| 85 | +-- 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) |
| 86 | +[...] |
| 87 | +-- C Compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.41.34120/bin/Hostarm64/arm64/cl.exe |
| 88 | +[...] |
| 89 | +-- Install to: C:/Users/username/work/opencv/build_msvc/install |
| 90 | +-- ----------------------------------------------------------------- |
| 91 | +-- |
| 92 | +-- Configuring done (97.5s) |
| 93 | +-- Generating done (2.8s) |
| 94 | +-- Build files have been written to: C:/Users/username/work/opencv/build_msvc |
| 95 | +``` |
| 96 | + |
| 97 | +### Build and install |
| 98 | + |
| 99 | +Now run the following command to build and install: |
| 100 | + |
| 101 | +```bash |
| 102 | +cmake --build . --config Release |
| 103 | +cmake --build . --target INSTALL --config Release |
| 104 | +``` |
| 105 | + |
| 106 | +{{% notice Note %}} |
| 107 | +The build takes approximately 25 mins on Lenovo X13s |
| 108 | +{{% /notice %}} |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +When the build and the install is complete, confirm the shared library have been created: |
| 113 | + |
| 114 | +```bash { output_lines = "2-11" } |
| 115 | +ls ./install/x64/vc17/bin |
| 116 | + Directory: C:\Users\username\work\opencv\build_msvc\install\x64\vc17\bin |
| 117 | +Mode LastWriteTime Length Name |
| 118 | +---- ------------- ------ ---- |
| 119 | +-a---- 08/11/2024 09:03 43008 opencv_annotation.exe |
| 120 | +-a---- 08/11/2024 09:03 143872 opencv_interactive-calibration.exe |
| 121 | +-a---- 08/11/2024 09:03 41984 opencv_model_diagnostics.exe |
| 122 | +-a---- 08/11/2024 09:12 36864 opencv_version.exe |
| 123 | +-a---- 08/11/2024 09:12 35328 opencv_version_win32.exe |
| 124 | +-a---- 08/11/2024 08:50 26391552 opencv_videoio_ffmpeg4100_64.dll |
| 125 | +-a---- 08/11/2024 09:12 56320 opencv_visualisation.exe |
| 126 | +-a---- 08/11/2024 09:03 27179008 opencv_world4100.dll |
| 127 | +``` |
| 128 | + |
| 129 | +```bash { output_lines = "2-9" } |
| 130 | +ls ./install/x64/vc17/lib |
| 131 | + Directory: C:\Users\username\work\opencv\build_msvc\install\x64\vc17\lib |
| 132 | +Mode LastWriteTime Length Name |
| 133 | +---- ------------- ------ ---- |
| 134 | +-a---- 08/11/2024 08:50 434 OpenCVConfig-version.cmake |
| 135 | +-a---- 08/11/2024 08:50 15260 OpenCVConfig.cmake |
| 136 | +-a---- 08/11/2024 08:50 972 OpenCVModules-release.cmake |
| 137 | +-a---- 08/11/2024 08:50 3879 OpenCVModules.cmake |
| 138 | +-a---- 08/11/2024 09:02 2849862 opencv_world4100.lib |
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +`opencv_world<version>.lib/dll` will be the library used by your application. Once the library files are correctly generated, run the following command to ensure there are no errors. |
| 144 | + |
| 145 | +```bash { output_lines = "2" } |
| 146 | +./install/x64/vc17/bin/opencv_version.exe |
| 147 | +4.10.0 |
| 148 | +``` |
| 149 | + |
| 150 | +{{% notice Note %}} |
| 151 | +The genereated directory name contains "x64," but there is no need to worry as the libraries and executable files will definitely run as ARM64. |
| 152 | +{{% /notice %}} |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +## Build OpenCV Applications |
| 157 | + |
| 158 | +Once the OpenCV library has been successfully built, the next step is to link it to a simple application and try using it. |
| 159 | + |
| 160 | +### Create a new project in Visual Studio |
| 161 | + |
| 162 | +First, create a new project in Visual Studio. Launch Visual Studio, click `Create a new project` on the initial screen, then select `Empty Project` and click `Next`. On the next screen, set the `Project name` and `Location`. You can choose any name and location, but for this example, we named the project `TestOpenCV`, as shown below. Then click `Create` to generate the new project. |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | +### Adding a source code |
| 167 | + |
| 168 | + In `Solution Explorer`, right-click the `Source Files` folder, select `Add`, and then `New Item...`. Create a file named `test_opencv.cpp`. |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +Once the file is created, it will open in the editor. Copy and paste the following program into it and save the file. |
| 175 | + |
| 176 | +```cpp |
| 177 | +#include <opencv2/opencv.hpp> |
| 178 | +#include <iostream> |
| 179 | +int main() { |
| 180 | + cv::Mat image = cv::Mat::zeros(100, 100, CV_8UC3); |
| 181 | + if (image.empty()) { |
| 182 | + std::cout << "Failed to create an image!" << std::endl; |
| 183 | + return -1; |
| 184 | + } |
| 185 | + cv::circle(image, cv::Point(50, 50), 30, cv::Scalar(255, 0, 0), -1); |
| 186 | + cv::imwrite("test_image.png", image); |
| 187 | + cv::waitKey(0); |
| 188 | + return 0; |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +This program is a simple example that uses OpenCV's functionality to create a 100x100 black image, draw a blue circle on it, and save it as a file. |
| 193 | + |
| 194 | +### Configure build settings |
| 195 | + |
| 196 | +Next, select the `Configuration` dropdown menu in the center of the screen and change it from `Debug` to `Release`. At this stage, your screen should look like the example shown below. |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +Now, set up the compile and link settings. Select `Project` from the top menu and click on `TestOpenCV properties`. Edit `Include directories`, `Library directories`, and `Additional dependencies` as shown in the images below, and then click OK. |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +Finally, ensure that the directory containing the dynamic libraries (DLLs) is added to the `PATH` environment variable. Set this in the Windows system settings. After setting the environment variable, restart Visual Studio to apply the changes. |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | +### Run the build |
| 221 | + |
| 222 | +Once these steps are complete, you're ready to build. From the top menu, select `Debug` and click `Start Without Debugging` or press `Ctrl` + `F5`. |
| 223 | + |
| 224 | +If a console window appears showing that the program exited with code 0 and `test_image.png` is generated in the top-level directory of your Visual Studio project, you have succeeded. When you open the image file, it should look like the example shown below. |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +Congratulations! You are now ready to create your own OpenCV applications. |
| 229 | + |
0 commit comments