Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #ifndef IMAGE_UTILS_HPP |
| 18 | #define IMAGE_UTILS_HPP |
| 19 | |
| 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | #include <forward_list> |
| 23 | #include <vector> |
| 24 | |
| 25 | /* Helper macro to convert RGB888 to RGB565 format. */ |
| 26 | #define RGB888_TO_RGB565(R8,G8,B8) ((((R8>>3) & 0x1F) << 11) | \ |
| 27 | (((G8>>2) & 0x3F) << 5) | \ |
| 28 | ((B8>>3) & 0x1F)) |
| 29 | |
| 30 | constexpr uint16_t COLOR_BLACK = 0; |
| 31 | constexpr uint16_t COLOR_GREEN = RGB888_TO_RGB565( 0, 255, 0); // 2016; |
| 32 | constexpr uint16_t COLOR_YELLOW = RGB888_TO_RGB565(255, 255, 0); // 65504; |
| 33 | |
| 34 | |
| 35 | namespace arm { |
| 36 | namespace app { |
| 37 | namespace image { |
| 38 | |
| 39 | /** |
| 40 | * Contains the x,y co-ordinates of a box centre along with the box width and height. |
| 41 | */ |
| 42 | struct Box { |
| 43 | float x; |
| 44 | float y; |
| 45 | float w; |
| 46 | float h; |
| 47 | }; |
| 48 | |
| 49 | struct Detection { |
| 50 | Box bbox; |
| 51 | std::vector<float> prob; |
| 52 | float objectness; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * @brief Calculate the 1D overlap. |
| 57 | * @param[in] x1Center First center point. |
| 58 | * @param[in] width1 First width. |
| 59 | * @param[in] x2Center Second center point. |
| 60 | * @param[in] width2 Second width. |
| 61 | * @return The overlap between the two lines. |
| 62 | **/ |
| 63 | float Calculate1DOverlap(float x1Center, float width1, float x2Center, float width2); |
| 64 | |
| 65 | /** |
| 66 | * @brief Calculate the intersection between the two given boxes. |
| 67 | * @param[in] box1 First box. |
| 68 | * @param[in] box2 Second box. |
| 69 | * @return The intersection value. |
| 70 | **/ |
| 71 | float CalculateBoxIntersect(Box& box1, Box& box2); |
| 72 | |
| 73 | /** |
| 74 | * @brief Calculate the union between the two given boxes. |
| 75 | * @param[in] box1 First box. |
| 76 | * @param[in] box2 Second box. |
| 77 | * @return The two given boxes union value. |
| 78 | **/ |
| 79 | float CalculateBoxUnion(Box& box1, Box& box2); |
| 80 | |
| 81 | /** |
| 82 | * @brief Calculate the intersection over union between the two given boxes. |
| 83 | * @param[in] box1 First box. |
| 84 | * @param[in] box2 Second box. |
| 85 | * @return The intersection over union value. |
| 86 | **/ |
| 87 | float CalculateBoxIOU(Box& box1, Box& box2); |
| 88 | |
| 89 | /** |
| 90 | * @brief Calculate the Non-Maxima suppression on the given detection boxes. |
| 91 | * @param[in] detections List of Detection boxes. |
| 92 | * @param[in] classes Number of classes. |
| 93 | * @param[in] iouThreshold Intersection over union threshold. |
| 94 | **/ |
| 95 | void CalculateNMS(std::forward_list<Detection>& detections, int classes, float iouThreshold); |
| 96 | |
| 97 | /** |
| 98 | * @brief Helper function to convert a UINT8 image to INT8 format. |
| 99 | * @param[in,out] data Pointer to the data start. |
| 100 | * @param[in] kMaxImageSize Total number of pixels in the image. |
| 101 | **/ |
| 102 | void ConvertImgToInt8(void* data, size_t kMaxImageSize); |
| 103 | |
| 104 | /** |
| 105 | * @brief Converts RGB image to grayscale. |
| 106 | * @param[in] srcPtr Pointer to RGB source image. |
| 107 | * @param[out] dstPtr Pointer to grayscale destination image. |
| 108 | * @param[in] imgSz Destination image size. |
| 109 | **/ |
| 110 | void RgbToGrayscale(const uint8_t* srcPtr, uint8_t* dstPtr, size_t dstImgSz); |
| 111 | |
| 112 | } /* namespace image */ |
| 113 | } /* namespace app */ |
| 114 | } /* namespace arm */ |
| 115 | |
| 116 | #endif /* IMAGE_UTILS_HPP */ |