Search code examples
c#rotationzoomingtranslationscale

How to scale up a rotated and translated image to fill a frame


In my C# application, I have a 1280x720 image (in blue on the image below). In the image is an object of interest (in yellow) whose center was originally at (objectCenterX, objectCenterY) and which was originally tilted at an angle (objectAngle in degrees).

The image was rotated and translated to straighten the object and bring it to the center of the frame (in red; also 1280x720) causing black regions to appear.

Now I am trying to figure out by how much (minimum) should I uniformly scale up (zoom in) the image around the object's center to get rid of the black regions (in other words, zoom to fill the red frame).

EDIT: I am interested in the computation of the required scale factor.

Rotated and Translated image around an object of interest. Scaling up is required.


Solution

  • After giving this problem some thoughts, I finally found the solution that involves finding the intersections between the frame diagonals and the edges of the transformed image (small circles on the image below). Note that depending on the transformation, some edges may not be intersected by the diagonals while some edges my be intersected twice.

    The distance from the center to the closest intersection (red circle) allows us to compute by how much to scale the transformed image to zoom in and cover the black areas.

    The tricky part came from the fact that in order to do the opposite and find the scale factor required to zoom out and fit the entire transformed image in the frame, you have to compute the distances from the center to the CORNERS of the transformed image (bounding box).

    This is what I had done at first and took the inverse of the scale thinking it would work. But I finally understood why it didn't.

    enter image description here