简体   繁体   English

有没有简单的方法可以将cv :: Rect的大小和坐标相乘?

[英]Is there any simple way to multiply cv::Rect size and coordinates?

Let's say I have 2 rectangle. 假设我有2个矩形。 I want the second rectangle to be twice bigger than the first rectangle and the position of x,y also twice bigger. 我希望第二个矩形比第一个矩形大两倍,并且x,y的位置也要大一倍。

cv::Rect r1=Rect(10,20,40,60);
cv::Rect r2 = r1 * 2;  //this won't work

Setting the rectangle 2 parameter 1 by 1 will work 将矩形2参数1设置为1将起作用

r2.height = r1.height * 2;
r2.width = r1.height * 2;
r2.x = r1.x * 2;
r2.y = r2.y * 2;

It works, but is there any simpler way to do it (like single line code)? 它可以工作,但是有没有更简单的方法(例如单行代码)呢?

If you want to do this, this might be the shortest way: 如果要这样做,这可能是最短的方法:

cv::Rect r1=Rect(10,20,40,60);
cv::Rect r2(r1.tl() * 2, r1.br() * 2);

We can overload the * operator: 我们可以重载*运算符:

cv::Rect operator*(cv::Rect r, double scale) {
    r.height *= scale;
    r.width *= scale;
    r.x *= scale;
    r.y *= scale;
    return r;
}

And then you can multiply rectangles directly: 然后,您可以直接乘以矩形:

Rect r2 = Rect(10, 20, 40, 60) * 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM