Scaling a Rect

asked 2013-06-14 03:23:33 -0600

zerog80 gravatar image

Hi,

I was trying to scale a Rect relatively to axis origin (0,0) by a fixed factor, and I was expecting that this would work, for example:

float factor = 1.75f;
Rect originalRect(10, 15, 100, 200);
Rect scaledRect = originalRect * factor;

but the binary * operator is not defined for Rect and the compiler correctly reports an error.

I had to manually scale each coordinate:

Rect scaledRect;
scaledRect.x = originalRect.x * factor;
scaledRect.y = originalRect.y * factor;
scaledRect.width = originalRect.width * factor;
scaledRect.height = originalRect.height * factor;

which takes a lot more lines of code ;)

Is this simple addition to Rect class planned to be implemented in a future OpenCV version?

edit retag flag offensive close merge delete

Comments

1

Well surely it doesn't exist at present. Also, I think in the above code, in addition to scaling you are also moving the position of Rect.

vinayverma gravatar imagevinayverma ( 2013-06-14 04:32:53 -0600 )edit

I agree with him, you should keep the original 0,0 coordinates if you want a solution to your problem stated above.

 scaledRect.x = originalRect.x;
 scaledRect.y = originalRect.y;
StevenPuttemans gravatar imageStevenPuttemans ( 2013-06-14 04:54:50 -0600 )edit
1

Yes, that was intended, in fact I stated "relatively to axis origin (0,0)", because I am scaling the whole coordinate system because of a camera calibration factor (px/mm). If I wanted to scale the Rect relatively to its top-left corner I should have done as Steven suggested.

zerog80 gravatar imagezerog80 ( 2013-06-14 10:01:16 -0600 )edit