Skip to content

Commit c704942

Browse files
committed
dnn: add a documentation for NMS, fix missing experimantal namespace
1 parent acedb4a commit c704942

File tree

4 files changed

+32
-43
lines changed

4 files changed

+32
-43
lines changed

modules/dnn/include/opencv2/dnn/dnn.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -734,18 +734,20 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
734734
*/
735735
CV_EXPORTS_W void shrinkCaffeModel(const String& src, const String& dst);
736736

737-
/** @brief
738-
* @param bboxes
739-
* @param scores
740-
* @param score_threshold
741-
* @param nms_threshold
742-
* @param eta
743-
* @param top_k
744-
* @param indices
737+
/** @brief Performs non maximum suppression given boxes and corresponding scores.
738+
739+
* @param bboxes a set of bounding boxes to apply NMS.
740+
* @param scores a set of corresponding confidences.
741+
* @param score_threshold a threshold used to filter boxes by score.
742+
* @param nms_threshold a threshold used in non maximum suppression.
743+
* @param indices the kept indices of bboxes after NMS.
744+
* @param eta a coefficient in adaptive threshold formula: \f$nms\_threshold_{i+1}=eta\cdot nms\_threshold_i\f$.
745+
* @param top_k if `>0`, keep at most @p top_k picked indices.
745746
*/
746747
CV_EXPORTS_W void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores,
747748
const float score_threshold, const float nms_threshold,
748-
const float eta, const int top_k, CV_OUT std::vector<int>& indices);
749+
CV_OUT std::vector<int>& indices,
750+
const float eta = 1.f, const int top_k = 0);
749751

750752

751753
//! @}

modules/dnn/include/opencv2/dnn/nms.inl.hpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,26 @@ inline void GetMaxScoreIndex(const std::vector<float>& scores, const float thres
4848
SortScorePairDescend<int>);
4949

5050
// Keep top_k scores if needed.
51-
if (top_k > -1 && top_k < (int)score_index_vec.size())
51+
if (top_k > 0 && top_k < (int)score_index_vec.size())
5252
{
5353
score_index_vec.resize(top_k);
5454
}
5555
}
5656

57-
template <typename BoxType>
58-
struct NMSOverlap
59-
{
60-
float operator() (const BoxType& a, const BoxType& b);
61-
};
62-
63-
template <>
64-
inline float NMSOverlap<Rect>::operator() (const Rect& a, const Rect& b)
65-
{
66-
float rectIntersectionArea = (float)(a & b).area();
67-
return rectIntersectionArea / (a.area() + b.area() - rectIntersectionArea);
68-
}
69-
7057
// Do non maximum suppression given bboxes and scores.
7158
// Inspired by Piotr Dollar's NMS implementation in EdgeBox.
7259
// https://goo.gl/jV3JYS
7360
// bboxes: a set of bounding boxes.
7461
// scores: a set of corresponding confidences.
7562
// score_threshold: a threshold used to filter detection results.
7663
// nms_threshold: a threshold used in non maximum suppression.
77-
// top_k: if not -1, keep at most top_k picked indices.
64+
// top_k: if not > 0, keep at most top_k picked indices.
7865
// indices: the kept indices of bboxes after nms.
7966
template <typename BoxType>
8067
inline void NMSFast_(const std::vector<BoxType>& bboxes,
8168
const std::vector<float>& scores, const float score_threshold,
8269
const float nms_threshold, const float eta, const int top_k,
83-
std::vector<int>& indices, NMSOverlap<BoxType> computeOverlap)
70+
std::vector<int>& indices, float (*computeOverlap)(const BoxType&, const BoxType&))
8471
{
8572
CV_Assert(bboxes.size() == scores.size());
8673

@@ -91,8 +78,8 @@ inline void NMSFast_(const std::vector<BoxType>& bboxes,
9178
// Do nms.
9279
float adaptive_threshold = nms_threshold;
9380
indices.clear();
94-
while (score_index_vec.size() != 0) {
95-
const int idx = score_index_vec.front().second;
81+
for (size_t i = 0; i < score_index_vec.size(); ++i) {
82+
const int idx = score_index_vec[i].second;
9683
bool keep = true;
9784
for (int k = 0; k < (int)indices.size() && keep; ++k) {
9885
const int kept_idx = indices[k];
@@ -101,7 +88,6 @@ inline void NMSFast_(const std::vector<BoxType>& bboxes,
10188
}
10289
if (keep)
10390
indices.push_back(idx);
104-
score_index_vec.erase(score_index_vec.begin());
10591
if (keep && eta < 1 && adaptive_threshold > 0.5) {
10692
adaptive_threshold *= eta;
10793
}

modules/dnn/src/layers/detection_output_layer.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ static inline bool SortScorePairDescend(const std::pair<float, T>& pair1,
6262
return pair1.first > pair2.first;
6363
}
6464

65+
static inline float caffe_box_overlap(const caffe::NormalizedBBox& a, const caffe::NormalizedBBox& b);
66+
6567
} // namespace
6668

6769
class DetectionOutputLayerImpl : public DetectionOutputLayer
@@ -309,7 +311,8 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
309311
LabelBBox::const_iterator label_bboxes = decodeBBoxes.find(label);
310312
if (label_bboxes == decodeBBoxes.end())
311313
CV_ErrorNoReturn_(cv::Error::StsError, ("Could not find location predictions for label %d", label));
312-
ApplyNMSFast(label_bboxes->second, scores, _confidenceThreshold, _nmsThreshold, 1.0, _topK, indices[c]);
314+
NMSFast_(label_bboxes->second, scores, _confidenceThreshold, _nmsThreshold, 1.0, _topK,
315+
indices[c], util::caffe_box_overlap);
313316
numDetections += indices[c].size();
314317
}
315318
if (_keepTopK > -1 && numDetections > (size_t)_keepTopK)
@@ -620,16 +623,6 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
620623
}
621624
}
622625

623-
624-
625-
static void ApplyNMSFast(const std::vector<caffe::NormalizedBBox>& bboxes,
626-
const std::vector<float>& scores, const float score_threshold,
627-
const float nms_threshold, const float eta, const int top_k,
628-
std::vector<int>& indices)
629-
{
630-
NMSFast_(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, NMSOverlap<caffe::NormalizedBBox>());
631-
}
632-
633626
// Compute the jaccard (intersection over union IoU) overlap between two bboxes.
634627
template<bool normalized>
635628
static float JaccardOverlap(const caffe::NormalizedBBox& bbox1,
@@ -675,8 +668,7 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
675668
}
676669
};
677670

678-
template <>
679-
float NMSOverlap<caffe::NormalizedBBox>::operator() (const caffe::NormalizedBBox& a, const caffe::NormalizedBBox& b)
671+
float util::caffe_box_overlap(const caffe::NormalizedBBox& a, const caffe::NormalizedBBox& b)
680672
{
681673
return DetectionOutputLayerImpl::JaccardOverlap<true>(a, b);
682674
}

modules/dnn/src/nms.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ namespace cv
1212
{
1313
namespace dnn
1414
{
15+
CV__DNN_EXPERIMENTAL_NS_BEGIN
16+
17+
static inline float rectOverlap(const Rect& a, const Rect& b)
18+
{
19+
return 1.f - static_cast<float>(jaccardDistance(a, b));
20+
}
1521

1622
void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores,
1723
const float score_threshold, const float nms_threshold,
18-
const float eta, const int top_k, std::vector<int>& indices)
24+
std::vector<int>& indices, const float eta, const int top_k)
1925
{
20-
NMSFast_(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, NMSOverlap<Rect>());
26+
CV_Assert(bboxes.size() == scores.size(), score_threshold >= 0,
27+
nms_threshold >= 0, eta > 0);
28+
NMSFast_(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, rectOverlap);
2129
}
2230

31+
CV__DNN_EXPERIMENTAL_NS_END
2332
}// dnn
2433
}// cv

0 commit comments

Comments
 (0)