您的位置:首页 > 运维架构

opencv 中两个rect相交

2015-07-27 16:04 405 查看
在对opencv进行应用中,我们常会应用到rect之间的关系,可以采用如下方法来实现:

bool rectA_intersect_rectB(cv::Rect rectA, cv::Rect rectB)

如果rectA与rectB相交,则返回true,这里的相交没有包含的概念,如果要用到包含则可根据下面代码进行修改

bool rectA_intersect_rectB(cv::Rect rectA, cv::Rect rectB)
{
	if ( rectA.x > rectB.x + rectB.width ) { return false; }  
	if ( rectA.y > rectB.y + rectB.height ) { return false; }
	if ( (rectA.x + rectA.width) < rectB.x ) { return false; }
	if ( (rectA.y + rectA.height) < rectB.y ) { return false; }

	float colInt =  min(rectA.x+rectA.width,rectB.x+rectB.width) - max(rectA.x, rectB.x);  
	float rowInt =  min(rectA.y+rectA.height,rectB.y+rectB.height) - max(rectA.y,rectB.y);  
	float intersection = colInt * rowInt;  
	float areaA = rectA.width * rectA.height;  
	float areaB = rectB.width * rectB.height;  
	float intersectionPercent =  intersection / (areaA + areaB - intersection);  

	if ( (0 < intersectionPercent)&&(intersectionPercent < 1)&&(intersection != areaA)&&(intersection != areaB) )
	{
		return true;
	}

	return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: