# OpenCV Example

## OpenCV Overview

`OpenCV (Open Source Computer Vision Library)` is a cross-platform computer-vision library originally initiated by Intel and released under the `BSD` license. It can be used freely in both commercial and research scenarios. OpenCV is widely used to build real-time image processing, computer vision, and pattern-recognition applications.

`K230 RTOS SDK` provides an optimized OpenCV acceleration library for K230. Compared with the original OpenCV version, it significantly reduces the runtime of many OpenCV operators. For example:

| Operator | K230 + original OpenCV | K230 + optimized OpenCV |
| --- | --- | --- |
| integral image | `34.5 ms` | `7.7 ms` |
| warp affine | `391.1 ms` | `34.7 ms` |

Notes:

- The above timings were measured on the K230 big core at `1.6 GHz`.
- For the integral-image test, the input image is a `1280 x 1080` grayscale image and the integral image uses `32-bit float`.
- For the warp-affine test, the input image is a `1280 x 1080` grayscale image, rotated `15` degrees clockwise and scaled by `0.6`, with a `1280 x 1080` grayscale destination image.

The SDK already includes a prebuilt optimized static OpenCV library under:

```bash
src/rtsmart/libs/opencv/
```

Users can directly link against this library when building their own applications.

## Build Examples

This section explains how to build executables with the OpenCV static library already provided in the SDK. The SDK provides five example programs under:

```bash
src/rtsmart/examples/3rd-party/opencv
```

### Code Structure

Representative directory structure:

```text
opencv_calculate_hist
opencv_detect_features2d
opencv_find_contours
opencv_grayscale_binarize
opencv_obj_detect
CMakeLists.txt
build_app.sh
cmake/
utils/
```

The `utils` directory contains all required input images and related data.

### Firmware Build

If you want the examples to be built into the firmware, run `make menuconfig` from the SDK root and enable:

```text
RT-Smart UserSpace Examples Configuration
-> Enable build 3rd-party examples
-> Enable Build OpenCV Sample Programs
```

The built examples are placed under `sdcard/app/examples/3rd-party/opencv`.

### Example Build

If you only want to build the OpenCV examples, enter:

```bash
src/rtsmart/examples/3rd-party/opencv
```

and run:

```bash
./build_app.sh
```

After a successful build, the `k230_bin` directory contains the compiled `elf` files and the required test files.

## Run Examples

> **Note**
>
> All required input images are provided under:
>
> ```bash
> src/rtsmart/examples/3rd-party/opencv/utils
> ```

### `opencv_calculate_hist`

This sample reads an image and calculates the histogram of each color channel (`B`, `G`, `R`).

```bash
./opencv_calculate_hist.elf
```

### `opencv_detect_features2d`

This sample reads an image and uses the FAST detector to detect image feature points.

```bash
./opencv_detect_features2d.elf
```

### `opencv_find_contours`

This sample reads an image, detects contours, and draws the contour result into a new image.

```bash
./opencv_find_contours.elf
```

### `opencv_grayscale_binarize`

This sample reads a color image, converts it to grayscale, then performs binarization and saves the processed result.

```bash
./opencv_grayscale_binarize.elf
```

### `opencv_obj_detect`

This sample uses cascade classifiers to detect faces and eyes, then draws the detection result on the original image.

```bash
./opencv_obj_detect.elf
```
