Skip to content

Commit

Permalink
Merge pull request #1398 from jasonrandrews/review
Browse files Browse the repository at this point in the history
First review of OpenCV on Windows Learning Path
  • Loading branch information
jasonrandrews authored Nov 25, 2024
2 parents 1ecaa89 + 3239630 commit 7f15c41
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ weight: 2
layout: learningpathall
---

## OpenCV
## What is OpenCV?

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.

Here’s what developers should know:
Expand All @@ -24,19 +25,19 @@ Here’s what developers should know:
* __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.


## Compilers for Windows on Arm Development
## Which compilers are available for Windows on Arm Development?

When building applications for Windows on Arm, both MSVC (Microsoft Visual C++) and Clang are options for developers, each with its own advantages.
MSVC (Microsoft Visual C++) and Clang are options for developers building Windows on Arm applications.

* __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.

* __Clang__: An open-source compiler that is part of the LLVM project. It’s known for its modern design and cross-platform capabilities.

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.
MSVC is ideal for Windows-focused projects needing seamless integration with Visual Studio. Clang is ideal for cross-platform projects or when using modern C++ features.

## Before you begin

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.
Any Windows on Arm machine which has the required tools installed can be used for this Learning Path. You will learn how to build OpenCV using both MSVC and Clang.

Please install the following tools required for both methods.

Expand All @@ -52,7 +53,7 @@ The instructions were tested with the version 3.28.1
There is currently no Arm version of Git. Install the 64-bit x86 version.
{{% /notice %}}

Follow the link to intall the required tools for a method using MSVC.
Follow the link to install the required tools for a method using MSVC.

* [Visual Studio 2022 or higher](/install-guides/vs-woa).

Expand All @@ -62,7 +63,7 @@ The instructions were tested with Visual Studio 2022.

To build using Clang, please install the following.

* [LLVM](install-guides/llvm-woa/)
* [LLVM](/install-guides/llvm-woa/)

{{% notice Note %}}
The instructions were tested with the version 18.1.8.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ weight: 3
layout: learningpathall
---

## Build OpenCV with MSVC
## How do I build OpenCV with MSVC?

### Clone OpenCV repo
### Clone the OpenCV repository

Open up a Windows Powershell and checkout the source tree:
Open up a Windows PowerShell and checkout the source tree:

```bash
git clone https://github.com/opencv/opencv
Expand All @@ -19,12 +19,14 @@ git checkout tags/4.10.0
```

{{% notice Note %}}
You might be able to use a later version. These steps have been tested with the version 4.10.0.
You might be able to use a later version, but these steps have been tested with the version 4.10.0.
{{% /notice %}}

### Pre-build configuration

Here, you will use CMake from the command line. First, run the following command to run the pre-build configuration.
You can use CMake from the command line.

First, run the following command to run the pre-build configuration.

```bash
mkdir build_msvc
Expand All @@ -51,14 +53,14 @@ The given options specify the following:
- The source code is located one level above the current directory.
- The build will be performed in the current directory.
- The Visual Studio 2022 MSVC compiler will be used as the compiler.
- The built library is generated as a single file that includes all of OpenCV's functionality.
- The compiled library is generated as a single file that includes all of OpenCV's functionality.
- Unnecessary options have been disabled, assuming processing on Arm CPUs.

 

If the configuration is successful, a message similar to the following should be displayed at the end of the execution:

```
```output
-- General configuration for OpenCV 4.10.0 =====================================
-- Version control: 4.10.0
--
Expand Down Expand Up @@ -96,20 +98,20 @@ If the configuration is successful, a message similar to the following should be

### Build and install

Now run the following command to build and install:
Run the following commands to build and install OpenCV:

```bash
cmake --build . --config Release
cmake --build . --target INSTALL --config Release
```

{{% notice Note %}}
The build takes approximately 25 mins on Lenovo X13s
The build takes approximately 25 mins on a Lenovo X13s
{{% /notice %}}

 

When the build and the install is complete, confirm the shared library have been created:
When the build and the install steps are complete, confirm the shared library has been created by inspecting the results in the `install/bin` directory:

```bash { output_lines = "2-11" }
ls ./install/x64/vc17/bin
Expand All @@ -126,6 +128,8 @@ Mode LastWriteTime Length Name
-a---- 08/11/2024 09:03 27179008 opencv_world4100.dll
```

Also inspect the `install/lib` directory:

```bash { output_lines = "2-9" }
ls ./install/x64/vc17/lib
Directory: C:\Users\username\work\opencv\build_msvc\install\x64\vc17\lib
Expand All @@ -140,7 +144,9 @@ Mode LastWriteTime Length Name

 

`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.
The library used in your application is `opencv_world<version>.lib/dll`.

Once the library files are correctly generated, run the following command to ensure there are no errors.

```bash { output_lines = "2" }
./install/x64/vc17/bin/opencv_version.exe
Expand All @@ -155,23 +161,31 @@ The genereated directory name contains "x64," but there is no need to worry as t

## Build OpenCV Applications

Once the OpenCV library has been successfully built, the next step is to link it to a simple application and try using it.
Once the OpenCV library has been successfully created, the next step is to link it to a simple application and try using it.

### Create a new project in Visual Studio

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.
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`.

![MSVC project](msvc_project.png "Create a new project")
On the next screen, set the `Project name` and `Location`. You can choose any name and location, but for this example, name the project `TestOpenCV`, as shown below.

### Adding a source code
Click `Create` to generate the new project.

![MSVC project #center](msvc_project.png "Create a new project")

### Add source code

In `Solution Explorer`, right-click the `Source Files` folder, select `Add`, and then `New Item...`. Create a file named `test_opencv.cpp`.

![MSVC add file](msvc_add_file.png "Add a source file")
![MSVC add file #center](msvc_add_file.png "Add a source file")

&nbsp;

Once the file is created, it will open in the editor. Copy and paste the following program into it and save the file.
Once the file is created, it will open in the editor.

Copy and paste the following program into it and save the file.

```cpp
#include <opencv2/opencv.hpp>
Expand All @@ -189,41 +203,45 @@ int main() {
}
```

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.
This program is a simple example that uses OpenCV to create a 100x100 black image, draw a blue circle on it, and save it as a file.

### Configure build settings

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.

![MSVC screenshot](msvc_screen.png "MSVC screenshot")
![MSVC screenshot #center](msvc_screen.png "MSVC screenshot")

&nbsp;

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.

![MSVC include dir](msvc_include_dir.png "Include directories: Specify the directory containing the OpenCV header files.")
![MSVC include dir #center](msvc_include_dir.png "Include directories: Specify the directory containing the OpenCV header files.")

&nbsp;

![MSVC link dir](msvc_link_dir.png "Library directories: Specify the directory where the libraries for linking are located.")
![MSVC link dir #center](msvc_link_dir.png "Library directories: Specify the directory where the libraries for linking are located.")

&nbsp;

![MSVC link lib](msvc_link_lib.png "Additional dependencies: Specify the names of the libraries to link")
![MSVC link lib #center](msvc_link_lib.png "Additional dependencies: Specify the names of the libraries to link")

&nbsp;

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.

![path setting](set_path.png "Set the DLL dir to the PATH environment variable")
![path setting #center](set_path.png "Set the DLL dir to the PATH environment variable")

### Build the application

You are now ready to build the application.

### Run the build
From the top menu, select `Debug` and click `Start Without Debugging` or press `Ctrl` + `F5`.

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`.
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.

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.
Open the image file, it should look like the example shown below.

![test_image pic](test_image.png "test_image.png")

Congratulations! You are now ready to create your own OpenCV applications.
Congratulations! You are now ready to create your own OpenCV applications using MSVC.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ weight: 4
layout: learningpathall
---

## Build OpenCV with Clang
## How do I build OpenCV with Clang?

### Clone OpenCV repo
### Clone the OpenCV repository

Open up a Windows Powershell and checkout the source tree:

Expand All @@ -19,12 +19,14 @@ git checkout tags/4.10.0
```

{{% notice Note %}}
You might be able to use a later version. These steps have been tested with the version 4.10.0.
You might be able to use a later version, but these steps have been tested with the version 4.10.0.
{{% /notice %}}

### Pre-build configuration

Here, you will use CMake from the command line. First, run the following command to run the pre-build configuration.
You can use CMake from the command line.

First, run the following command to run the pre-build configuration.

```bash
mkdir build_clang
Expand Down Expand Up @@ -53,7 +55,7 @@ The given options specify the following:
- The source code is located one level above the current directory.
- The build will be performed in the current directory.
- The Clang compiler will be used as the compiler.
- The built library is generated as a single file that includes all of OpenCV's functionality.
- The compiled library is generated as a single file that includes all of OpenCV's functionality.
- Unnecessary options have been disabled, assuming processing on Arm CPUs.

&nbsp;
Expand Down Expand Up @@ -98,20 +100,20 @@ If the configuration is successful, a message similar to the following should be

### Build and install

Now run the following command to build and install:
Run the following commands to build and install OpenCV:

```bash
ninja
ninja install
```

{{% notice Note %}}
The build takes approximately 25 mins on Lenovo X13s
The build takes approximately 25 mins on a Lenovo X13s
{{% /notice %}}

&nbsp;

When the build and the install is complete, confirm the shared library have been created:
When the build and the install steps are complete, confirm the shared library has been created by inspecting the results in the `install/bin` directory:

```bash { output_lines = "2-11" }
ls ./install/bin
Expand All @@ -127,6 +129,8 @@ Mode LastWriteTime Length Name
-a---- 08/11/2024 09:50 20207104 opencv_world4100.dll
```

Also inspect the `install/lib` directory:

```bash { output_lines = "2-9" }
ls ./install/lib
Directory: C:\Users\username\work\opencv\build_clang\install\lib
Expand All @@ -141,7 +145,9 @@ Mode LastWriteTime Length Name

&nbsp;

`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.
The library used in your application is `opencv_world<version>.lib/dll`.

Once the library files are correctly generated, run the following command to ensure there are no errors.

```bash { output_lines = "2" }
./install/bin/opencv_version.exe
Expand All @@ -152,11 +158,11 @@ Mode LastWriteTime Length Name

## Build OpenCV Applications

Once the OpenCV library has been successfully built, the next step is to link it to a simple application and try using it.
Once the OpenCV library has been successfully created, you can create a simple application and try using it.

### Prepare a application program

First, save the following C++ program as `test_opencv.cpp` in an appropriate location. Here, it is assumed that the file is saved under the `build_clang` directory.
First, use a text editor to save the following C++ program as `test_opencv.cpp` in the `build_clang` directory.

```cpp
#include <opencv2/opencv.hpp>
Expand All @@ -174,11 +180,12 @@ int main() {
}
```

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.
This program is a simple example that uses OpenCV to create a 100x100 black image, draw a blue circle on it, and save it as a file.

### Compile the program

Now, let's compile this code using the command below.
Compile the code using the command below:

```bash
clang++ .\test_opencv.cpp -o test_opencv.exe -I.\install\include -L.\install\lib -lopencv_world4100
```
Expand All @@ -191,7 +198,7 @@ The given options specify the following:

### Run the program

Now, let's run it. To run this executable, you need to ensure that the directory containing the dynamic libraries (DLLs) is added to the `PATH` environment variable, or place the DLLs in the same location as the executable.
To run the executable, you need to ensure that the directory containing the dynamic libraries (DLLs) is added to the `PATH` environment variable, or place the DLLs in the same location as the executable.

```bash
.\test_opencv.exe
Expand All @@ -205,9 +212,10 @@ $env:PATH = "./install/bin;" + $env:PATH
```
{{% /notice %}}

When you execute the command, it should finish quickly, and `test_image.png` should be generated. If the image file looks like the one shown below, it means you've succeeded.
When you execute the command, it will finish quickly, and `test_image.png` is generated.

![test_image pic](test_image.png "test_image.png")
Open the image file, it should look like the example shown below.

![test_image pic](test_image.png "test_image.png")

Congratulations! You are now ready to create your own OpenCV applications.
Congratulations! You are now ready to create your own OpenCV applications using Clang.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ minutes_to_complete: 90
who_is_this_for: This is an advanced topic for software developers who want to use OpenCV with Windows on Arm devices.

learning_objectives:
- Build OpenCV library with Windows on Arm devices.
- Develop applications with OpenCV.
- Build the OpenCV library for Windows on Arm devices.
- Develop applications using OpenCV.

prerequisites:
- A Windows on Arm computer such as Lenovo Thinkpad X13s or a [virtual machine](/learning-paths/cross-platform/woa_azure/).
- A Windows on Arm computer such as Lenovo Thinkpad X13s or an [Azure virtual machine](/learning-paths/cross-platform/woa_azure/).

author_primary: Koki Mitsunami

Expand Down
Loading

0 comments on commit 7f15c41

Please sign in to comment.