zoukankan      html  css  js  c++  java
  • c++版 nms

     定义的box是左上角的x、y以及宽度w、高度h

     iou计算出来的交集的w、h必须判断是否大于0,这避免了两个框没有交集的情况。如果不判断,可能存在w、h都为负生成iou>0的情况进而影响结果。

     +1操作是计算像素点个数,这个地方其实有点没太理解到,但开源的代码都是这么写的。

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    
    
    typedef struct Bbox{
        int x;
        int y;
        int w;
        int h;
        float score;
    }Bbox;
    
    class Solution {
    public:
    
        static bool sort_score(Bbox box1,Bbox box2){
            return box1.score > box2.score ? true : false;
        }
    
    
        float iou(Bbox box1,Bbox box2){
            int x1 = max(box1.x,box2.x);
            int y1 = max(box1.y,box2.y);
            int x2 = min(box1.x+box1.w,box2.x+box2.w);
            int y2 = min(box1.y+box1.h,box2.y+box2.h);
            int w = max(0,x2 - x1 + 1);
            int h = max(0,y2 - y1 + 1);
            float over_area = w*h;
            return over_area/(box1.w * box1.h + box2.w * box2.h - over_area);
        }
    
        vector<Bbox> nms(std::vector<Bbox>&vec_boxs,float threshold){
            vector<Bbox>results;
            std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
            while(vec_boxs.size() > 0)
            {
                results.push_back(vec_boxs[0]);
                int index = 1;
                while(index < vec_boxs.size()){
                    float iou_value = iou(vec_boxs[0],vec_boxs[index]);
                    cout << "iou:" << iou_value << endl;
                    if(iou_value > threshold)
                        vec_boxs.erase(vec_boxs.begin() + index);
                    else
                        index++;
                }
                vec_boxs.erase(vec_boxs.begin());
            }
            return results;
        }
    };
    
    int main(){
        Solution a;
        vector<Bbox> input;
    
        // Bbox box1 = {1,1,1,1,0.3};
        // Bbox box2 = {0,0,2,2,0.4};
        //Bbox box1 = {1,3,2,2,0.3};        //iou为负
        //Bbox box2 = {0,0,2,2,0.4};
        //Bbox box1 = {4,4,2,2,0.3};
        //Bbox box2 = {0,0,2,2,0.4};
        // Bbox box1 = {4,4,1,1,0.3};
        // Bbox box2 = {0,0,2,2,0.4};
        // Bbox box1 = {3,3,1,1,0.3};
        // Bbox box2 = {0,0,2,2,0.4};
        input.push_back(box1);
        input.push_back(box2);
        vector<Bbox> res;
        res = a.nms(input,0.3);
        // for(int i = 0;i < res.size();i++){
        //     printf("%d %d %d %d %f",res[i].x,res[i].y,res[i].w,res[i].h,res[i].score);
        //     cout << endl;
        // }
        return 0;
    }
  • 相关阅读:
    ab命令做压测测试
    用js两张图片合并成一张图片
    Web全景图的原理及实现
    深入理解Java中的IO
    Spring AOP详解
    spring @Transactional注解参数详解
    优化MyBatis配置文件中的配置
    使用MyBatis对表执行CRUD操作
    @requestBody注解的使用
    url 拼接的一个模块furl
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/9677340.html
Copyright © 2011-2022 走看看