【Cpp】nms demo

1,代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

double IOU(const Rect& r1, const Rect& r2)
{
    int x1 = std::max(r1.x, r2.x);
    int y1 = std::max(r1.y, r2.y);
    int x2 = std::min(r1.x+r1.width, r2.x+r2.width);
    int y2 = std::min(r1.y+r1.height, r2.y+r2.height);
    int w = std::max(0, (x2-x1+1));
    int h = std::max(0, (y2-y1+1));
    double inter = w * h;
    double o = inter / (r1.area() + r2.area() - inter);
    return (o >= 0) ? o : 0;
}

void nms(vector<Rect>& proposals, const double nms_threshold)
{
    vector<int> scores;
    for(auto i : proposals) scores.push_back(i.area());

    vector<int> index;
    for(int i = 0; i < scores.size(); ++i){
        index.push_back(i);
    }

    sort(index.begin(), index.end(), [&](int a, int b){
        return scores[a] > scores[b];
    }); 

    vector<bool> del(scores.size(), false);
    for(size_t i = 0; i < index.size(); i++){
        if( !del[index[i]]){
            for(size_t j = i+1; j < index.size(); j++){
                if(IOU(proposals[index[i]], proposals[index[j]]) > nms_threshold){
                    del[index[j]] = true;
                }
            }
        }
    }

    vector<Rect> new_proposals;
    for(const auto i : index){
        if(!del[i]) new_proposals.push_back(proposals[i]);
    }
    proposals = new_proposals;
}

void test_nms()
{
    Mat img = imread("D:\\test\\test.jpg");
    Mat img2 = img.clone();

    // prepare data
    vector<Rect> proposals;
    Point2i origin(20, 20);
    int w = 100;
    int h = 200;
    for (auto i : {0,1,2,3,4,5}) proposals.push_back(Rect(origin.x + 10*i, origin.y + 20*i, w - 5*i, h-7*i));
    origin.x = 200;
    origin.y = 400;
    w = 300;
    h = 120;
    for (auto i : {0,1,2,3,4,5}) proposals.push_back(Rect(origin.x + 10*i, origin.y + 20*i, w - 5*i, h-7*i));

    proposals.push_back(Rect(400, 200, 386, 239));
    proposals.push_back(Rect(200, 120, 186, 209));
    proposals.push_back(Rect(310, 376, 286, 139));

    for(auto rect : proposals) rectangle(img, rect, Scalar(0, 255, 255), 2);
    imshow("before", img);
    // nms
    nms(proposals, 0.1);
    for(auto rect : proposals) rectangle(img2, rect, Scalar(0, 255, 255), 2);
    imshow("after", img2);
    waitKey(0);
}
int main()
{
    test_nms();
    return 0;
}

2,相关链接
https://github.com/yang-song/NMS/blob/master/nms.cpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值