Skip to content

Commit 1ecaa89

Browse files
Merge pull request #1380 from mitsunami/koki/win-opencv
New Learning Path: Build OpenCV applications on Windows on Arm
2 parents 7dcde9a + 7483342 commit 1ecaa89

File tree

15 files changed

+627
-1
lines changed

15 files changed

+627
-1
lines changed

assets/contributors.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Rin Dobrescu,Arm,,,,
4343
Przemyslaw Wirkus,Arm,PrzemekWirkus,przemyslaw-wirkus-78b73352,,
4444
Nader Zouaoui,Day Devs,nader-zouaoui,nader-zouaoui,@zouaoui_nader,https://daydevs.com/
4545
Alaaeddine Chakroun,Day Devs,Alaaeddine-Chakroun,alaaeddine-chakroun,,https://daydevs.com/
46-
Koki Mitsunami,Arm,,,,
46+
Koki Mitsunami,Arm,,kmitsunami,,
4747
Chen Zhang,Zilliz,,,,
4848
Tianyu Li,Arm,,,,
4949
Georgios Mermigkis,VectorCamp,gMerm,georgios-mermigkis,,https://vectorcamp.gr/
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: OpenCV and Compilers for Windows on Arm
3+
weight: 2
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## OpenCV
10+
OpenCV (Open Source Computer Vision Library) is a popular, open-source library that developers use to build computer vision applications. It provides a set of tools and functions that help you handle tasks related to images and videos without needing to write everything from scratch.
11+
12+
Here’s what developers should know:
13+
14+
* __Ease of Use__: OpenCV comes with pre-built functions for common tasks like reading, displaying, and processing images and videos. This saves time compared to writing algorithms from the ground up.
15+
16+
* __Image Processing__: You can perform operations like changing colors, applying filters, resizing, rotating, and other transformations to images with minimal code.
17+
18+
* __Video Handling__: Developers can use OpenCV to capture, modify, and analyze video frames, making it ideal for creating applications like video surveillance or video editing tools.
19+
20+
* __Computer Vision Algorithms__: OpenCV includes built-in algorithms for complex tasks like object detection (e.g., face and eye recognition), edge detection, and image segmentation.
21+
22+
* __Machine Learning__: It includes modules for training models using basic machine learning algorithms, which can be applied for pattern recognition and data analysis in visual data.
23+
24+
* __Community and Resources__: Being open-source and widely adopted, there is a large community of developers contributing to and supporting OpenCV. This makes it easier to find tutorials, documentation, and answers to questions.
25+
26+
27+
## Compilers for Windows on Arm Development
28+
29+
When building applications for Windows on Arm, both MSVC (Microsoft Visual C++) and Clang are options for developers, each with its own advantages.
30+
31+
* __MSVC__: A compiler developed by Microsoft that’s part of the Visual Studio IDE. It’s designed specifically for Windows and integrates well with the Windows development ecosystem.
32+
33+
* __Clang__: An open-source compiler that is part of the LLVM project. It’s known for its modern design and cross-platform capabilities.
34+
35+
MSVC is the go-to for Windows-focused projects needing seamless integration with Visual Studio. Clang is ideal for cross-platform projects or when using modern C++ features with flexibility.
36+
37+
## Before you begin
38+
39+
Any Windows on Arm machine which has the required tools installed can be used for this Learning Path. You will learn the build methods using both MSVC and Clang.
40+
41+
Please install the following tools required for both methods.
42+
43+
* [CMake](/install-guides/cmake)
44+
45+
{{% notice Note %}}
46+
The instructions were tested with the version 3.28.1
47+
{{% /notice %}}
48+
49+
* [Git](https://git-scm.com/downloads/win)
50+
51+
{{% notice Note %}}
52+
There is currently no Arm version of Git. Install the 64-bit x86 version.
53+
{{% /notice %}}
54+
55+
Follow the link to intall the required tools for a method using MSVC.
56+
57+
* [Visual Studio 2022 or higher](/install-guides/vs-woa).
58+
59+
{{% notice Note %}}
60+
The instructions were tested with Visual Studio 2022.
61+
{{% /notice %}}
62+
63+
To build using Clang, please install the following.
64+
65+
* [LLVM](install-guides/llvm-woa/)
66+
67+
{{% notice Note %}}
68+
The instructions were tested with the version 18.1.8.
69+
{{% /notice %}}
70+
71+
* [Ninja]( https://github.com/ninja-build/ninja/releases)
72+
73+
{{% notice Note %}}
74+
The instructions were tested with version 1.11.1
75+
{{% /notice %}}
76+
77+
You use the LLVM Clang and the Ninja generator to build. Set PATH to the paths to your LLVM and Ninja install.
78+
79+
You now have the required development tools installed. Please proceed to the page for the compiler you want to build with.
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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+
&nbsp;
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+
![MSVC project](msvc_project.png "Create a new project")
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+
![MSVC add file](msvc_add_file.png "Add a source file")
171+
172+
&nbsp;
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+
![MSVC screenshot](msvc_screen.png "MSVC screenshot")
199+
200+
&nbsp;
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+
![MSVC include dir](msvc_include_dir.png "Include directories: Specify the directory containing the OpenCV header files.")
205+
206+
&nbsp;
207+
208+
![MSVC link dir](msvc_link_dir.png "Library directories: Specify the directory where the libraries for linking are located.")
209+
210+
&nbsp;
211+
212+
![MSVC link lib](msvc_link_lib.png "Additional dependencies: Specify the names of the libraries to link")
213+
214+
&nbsp;
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+
![path setting](set_path.png "Set the DLL dir to the PATH environment variable")
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+
![test_image pic](test_image.png "test_image.png")
227+
228+
Congratulations! You are now ready to create your own OpenCV applications.
229+

0 commit comments

Comments
 (0)