Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
---
title: Accelerate OpenCV-based Android Applications with KleidiCV
title: Accelerate an OpenCV-based Android Application with KleidiCV

minutes_to_complete: 45

who_is_this_for: This is an introductory topic for developers who are interested in creating Computer Vision Applications with OpenCV and KleidiCV on Android Devices.
who_is_this_for: This is an introductory topic for developers who are interested in creating Computer Vision applications with OpenCV and KleidiCV on Android Devices.

learning_objectives:
- Describe what KleidiCV is, and what it can offer.
- Create and configure a project to add OpenCV support.
- Process images using various OpenCV functions.
- Process images using OpenCV functionality.

prerequisites:
- A development machine with [Android Studio](https://developer.android.com/studio) installed.
- Familiarity with Android development concepts.
- An Android smartphone.

author: Dawid Borycki
Expand All @@ -22,7 +23,7 @@ subjects: Graphics
armips:
- Cortex-A
operatingsystems:
- Windows
- Android
tools_software_languages:
- Android
- Android Studio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ weight: 2
layout: "learningpathall"
---

## OpenCV Overview with HAL and KleidiCV
## What is OpenCV in Computer Vision?
Open Source Computer Vision Library (OpenCV) is a framework for real-time computer vision, that you can use across different platforms, including mobile devices. Modern smartphones equipped with advanced cameras and powerful processors can efficiently handle complex computer vision tasks, and OpenCV is a go-to choice for developers. Its cross-platform compatibility allows the creation of versatile applications that work across multiple devices without extensive code modifications.

### OpenCV in Computer Vision
OpenCV (Open Source Computer Vision Library) is a robust framework for real-time computer vision, widely used across platforms, including mobile devices. Modern smartphones equipped with advanced cameras and powerful processors can efficiently handle complex computer vision tasks, making OpenCV a go-to choice for developers. Its cross-platform compatibility allows the creation of versatile applications that work seamlessly across multiple devices without extensive code modifications.
## What does OpenCV offer Android Developers?
For Android developers, OpenCV offers an SDK designed for integration with Android Studio, the primary development environment. The SDK simplifies the process of adding OpenCV libraries, managing dependencies, and configuring projects, enabling developers to easily incorporate vision capabilities into their applications.

### Integration with Android and Kotlin
OpenCV integrates well with Android development, leveraging Kotlin’s concise syntax and Java interoperability to simplify implementation. Developers can use OpenCV’s diverse set of functionalities, such as image and video capture, filtering, transformations, feature detection, object recognition, and machine learning integration, to build efficient and maintainable applications.
## How does OpenCV Integrate with Android and Kotlin?
OpenCV integrates with Android development, leveraging Kotlin’s concise syntax and Java's interoperability, to simplify implementation. You can use OpenCV’s diverse set of functionalities, such as image and video capture, filtering, transformations, feature detection, object recognition, and machine learning integration, to build efficient and maintainable applications.

### Performance Optimization and HAL
OpenCV is optimized for high performance, utilizing native C++ code for efficient processing. This ensures real-time performance, particularly on mobile devices where computational resources may be limited. A critical component enabling these optimizations is the Hardware Acceleration Layer (HAL). HAL serves as an abstraction layer that enables hardware-specific acceleration by utilizing device-specific optimizations. This significantly boosts the performance of OpenCV functions on supported hardware, reducing processing time and power consumption. HAL makes OpenCV highly adaptable to modern multi-core processors and GPU architectures, vital for computationally intensive tasks like object detection and image recognition.
## How is Performance Optimized with HAL?
OpenCV is optimized for performance, utilizing native C++ code for efficient processing. This ensures optimized real-time performance, particularly on mobile devices where computational resources might be limited.

### ARM and KleidiCV
To further enhance OpenCV’s capabilities on ARM-based devices, ARM developed KleidiCV, a specialized library that leverages OpenCV’s HAL for hardware acceleration. KleidiCV is an Arm Kleidi Library, a suite of highly performant open-source Arm routines. KleidiCV focuses on optimizing various OpenCV functions specifically for ARM processors, ensuring faster execution and lower power consumption. This integration is particularly beneficial for mobile and embedded systems where performance efficiency is critical. By utilizing HAL, KleidiCV allows developers to harness the full potential of ARM hardware, making it an ideal solution for applications requiring high-performance computer vision on ARM-based devices.
A critical component in enabling these optimizations is the Hardware Acceleration Layer (HAL). HAL serves as an abstraction layer that enables hardware-specific acceleration by utilizing device-specific optimizations. This significantly boosts the performance of OpenCV functions on supported hardware, which reduces processing time and power consumption. HAL makes OpenCV adaptable to modern multi-core processors and GPU architectures, which are essential for computationally-intensive tasks, such as object detection and image recognition.

### OpenCV for Android Developers
For Android developers, OpenCV offers an SDK designed for seamless integration with Android Studio, the primary development environment. The SDK simplifies the process of adding OpenCV libraries, managing dependencies, and configuring projects, enabling developers to incorporate sophisticated vision capabilities into their applications effortlessly.
## What is KleidiCV?
KleidiCV is an Arm Kleidi Library, a suite of highly performant open-source Arm routines that leverages OpenCV’s HAL for hardware acceleration. KleidiCV focuses on optimizing various OpenCV functions specifically for Arm processors, ensuring faster execution and lower power consumption.

By combining OpenCV’s rich feature set, hardware optimization via HAL, and specialized enhancements like ARM’s KleidiCV, developers can build efficient, high-performing computer vision solutions tailored for modern mobile and embedded systems.
This integration is particularly beneficial for mobile and embedded systems where performance efficiency is critical. By utilizing HAL, KleidiCV allows developers to harness the full potential of Arm hardware, making it an ideal solution for applications requiring high-performance computer vision on Arm-based devices.

In this Learning Path we will demonstrate how to use KleidiCV-accelerated OpenCV in Android application.
## A Performant Solution
By combining OpenCV’s feature set, the hardware optimization from HAL, and specialized enhancements such as Arm’s KleidiCV, developers can build efficient, high-performing computer vision solutions tailored for modern mobile and embedded systems.

In this Learning Path, you will learn how you can use KleidiCV-accelerated OpenCV in an Android application.

You can find all the code used in this Learning Path in a [GitHub repository](https://github.com/dawidborycki/Arm64.KleidiCV.Demo.git).
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@ weight: 3
layout: "learningpathall"
---
## Create a project
You will need a development computer with [Android Studio](https://developer.android.com/studio) installed (this examples uses Android Studio Ladybug | 2024.2.1 Patch 3)
You will need a development computer with [Android Studio](https://developer.android.com/studio) installed. This example uses Android Studio Ladybug 2024.2.1, Patch 3.

Follow these steps to create a project and add OpenCV with KleidiCV support:
You can create a project and add OpenCV with KleidiCV support by following the steps outlined in this section.

1. Open Android Studio on your development machine and then click the **+ New Project** icon:
2. In the New Project window, select **Empty Views Activity**:
Start by creating a new project, and configuring it:

![img1](Figures/01.png)
1. Open Android Studio on your development machine, and click the **+ New Project** icon.
2. In the **New Project** window, select **Empty Views Activity**:

3. Configure the project as follows (see figure below):
![img1 alt-text#center](Figures/01.png "Figure 1: Creating a new project.")

3. Figure 2 shows you how to configure the project:
- Name: **Arm64.KleidiCV.Demo**.
- Package name: **com.arm.arm64kleidicvdemo**.
- Save location: *Select relevant file location*.
- Save location: *Select relevant file location for your setup*.
- Language: **Kotlin**.
- Minimum SDK: **API 24**.
- Build configuration language: **Kotlin DSL**.

![img2](Figures/02.png)
![img2 alt-text#center](Figures/02.png "Figure 2: Configuring your new project.")

4. Click the **Finish** button.

The project will be ready in a few moments. Afterward you can configure the project.
Now wait a few moments until your project is ready. You will be able to configure the project further later on.

## Add OpenCV support
To add OpenCV for Arm64, open the *build.gradle.ts (Module: app)*, and add the following line under the dependencies:
To add OpenCV support for Arm64 (AArch64), open the *build.gradle.ts (Module: app)*, and add the following line under the dependencies:

```JSON
implementation("org.opencv:opencv:4.11.0")
```

Also, make sure that compileSdk is set to 35. The contents of the file should look something like this:
Make sure that compileSdk is set to 35. The contents of the file should look like this:

```JSON
plugins {
Expand Down Expand Up @@ -90,6 +92,8 @@ dependencies {
}
```

Then, click the **Sync Now** link in the top pane that appears. From here on, you can use OpenCV with KleidiCV support in your application.
Now click the **Sync Now** link in the top pane that appears.

You can now use OpenCV with KleidiCV support in your application.

Save the file. In the next step, you will define the application UI.
Loading