How to calculate distance in an image?

140 Views Asked by At

I am trying to programmatically simulate a zoom effect within a timelapse by cropping each photo in the series toward an object in the images. However, if I crop each photo by a certain number of pixels each time, the zoom effect will appear to accelerate the farther "into" the image I get.

Is there a formula that can calculate how many pixels I need to crop each photo to create a consistent zooming speed? What should happen is that each photo in the series should be cropped by a smaller and smaller number of pixels. I had some luck with the relationship of physical distance from the camera, focal length of the lens, and other known physical distances between objects within the photo, but was never able to get a consistent result in different scenes. It ends up being trial and error each time.

Since I am setting up the scenes, I can accurately measure anything I need to ahead of time if I can come up with a formula that uses those inputs to accurately calculate this motion effect afterward. I'm a newb to this forum so I can't post images yet but here is a link to one that I was playing with for reference.

https://photos.app.goo.gl/C44eVTQ3Dn5TtBRK8

Someone, please help me get on track.

Thanks!

1

There are 1 best solutions below

0
On

To expand on Jam’s suggestion, you could try successively cropping and rescaling the image by the same amount at each step. Now, I wouldn’t necessarily implement it this way because of the ever-increasing loss of image quality that would result, but you can get the same effect by using a geometric progression of crop sizes on the original image: if the final zoom factor desired is $z$ and the number of steps in the animation is $n$, then at the $k$th iteration you would crop the initial image down to $1/z^{k/n}$ of its original size and then rescale to its original dimensions. So, using your example image with $z=2$ and $n=12$, the original image dimensions are 1159x773, the first crop would be to 1094x730, the second to 1033x689, and so on down to 580x386.

You might need a bit of finesse in the size and placement of the crop frame to minimize image jitter at larger zoom amounts, but to my eye this method produces what looks like a constant approach speed toward the center of the zoom.