目标检测 — 计算IOU

1、IOU=交集/并集

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include <vector>
#include<string>
#include<sstream>
#include<map>
#include<set>
#include<iomanip>
#include <functional> // std::greater
using namespace std;

double calcS(vector<int> num)
{
    return (num[2] - num[0])*(num[3] - num[1]);
}

vector<vector<double>> calcIOU(vector<vector<int>> &nums)
{
    vector<vector<double>> res(nums.size(), vector<double>(nums.size(),0.0));
    for (int i = 0; i < nums.size(); ++i)
    {
        for (int j = i + 1; j < nums.size(); ++j)
        {
            
            int x1 = max(nums[i][0], nums[j][0]);
            int x2 = min(nums[i][2], nums[j][2]);
            int y1 = max(nums[i][1], nums[j][1]);
            int y2 = min(nums[i][3], nums[j][3]);
            double inter_square = (x2 - x1)*(y2 - y1);
            double union_square = calcS(nums[i]) + calcS(nums[j]) - inter_square;
            res[i][j] = inter_square / union_square;
        }
    }
    return res;
}

int main()
{
    vector<vector<int>> nums;
    vector<vector<double>> res;
    // 表示坐标位置,(x1,y1,x2,y2),分别是左上角和右下角的坐标
    int a[3][4] = { { 3,6,9,11 },{ 6,3,8,7 },{ 3,7,10,12 } };
    for (int i = 0; i < 3; ++i)
    {
        vector<int> temp;
        for (int j = 0; j < 4; ++j)
        {
            temp.push_back(a[i][j]);
        }
        nums.push_back(temp);
    }

    res = calcIOU(nums);
    for (int i = 0; i < nums.size(); ++i)
    {
        for (int j = i + 1; j < nums.size(); ++j)
            cout << fixed << setprecision(3) <<res[i][j] << "  ";
    }
    cout << endl;
    system("pause");
    return 0;
}

 

posted @ 2018-09-04 12:41  深度机器学习  阅读(888)  评论(0编辑  收藏  举报