8 std::vector<bool> row_cover, col_cover;
9 std::vector<std::pair<size_t, size_t>> path, result;
17 assert ( 0 <= step && step <= 7);
18 assert ( C.cols >= C.rows );
19 assert ( (C.rows == M.rows && C.cols == M.cols) || step < 3 );
20 assert ( M.rows == row_cover.size() && M.cols == col_cover.size() );
24 step_0 (costmatrix, C, transpose, k, step);
30 step_2 (C, M, row_cover, col_cover, step);
33 step_3 (M, col_cover, k, step);
36 step_4 (C, M, row_cover, col_cover, path, step);
39 step_5 (M, row_cover, col_cover, path, step);
42 step_6 (C, row_cover, col_cover, step);
45 step_7 (M, transpose, result, k, step);
59 void Munkre::step_0 (
const cv::Mat_<double> costmatrix, cv::Mat_<double> &C,
bool &transpose,
size_t &k,
size_t &step) {
63 C = costmatrix.clone();
66 if ( C.rows > C.cols) {
80 for (
size_t r = 0; r < C.rows; r++) {
83 for (
size_t c = 1; c < C.cols; c++) {
89 for (
size_t c = 0; c < C.cols; c++) {
91 if ( std::abs ( C[r][c] ) < __DBL_MIN__)
99 void Munkre::step_2 (
const cv::Mat_<double> C, cv::Mat_<Zero> &M, std::vector<bool> &row_cover, std::vector<bool> &col_cover,
size_t &step) {
100 assert ( step == 2 );
107 M = cv::Mat_<Zero> ( C.rows, C.cols,
UNDEF );
113 row_cover = std::vector<bool> ( C.rows );
119 col_cover = std::vector<bool> ( C.cols );
122 for (
size_t r = 0; r < C.rows; r++) {
123 for (
size_t c = 0; c < C.cols; c++) {
124 if ( C[r][c] == 0 && !row_cover[r] && !col_cover[c] ) {
133 for (
size_t r = 0; r < row_cover.size(); r++)
134 row_cover[r] =
false;
135 for (
size_t c = 0; c < col_cover.size(); c++)
136 col_cover[c] =
false;
141 void Munkre::step_3 (
const cv::Mat_<Zero> M, std::vector<bool> &col_cover,
const size_t k,
size_t &step) {
142 assert ( step == 3 );
146 for (
size_t c = 0; c < M.cols; c++) {
147 for (
size_t r = 0; r < M.rows; r++) {
162 void Munkre::step_4 (
const cv::Mat_<double> C, cv::Mat_<Zero> &M, std::vector<bool> &row_cover, std::vector<bool> &col_cover, std::vector<std::pair<size_t, size_t>> &path,
size_t &step) {
163 assert ( step == 4 );
168 if ( 0 <= row && row < M.rows && 0 <= col && col < M.cols ) {
171 size_t col_prime = col;
174 if (!(0 <= col && col < M.cols)) {
176 path = std::vector<std::pair<size_t, size_t>> (1);
177 path[0] = std::pair<size_t, size_t>((size_t) row, (
size_t) col_prime);
183 row_cover[row] =
true;
184 col_cover[col] =
false;
194 void Munkre::step_5 (cv::Mat_<Zero> M, std::vector<bool> &row_cover, std::vector<bool> &col_cover, std::vector<std::pair<size_t, size_t>> &path,
size_t &step) {
195 assert ( step == 5 );
196 assert ( path.size() == 1 );
200 std::pair<size_t, size_t> Z0, Z1, Z2;
203 Z0 = path[path.size()-1];
207 if ( 0 <= row && row < M.rows ) {
208 Z1 = std::pair<size_t, size_t> ((size_t) row, Z0.second);
214 assert ( 0 <= col && col < M.cols);
215 Z2 = std::pair<size_t, size_t> (Z1.first, (size_t) col);
222 for (
size_t i = 0; i < path.size(); i++) {
223 assert ( M[path[i].first][path[i].second] ==
PRIME ||
224 M[path[i].first][path[i].second] ==
STAR );
226 if (M[path[i].first][path[i].second] ==
STAR)
227 M[path[i].first][path[i].second] =
UNDEF;
229 M[path[i].first][path[i].second] =
STAR;
233 for (
size_t r = 0; r < M.rows; r++) {
234 for (
size_t c = 0; c < M.cols; c++) {
235 if (M[r][c] ==
PRIME)
241 for (
size_t r = 0; r < row_cover.size(); r++)
242 row_cover[r] =
false;
243 for (
size_t c = 0; c < col_cover.size(); c++)
244 col_cover[c] =
false;
249 void Munkre::step_6 (cv::Mat_<double> C,
const std::vector<bool> row_cover,
const std::vector<bool> col_cover,
size_t &step) {
250 assert ( step == 6 );
253 double min = std::numeric_limits<double>::infinity();
254 for (
size_t r = 0; r < C.rows; r++) {
255 for (
size_t c = 0; c < C.cols; c++) {
256 if (!row_cover[r] && !col_cover[c] && C[r][c] < min)
260 assert ( min < std::numeric_limits<double>::infinity() );
264 for (
size_t r = 0; r < C.rows; r++) {
265 for (
size_t c = 0; c < C.cols; c++) {
270 if ( std::abs ( C[r][c] ) < __DBL_MIN__)
278 void Munkre::step_7 (
const cv::Mat_<Zero> M,
const bool transpose, std::vector<std::pair<size_t, size_t>> &result,
size_t &k,
size_t &step) {
279 assert ( step == 7 );
283 result = std::vector<std::pair<size_t, size_t>> (k);
284 for (
size_t r = 0; r < M.rows; r++) {
285 for (
size_t c = 0; c < M.cols; c++) {
286 bool col_finish =
false;
287 if (M[r][c] ==
STAR) {
288 assert ( !col_finish );
292 result[i] = std::pair<size_t, size_t> (c, r);
294 result[i] = std::pair<size_t, size_t> (r, c);
306 void Munkre::find_uncovered_zero (
const cv::Mat_<double> C,
const std::vector<bool> row_cover,
const std::vector<bool> col_cover,
int &row,
int &col) {
307 assert ( C.rows == row_cover.size() && C.cols == col_cover.size() );
311 for (
size_t r = 0; r < C.rows; r++) {
312 for (
size_t c = 0; c < C.cols; c++) {
313 if (C[r][c] == 0 && !row_cover[r] && !col_cover[c]) {
323 for (
size_t c = 0; c < M.cols; c++) {
324 if (M[row][c] == type)
332 for (
size_t r = 0; r < M.rows; r++) {
333 if (M[r][col] == type)
340 void Munkre::print (
const cv::Mat_<double> C,
const cv::Mat_<Zero> M,
const std::vector<bool> row_cover,
const std::vector<bool> col_cover,
const size_t step) {
341 printf(
"next step %lu:\n", step);
343 for (
size_t r = 0; r < C.rows; r++) {
349 for (
size_t c = 0; c < C.cols; c++) {
350 if (row_cover[r] || col_cover[c])
355 printf(
"%.2f", C[r][c]);
357 if (row_cover[r] || col_cover[c])
362 if (M.rows == C.rows && M.cols == C.cols) {
static void find_uncovered_zero(const cv::Mat_< double > C, const std::vector< bool > row_cover, const std::vector< bool > col_cover, int &row, int &col)
static int find_zero_in_col(const cv::Mat_< Zero > M, const size_t col, const Zero type)
static void step_0(const cv::Mat_< double > costmatrix, cv::Mat_< double > &C, bool &transpose, size_t &k, size_t &step)
static void step_2(const cv::Mat_< double > C, cv::Mat_< Zero > &M, std::vector< bool > &row_cover, std::vector< bool > &col_cover, size_t &step)
static void step_6(cv::Mat_< double > C, const std::vector< bool > row_cover, const std::vector< bool > col_cover, size_t &step)
static void step_5(cv::Mat_< Zero > M, std::vector< bool > &row_cover, std::vector< bool > &col_cover, std::vector< std::pair< size_t, size_t >> &path, size_t &step)
static int find_zero_in_row(const cv::Mat_< Zero > M, const size_t row, const Zero type)
static void print(const cv::Mat_< double > C, const cv::Mat_< Zero > M, const std::vector< bool > row_cover, const std::vector< bool > col_cover, const size_t step)
static void step_4(const cv::Mat_< double > C, cv::Mat_< Zero > &M, std::vector< bool > &row_cover, std::vector< bool > &col_cover, std::vector< std::pair< size_t, size_t >> &path, size_t &step)
static std::vector< std::pair< size_t, size_t > > find_minimum_assignment(const cv::Mat_< double > costmatrix)
static void step_7(const cv::Mat_< Zero > M, const bool transpose, std::vector< std::pair< size_t, size_t >> &result, size_t &k, size_t &step)
static void step_1(cv::Mat_< double > &C, size_t &step)
static void step_3(const cv::Mat_< Zero > M, std::vector< bool > &col_cover, const size_t k, size_t &step)