Reputation: 4151
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)?
Upvotes: 1
Views: 1635
Reputation: 15018
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);
Upvotes: 1
Reputation: 10750
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;
Upvotes: 0