0

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.

Gilles jr Bisson
  • 449
  • 4
  • 11
  • Basically, you can't. What if the object is in the upper left corner? The image would be black for, let's say, 45% of its area. If you zoom that to elimination of black, it would basically fill the whole image. Would that be OK? – Fildor Jul 15 '23 at 05:53
  • And if the object is at 45° and really really tucked into one of the corners, it may even be impossible just by zooming. – Fildor Jul 15 '23 at 05:58
  • Is this for an industrial application? Does the background even matter? Or would it even be beneficial to get rid of all the background? – Fildor Jul 15 '23 at 06:02
  • @Fildor-standswithMods In practice, the object is a mark the camera is pointed at. It will be close to the center and not tilted much. So corner cases (pun intended) are of no concerns! And since the user will be looking at it, I do want to zoom-in just enough to get rid of the black areas. So I guess I am interested in the math involved to solve that problem. – Gilles jr Bisson Jul 15 '23 at 07:02
  • Ok. That's reasonable, I guess. I would start by ripping the problem in two: 1. How much to zoom if you only translate. 2. How much to zoom if you only tilt. Then last, I'd try and find out how to combine the two to find the optimal solution. – Fildor Jul 15 '23 at 07:09
  • ^^ case 1 is kind of trivial: zoom by max of translation.x and translation.y ... case 2 is a little more tricky but certainly doable with a little bit of trigonometry. – Fildor Jul 15 '23 at 08:17

2 Answers2

0

This is a lot simpler than we think.

Option 1 You could calculate your zoom scale before rotation. Rotate, then multiply your scale by Cos(objectAngle) (When your scale zooms in with lower number). You get minimum for angles smaller than 45'. It will become problematic for bigger than 60'.

Option 2 You need to have coordinates for each corner of blue rectangle. Imagine a circle containing our frame and its center at yellow object. If the radius of this circle is <= center distance to rectangle lines, we wont have black regions. This circle help discard rotation.

So calculate for each line.

distance = Math.Abs((lineX2-lineX1)*(lineY1-centerY)-(lineX1-centerX)*(lineY2-lineY1)) / Math.Sqrt((lineX2-lineX1)^2 + (lineY2-lineY1)^2) Source here

Use the minimum value of calculated distances for radius of circle. Diameter of circle should be equal to diameter of frame rectangle. This wont give you the minimum but you will never have black regions.

Option 3 Make something using both options with inspiration from this post.

  • Because the scale and rotation are not around the center, the Cos(objectAngle) did not work for me. And the solution involving measuring the distance from a point to a line did not work either because we don't want the shortest distances from the center to the edges. Rather we need to find intersection between the frame diagonals and the transformed image edges. See my solution below. – Gilles jr Bisson Jul 18 '23 at 14:09
0

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

Gilles jr Bisson
  • 449
  • 4
  • 11