【OpenCV系列】【二】利用Rect计算IOU

在目标检测中,经常需要计算IOU。也就是两个矩形的交集除以两个矩形的并集。这里使用OpevCV的Rect来进行IOU的计算。

【场景一】无交集,IOU为0

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main() {
	Rect rect;
	rect.x = 0;
	rect.y = 0;
	rect.width = 10;
	rect.height = 10;

	Rect rect1;
	rect1.x = 10;
	rect1.y = 10;
	rect1.width = 10;
	rect1.height = 10;

	//计算两个矩形的交集
	Rect rect2 = rect | rect1;
	cout << rect2.x <<";"<< rect2.y << ";"<<rect2.width <<";"<< rect2.height <<";"<< rect2.area() << endl;

	//计算两个举行的并集
	Rect rect3 = rect & rect1;
	cout << rect3.x << ";" << rect3.y << ";" << rect3.width << ";" << rect3.height << ";" << rect3.area() << endl;

	//计算IOU
	double IOU = rect3.area() *1.0/ rect2.area();
	cout << "IOU=" << IOU << endl;

	system("Pause");
}

结果为:

202014_sOJi_3800567.png

【场景一】有交集,IOU为0.44444

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main() {
	Rect rect;
	rect.x = 0;
	rect.y = 0;
	rect.width = 10;
	rect.height = 10;

	Rect rect1;
	rect1.x = 2;
	rect1.y = 2;
	rect1.width = 10;
	rect1.height = 10;

	//计算两个矩形的交集
	Rect rect2 = rect | rect1;
	cout << rect2.x <<";"<< rect2.y << ";"<<rect2.width <<";"<< rect2.height <<";"<< rect2.area() << endl;

	//计算两个举行的并集
	Rect rect3 = rect & rect1;
	cout << rect3.x << ";" << rect3.y << ";" << rect3.width << ";" << rect3.height << ";" << rect3.area() << endl;

	//计算IOU
	double IOU = rect3.area()*1.0 / rect2.area();
	cout << "IOU=" << IOU << endl;

	system("Pause");
}

202603_ECmK_3800567.png

Rect坐标系为:

202733_2x4N_3800567.png

总结:可以看出,使用opencv计算IOU比较简单,只需要三行代码,当然,自己手动计算,也不费事,只是需要判断各种场景而已。

转载于:https://my.oschina.net/u/3800567/blog/1795889

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值