python中rect⽤法_【opencv基础】Rect类的神奇⽤法
前⾔
最近看github上源码发现对两个cv::Rect使⽤相与(&)操作,猛地感觉⾃⼰蒙啦,Rect类还有这种神奇⽤法?!翻看opencv官⽹Rect类,果然如此!
opencv中Rect类
Rect_类有些意思,成员变量x、y、width、height,分别为左上⾓点的坐标和矩形的宽和⾼。常⽤的成员函数有Size()返回值为⼀个Size,area()返回矩形的⾯积,contains(Point)⽤来判断点是否在矩形内,inside(Rect)函数判断矩形是否在该矩形内,tl()返回左上⾓点坐标,br()返回右下⾓点坐标。
Rect类的使⽤
rect = rect ± point (shifting a rectangle by a certain offset)
rect = rect ± size (expanding or shrinking a rectangle by a certain amount)
rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
rect = rect1 & rect2 (rectangle intersection)
rect = rect1 | rect2 (minimum area rectangle containing rect1 and rect2 )
rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
rect == rect1, rect != rect1 (rectangle comparison)
两个矩形的交并
Rect rect = rect1 &rect2;
Rect rect= rect1 | rect2;
两个矩形的平移或者缩放
Rect rectShift = rect +point;
Rect rectScale= rect + size;
rectangle函数opencvall usage
/
/如果创建⼀个Rect对象rect(100, 50, 50, 100),那么rect会有以下⼏个功能:
rect.area(); //返回rect的⾯积 5000
rect.size(); //返回rect的尺⼨ [50 × 100]
rect.tl(); //返回rect的左上顶点的坐标 [100, 50]
rect.br(); //返回rect的右下顶点的坐标 [150, 150]
rect.width(); //返回rect的宽度 50
rect.height(); //返回rect的⾼度 100
rect = rect1 &rect2;
rect= rect1 |rect2;//还可以对矩形进⾏平移和缩放
rect = rect + Point(-100, 100); //平移,也就是左上顶点的x坐标-100,y坐标+100
rect = rect + Size(-100, 100); //缩放,左上顶点不变,宽度-100,⾼度+100//还可以对矩形进⾏对⽐,返回布尔变量
rect1 ==rect2;
rect1!=rect2;//OpenCV⾥貌似没有判断rect1是否在rect2⾥⾯的功能,所以⾃⼰写⼀个吧boolisInside(Rect rect1, Rect rect2)
{return (rect1 == (rect1&rect2));
}//OpenCV貌似也没有获取矩形中⼼点的功能,还是⾃⼰写⼀个
Point getCenterPoint(Rect rect)
{
Point cpt;
cpt.x= rect.x + cvRound(rect.width/2.0);
cpt.y= rect.y + cvRound(rect.height/2.0);returncpt;
}//围绕矩形中⼼缩放
Rect rectCenterScale(Rect rect, Size size)
{
rect= rect +size;
Point pt;
pt.x= cvRound(size.width/2.0);
pt.y= cvRound(size.height/2.0);return (rect-pt);
}
参考

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。