| 28 | |
| 29 | template <typename image_scanner_type> |
| 30 | std::vector<rectangle> determine_object_boxes ( |
| 31 | const image_scanner_type& scanner, |
| 32 | const std::vector<rectangle>& rects, |
| 33 | double min_match_score |
| 34 | ) |
| 35 | { |
| 36 | // make sure requires clause is not broken |
| 37 | DLIB_ASSERT(0 < min_match_score && min_match_score <= 1, |
| 38 | "\t std::vector<rectangle> determine_object_boxes()" |
| 39 | << "\n\t Invalid inputs were given to this function. " |
| 40 | << "\n\t min_match_score: " << min_match_score |
| 41 | ); |
| 42 | |
| 43 | typename image_scanner_type::pyramid_type pyr; |
| 44 | |
| 45 | typedef std::list<std::pair<unsigned long, rectangle> > list_type; |
| 46 | |
| 47 | unsigned long max_area = 0; |
| 48 | |
| 49 | // Copy rects into sorted_rects and sort them in order of increasing area. But |
| 50 | // only include the rectangles that aren't already obtainable by the scanner. |
| 51 | list_type sorted_rects; |
| 52 | for (unsigned long i = 0; i < rects.size(); ++i) |
| 53 | { |
| 54 | if (scanner.get_num_detection_templates() > 0) |
| 55 | { |
| 56 | rectangle temp = scanner.get_best_matching_rect(rects[i]); |
| 57 | const double match_score = (rects[i].intersect(temp).area())/(double)(rects[i] + temp).area(); |
| 58 | // skip this rectangle if it's already matched well enough. |
| 59 | if (match_score > min_match_score) |
| 60 | continue; |
| 61 | } |
| 62 | max_area = std::max(rects[i].area(), max_area); |
| 63 | sorted_rects.push_back(std::make_pair(rects[i].area(), rects[i])); |
| 64 | } |
| 65 | sorted_rects.sort(dlib::impl::compare_first); |
| 66 | |
| 67 | // Make sure this area value is comfortably larger than all the |
| 68 | // rectangles' areas. |
| 69 | max_area = 3*max_area + 100; |
| 70 | |
| 71 | std::vector<rectangle> object_boxes; |
| 72 | |
| 73 | while (sorted_rects.size() != 0) |
| 74 | { |
| 75 | rectangle cur = sorted_rects.front().second; |
| 76 | sorted_rects.pop_front(); |
| 77 | object_boxes.push_back(centered_rect(point(0,0), cur.width(), cur.height())); |
| 78 | |
| 79 | // Scale cur up the image pyramid and remove any rectangles which match. |
| 80 | // But also stop when cur gets large enough to not match anything. |
| 81 | for (unsigned long itr = 0; |
| 82 | itr < scanner.get_max_pyramid_levels() && cur.area() < max_area; |
| 83 | ++itr) |
| 84 | { |
| 85 | auto i = sorted_rects.begin(); |
| 86 | while (i != sorted_rects.end()) |
| 87 | { |
no test coverage detected