Ask Your Question

Scale a RECT from its center ?

asked 2013-06-02 06:37:14 -0500

yes123 gravatar image

updated 2013-06-02 06:52:21 -0500

I have a

Rect ROI(tl,br);

Having for example scale = 0.5 How can I calc a new ROI that it's area is 0.5 of the original area while the center is the same?

I have wrote a function:

void scaleROI( Point2f& tl,Point2f& br, const Point2f& queryCenter, float scale  ) {

    //> TopLeft
    float shiftX = abs(queryCenter.x -tl.x);
    float shiftY = abs(queryCenter.y -tl.y);

    shiftX = shiftX * scale;
    shiftY = shiftY * scale;

    tl.x = queryCenter.x - shiftX;
    tl.y = shiftY + queryCenter.y;

    //> BottomRight

    shiftX = abs(br.x - queryCenter.x);
    shiftY = abs(br.y - queryCenter.y);

    shiftX = shiftX * scale;
    shiftY = shiftY * scale;

    br.x = queryCenter.x + shiftX;
    br.y = queryCenter.y - shiftY;


This calc a new topLeft and bottomRight point by scaling the shift between these points and the center.

I would like to know if there is a built in, or a better way to do it.

Sadly, ROI * scale isn't allowed

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-06 01:39:06 -0500

Mahdi gravatar image

updated 2013-06-06 02:52:17 -0500

I don't think that ROI * scale is implemented in opencv, but here are some notes:

  • It is a good idea to write a modular function for this task but a better interface would be scaleROI(Rect& sampleRect, float scale) and do other computations like center in the function.
  • There is some thing that should get corrected in your implementation: (Remember top left image starts with (0, 0)):
    tl.y = queryCenter.y - shiftY;
    br.y = queryCenter.y + shiftY;

  • If you are familiar with operator overloading in CPP, It may be a better idea to implement '*' for cv::Rect class!

edit flag offensive delete link more

Question Tools


Asked: 2013-06-02 06:37:14 -0500

Seen: 5,441 times

Last updated: Jun 06 '13