void ApplyNonMaximumSuppresion(std::vector< kstate >& in_source, float in_nms_threshold) { std::vector< kstate > tmp_source = in_source; if (tmp_source.empty()) return ; unsigned int size = in_source.size(); std::vector<float> area(size); std::vector<float> scores(size); std::vector<int> x1(size); std::vector<int> y1(size); std::vector<int> x2(size); std::vector<int> y2(size); std::vector<unsigned int> indices(size); std::vector<bool> is_suppresed(size); for(unsigned int i = 0; i< in_source.size(); i++) { kstate tmp = in_source[i]; area[i] = tmp.pos.width * tmp.pos.height; indices[i] = i; is_suppresed[i] = false; scores[i] = tmp.score; x1[i] = tmp.pos.x; y1[i] = tmp.pos.y; x2[i] = tmp.pos.width + tmp.pos.x; y2[i] = tmp.pos.height + tmp.pos.y; } Sort(scores, indices);//returns indices ordered based on scores for(unsigned int i=0; i< size; i++) { if(!is_suppresed[indices[i]]) { for(unsigned int j= i+1; j< size; j++) { int x1_max = std::max(x1[indices[i]], x1[indices[j]]); int x2_min = std::min(x2[indices[i]], x2[indices[j]]); int y1_max = std::max(y1[indices[i]], y1[indices[j]]); int y2_min = std::min(y2[indices[i]], y2[indices[j]]); int overlap_width = x2_min - x1_max + 1; int overlap_height = y2_min - y1_max + 1; if(overlap_width > 0 && overlap_height>0) { float overlap_part = (overlap_width*overlap_height)/area[indices[j]]; if(overlap_part > in_nms_threshold) { is_suppresed[indices[j]] = true; } } } } } unsigned int size_out = 0; for (unsigned int i = 0; i < size; i++) { if (!is_suppresed[i]) size_out++; } std::vector< kstate > filtered_detections(size_out); unsigned int index = 0; for(unsigned int i = 0 ; i < size_out; i++) { if(!is_suppresed[indices[i]]) { filtered_detections[index] = in_source[indices[i]];//x1[indices[i]]; index++; } } in_source = filtered_detections; }
void ApplyNonMaximumSuppresion(std::vector< LkTracker* >& in_out_source, float in_nms_threshold) { if (in_out_source.empty()) return; unsigned int size = in_out_source.size(); std::vector<float> area(size); std::vector<float> scores(size); std::vector<int> x1(size); std::vector<int> y1(size); std::vector<int> x2(size); std::vector<int> y2(size); std::vector<unsigned int> indices(size); std::vector<bool> is_suppresed(size); for(unsigned int i = 0; i< in_out_source.size(); i++) { cv::LatentSvmDetector::ObjectDetection tmp = in_out_source[i]->GetTrackedObject(); area[i] = tmp.rect.width * tmp.rect.height; if (area[i]>0) is_suppresed[i] = false; else { is_suppresed[i] = true; in_out_source[i]->NullifyLifespan(); } indices[i] = i; scores[i] = tmp.score; x1[i] = tmp.rect.x; y1[i] = tmp.rect.y; x2[i] = tmp.rect.width + tmp.rect.x; y2[i] = tmp.rect.height + tmp.rect.y; } Sort(area, indices);//returns indices ordered based on scores for(unsigned int i=0; i< size; i++) { for(unsigned int j= i+1; j< size; j++) { if(is_suppresed[indices[i]] || is_suppresed[indices[j]]) continue; int x1_max = std::max(x1[indices[i]], x1[indices[j]]); int x2_min = std::min(x2[indices[i]], x2[indices[j]]); int y1_max = std::max(y1[indices[i]], y1[indices[j]]); int y2_min = std::min(y2[indices[i]], y2[indices[j]]); int overlap_width = x2_min - x1_max + 1; int overlap_height = y2_min - y1_max + 1; if(overlap_width > 0 && overlap_height>0) { float overlap_part = (overlap_width*overlap_height)/area[indices[j]]; if(overlap_part > in_nms_threshold) { is_suppresed[indices[j]] = true; in_out_source[indices[j]]->NullifyLifespan(); if (in_out_source[indices[j]]->GetFrameCount() > in_out_source[indices[i]]->GetFrameCount()) { in_out_source[indices[i]]->object_id = in_out_source[indices[j]]->object_id; } } } } } return ; }