简体   繁体   中英

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

Let's say I have 2 rectangle. I want the second rectangle to be twice bigger than the first rectangle and the position of x,y also twice bigger.

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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