实验需要,需要计算两个矩形重叠面积
想来想去觉得挺复杂,搜了下,看见一个超给力的方法
这里分享下:
function D = DecideOberlap(Reframe,GTframe)
x1 = Reframe(1);
y1 = Reframe(2);
width1 = Reframe(3);
height1 = Reframe(4);
x2 = GTframe(1);
y2 = GTframe(2);
width2 = GTframe(3);
height2 = GTframe(4);
endx = max(x1+width1,x2+width2);
startx = min(x1,x2);
width = width1+width2-(endx-startx);
endy = max(y1+height1,y2+height2);
starty = min(y1,y2);
height = height1+height2-(endy-starty);
if width<=0||height<=0
D = 0;
else
Area = width*height;
Area1 = width1*height1;
Area2 = width2*height2;
ratio = Area/(Area1+Area2-Area);
if ratio>=0.5
D = 1;
else
D = 0;
end
end
ratio就是计算出来的重叠率
函数中的两个参数分别是两个矩形的左上角点的坐标和矩形长,宽。
代码效率很高,写成c也很容易