可以利用opencv进行实现,非常简单,如果不使用opencv也可以使用min,max也不难。因此下面的C++代码包含了两种计算矩形框IOU的方法。
#include<iostream>
#include<algorithm>
#include<vector>
#include<opencv2/opencv.hpp>
using namespace std;
struct bbox
{
int m_left;
int m_top;
int m_width;
int m_height;
bbox() {}
bbox(int left, int top, int width, int height)
{
m_left = left;
m_top = top;
m_width = width;
m_height = height;
}
};
float IOU(const bbox& b1, const bbox& b2)
{
float w = std::min(b1.m_left + b1.m_width, b2.m_left + b2.m_width)
- std::max(b1.m_left, b2.m_left);
float h = std::min(b1.m_top + b1.m_height, b2.m_top + b2.m_height)
- std::max(b1.m_top, b2.m_top);
if (w <= 0 || h <= 0)
return 0;
std::cout << "w :" << w << "h :" << h << std::endl;
return (w * h) / ((b1.m_height * b1.m_width) + (b2.m_heig