# Scale a RECT from its center ?

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 close merge delete

Sort by ยป oldest newest most voted

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!

more

Official site

GitHub

Wiki

Documentation

## Stats

Seen: 4,946 times

Last updated: Jun 06 '13