| 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2023 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | ******************************************************************************/ |
| 6 | |
| 7 | #ifndef VAS_OT_HUNGARIAN_WRAP_HPP |
| 8 | #define VAS_OT_HUNGARIAN_WRAP_HPP |
| 9 | |
| 10 | #include <opencv2/core.hpp> |
| 11 | |
| 12 | #include <cstdint> |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace vas { |
| 16 | namespace ot { |
| 17 | |
| 18 | const int32_t kHungarianModeMinimizeCost = 0; |
| 19 | const int32_t kHungarianModeMaximizeUtil = 1; |
| 20 | |
| 21 | typedef struct { |
| 22 | int32_t num_rows; |
| 23 | int32_t num_cols; |
| 24 | |
| 25 | std::vector<std::vector<int32_t>> cost; |
| 26 | std::vector<std::vector<int32_t>> assignment; |
| 27 | } hungarian_problem_t; |
| 28 | |
| 29 | class HungarianAlgo { |
| 30 | public: |
| 31 | explicit HungarianAlgo(const cv::Mat_<float> &cost_map); |
| 32 | ~HungarianAlgo(); |
| 33 | |
| 34 | cv::Mat_<uint8_t> Solve(); |
| 35 | |
| 36 | HungarianAlgo() = delete; |
| 37 | HungarianAlgo(const HungarianAlgo &) = delete; |
| 38 | HungarianAlgo(HungarianAlgo &&) = delete; |
| 39 | HungarianAlgo &operator=(const HungarianAlgo &) = delete; |
| 40 | HungarianAlgo &operator=(HungarianAlgo &&) = delete; |
| 41 | |
| 42 | protected: |
| 43 | /* This method initializes the hungarian_problem structure and the cost matrices (missing lines or columns are |
| 44 | *filled with 0). It returns the size of the quadratic(!) assignment matrix. |
| 45 | **/ |
| 46 | int32_t InitHungarian(int32_t mode); |
| 47 | |
| 48 | // Computes the optimal assignment |
| 49 | void SolveHungarian(); |
| 50 | |
| 51 | // Free the memory allocated by Init |
| 52 | void FreeHungarian(); |
| 53 | |
| 54 | int32_t size_width_; |
| 55 | int32_t size_height_; |
| 56 | |
| 57 | private: |
| 58 | const int32_t kHungarianNotAssigned = 0; |
| 59 | const int32_t kHungarianAssigned = 1; |
| 60 | const int32_t kIntMax = INT_MAX; |
| 61 | |
| 62 | std::vector<int32_t *> int_cost_map_rows_; |
| 63 | cv::Mat_<int32_t> int_cost_map_; |
| 64 | |
| 65 | hungarian_problem_t problem_; |
| 66 | }; |
| 67 | |
| 68 | }; // namespace ot |
| 69 | }; // namespace vas |
| 70 | |
| 71 | #endif // VAS_OT_HUNGARIAN_WRAP_HPP |
| 72 | |