9

I don't have a particular camera in mind right now, I'm just curious how this is done, programmatically/mathematically.

I have a 3D space, a rectangle, with a camera up in one corner looking inwards.
I have a moving object in that rectangle that's transmitting (x, y, z) coordinates of its current position.
I want to take those coordinates and translate them into instructions telling the camera to point at that position.
How is this translation typically done?

anonymous2
  • 4,902
  • 3
  • 22
  • 49
mal
  • 193
  • 1
  • 4

3 Answers3

10

Trigonometry !

My camera is a DLink 5020-L and has pan/tilt commands which can be given through an API. It also has predefined positions to set and can also be triggered through API

Pre-init

  • Define a position of your camera to a 0° Pan and a 0° Tilt in your referential => we will call this position Position 1

Init

  • Move your camera to Position 1
  • Store somewhere the pan/tilt of your camera, either in 0-initialized variables or through your API

Look at object

  • Locate your object in two planes, the X,Y and the Y,Z planes
  • You can get then the pan (left/right) angle (omg mathematics formulae in an IoT SE!)

$$\arctan\bigg(\frac{y}{x}\bigg)$$

  • You can get then the tilt (up/down) angle

$$\arctan\bigg(\frac{z}{y}\bigg)$$

  • Don't forget to save/update the new pan/tilt value since you might work with relative movement...

You might negate the previous results depending on how your camera is placed

(I'll add some schematics when I have time)

Goufalite
  • 3,776
  • 17
  • 33
6

Great answers already, I'd just like to add a few other things that you should take into consideration. Like hardlib and Goufalite have already mentioned, the way to do this is trigonometrically. I've drawn out a 2-d depiction of the camera and the IoT object:

enter image description here

As you can see, the camera's field of view is going to be larger than the object - if not in close range, when the object moves further away.

Now, you may want the camera always centred on the object. In that case, you can simply take the calculations that hardlib referenced:

ϴ = arctan(y/x)

...which will be the angle counterclockwise from the x-axis, per convention. You'll also need the angle away from level:

α = arctan(z / ((y^2+x^2)^1/2))

Obviously, you'll have to calculate based off of the camera position being at the origin in all three axes.

On the other hand, you may prefer to not make the camera move more than necessary, that is, to make the camera only move once the object appears to be about to move out of the frame. In that case, you'll probably want a "pressure" variable which will make the camera more likely to change its angle based on how close the object is to the edge of the frame.

If you go that route, you'll need to know the angle of the camera's field of view in both fields of view, so that you can determine where the object is compared to the camera's field of view.

anonymous2
  • 4,902
  • 3
  • 22
  • 49
5

This is normally done with basic trigonometry.

Start by working on a single 2d flat plane with the camera at the origin (0,0) and the object at (x,y)

Given that the x distance will be the adjacent side of the triangle and the y distance will be the opposite you get:

tan(angle) = y/x

so the pan angle can be found with

Angle = invTan(y/x)

You can also work out the straight line distance (the hypotenuses) between the camera and the object with:

h^2 = x^2 + y^2

Giving:

h = sqrt(x^2 + y^2)

Now you can use the h distance with the z height to calculate the tilt angle in the same way.

Once you have the angles you can feed these to what ever is controlling the pan/tilt on the camera.

hardillb
  • 12,813
  • 1
  • 21
  • 34