1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 //
5 // Copyright (C) 2017, Intel Corporation, all rights reserved.
6 // Third party copyrights are property of their respective owners.
7 
8 #include "precomp.hpp"
9 #include "nms.inl.hpp"
10 
11 #include <opencv2/imgproc.hpp>
12 
13 namespace cv { namespace dnn {
14 CV__DNN_INLINE_NS_BEGIN
15 
16 template <typename T>
rectOverlap(const T & a,const T & b)17 static inline float rectOverlap(const T& a, const T& b)
18 {
19     return 1.f - static_cast<float>(jaccardDistance(a, b));
20 }
21 
NMSBoxes(const std::vector<Rect> & bboxes,const std::vector<float> & scores,const float score_threshold,const float nms_threshold,std::vector<int> & indices,const float eta,const int top_k)22 void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores,
23                           const float score_threshold, const float nms_threshold,
24                           std::vector<int>& indices, const float eta, const int top_k)
25 {
26     CV_Assert_N(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);
29 }
30 
NMSBoxes(const std::vector<Rect2d> & bboxes,const std::vector<float> & scores,const float score_threshold,const float nms_threshold,std::vector<int> & indices,const float eta,const int top_k)31 void NMSBoxes(const std::vector<Rect2d>& bboxes, const std::vector<float>& scores,
32                           const float score_threshold, const float nms_threshold,
33                           std::vector<int>& indices, const float eta, const int top_k)
34 {
35     CV_Assert_N(bboxes.size() == scores.size(), score_threshold >= 0,
36         nms_threshold >= 0, eta > 0);
37     NMSFast_(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, rectOverlap);
38 }
39 
rotatedRectIOU(const RotatedRect & a,const RotatedRect & b)40 static inline float rotatedRectIOU(const RotatedRect& a, const RotatedRect& b)
41 {
42     std::vector<Point2f> inter;
43     int res = rotatedRectangleIntersection(a, b, inter);
44     if (inter.empty() || res == INTERSECT_NONE)
45         return 0.0f;
46     if (res == INTERSECT_FULL)
47         return 1.0f;
48     float interArea = contourArea(inter);
49     return interArea / (a.size.area() + b.size.area() - interArea);
50 }
51 
NMSBoxes(const std::vector<RotatedRect> & bboxes,const std::vector<float> & scores,const float score_threshold,const float nms_threshold,std::vector<int> & indices,const float eta,const int top_k)52 void NMSBoxes(const std::vector<RotatedRect>& bboxes, const std::vector<float>& scores,
53               const float score_threshold, const float nms_threshold,
54               std::vector<int>& indices, const float eta, const int top_k)
55 {
56     CV_Assert_N(bboxes.size() == scores.size(), score_threshold >= 0,
57         nms_threshold >= 0, eta > 0);
58     NMSFast_(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, rotatedRectIOU);
59 }
60 
61 CV__DNN_INLINE_NS_END
62 }// dnn
63 }// cv
64