I think this is the best place for this question.
I am trying to get the heading and pitch of any clicked point on an embedded Google Street View.
The only pieces of information I know and can get are:
- The field of view (degrees)
- The center point's heading and pitch (in degrees) and x and y pixel position
- The x and y pixel position of the mouse click
I've included here a screenshot with simplified measurements as an example:
I initally just thought you could divide the field of view by the pixel width to get degrees per pixel, but it's more complicated, I think it has to do with projecting onto the inside of a sphere, where the camera is at the centre of the sphere?
Bonus if you can tell me how to do the reverse too...
Clarification: The goal is not to move the view to the clicked point, but give information about a clicked point. The degrees per pixel method doesn't work because the viewport is not linear.
THe values I have here are just examples, but the field of view can be bigger or smaller (from [0.something, 180], and the center is not fixed, it could be any value in the range [0, 360] and vertically [-90, 90]. The point [0, 0] is simply the heading (horizontal degrees) and pitch (vertical degrees) of the photogapher when the photo was taken, and doesn't really represent anything.
This answer is unprecise, have a look at most recent answer of user3146587.
I'm not very good at mathematical explanations. I've coded an example and tried to explain the steps in the code. As soon as you click on one point in the image, this point becomes the new center of the image. Even though you have explicitly not demanded for this, this is perfect for illustrating the effect. The new image is drawn with the previously calculated angle.
Example: JSFiddle
The important part is, that I use the radian to calculate radius of the "sphere of view". The radian in this case is the width of the image (in your example 100)
With the radian, radius and the relative position of the mouse position I can calculate the degree that changes from the center to the mouse position.
When the relative mouse position is 25 the radian used for the calculation of the horizontal angle is 50.
See this image for the further process:
I can calculate the new heading and pitch when I add/subtract the calculated angle to the actual angle (depending on left/right, above/under). See the linked JSFiddle for the correct behavior of this.
Doing the reverse is simple, just do the listed steps in the opposite direction (the radius stays the same).
As I've already mentioned, I'm not very good at mathematical explanations, but don't hesitate to ask questions in the comments.
Martin Matysiak wrote a JS library that implements the inverse of this (placing a marker at a specific heading/pitch). I mention this as the various jsfiddle links in other answers are 404ing, the original requestor added a comment requesting this, and this SO page comes up near the top for related searches.
The blog post discussing it is at https://martinmatysiak.de/blog/view/panomarker.
The library itself is at https://github.com/marmat/google-maps-api-addons.
There's documentation and demos at http://marmat.github.io/google-maps-api-addons/ (look at http://marmat.github.io/google-maps-api-addons/panomarker/examples/basic.html and http://marmat.github.io/google-maps-api-addons/panomarker/examples/fancy.html for the PanoMarker examples).
TL;DR: JavaScript code for a proof of concept included at the end of this answer.
The heading and pitch parameters
h0
andp0
of the panorama image corresponds to a direction. By using the focal lengthf
of the camera to scale this direction vector, one can get the 3D coordinates(x0, y0, z0)
of the viewport center at(u0, v0)
:The goal is now to find the 3D coordinates of the point at to some given pixel coordinates
(u, v)
in the image. First, map these pixel coordinates to pixel offsets(du, dv)
(to the right and to the top) from the viewport center:Then a local orthonormal 2D basis of the viewport in 3D has to be found. The unit vector
(ux, uy, uz)
supports the x-axis (to the right along the direction of increasing headings) and the vector(vx, vy, vz)
supports the y-axis (to the top along the direction of increasing pitches) of the image. Once these two vectors are determined, the 3D coordinates of the point on the viewport matching the(du, dv)
pixel offset in the viewport are simply:And the heading and pitch parameters
h
andp
for this point are then:Finally to get the two unit vectors
(ux, uy, uz)
and(vx, vy, vz)
, compute the derivatives of the spherical coordinates by the heading and pitch parameters at(p0, h0)
, and one should get:where
sgn( a )
is+1
ifa >= 0
else-1
.Complements:
The focal length is derived from the horizontal field of view and the width of the image:
The reverse mapping from heading and pitch parameters to pixel coordinates can be done similarly:
(x, y, z)
of the direction of the ray corresponding to the specified heading and pitch parameters,(x0, y0, z0)
of the direction of the ray corresponding to the viewport center (an associated image plane is located at(x0, y0, z0)
with an(x0, y0, z0)
normal),du
anddv
du
anddv
to absolute pixel coordinates.In practice, this approach seems to work similarly well on both square and rectangular viewports.
Proof of concept code (call the
onLoad()
function on a web page containing a sized canvas element with a "panorama" id)Here is an attempt to give a mathematical derivation of the answer to your question.
Note: Unfortunately, this derivation only works in 1D and the conversion from a pair of angular deviations to heading and pitch is wrong.
Notations:
f
: focal length of the camerah
: height in pixels of the viewportw
: width in pixels of the viewportdy
: vertical deviation in pixels from the center of the viewportdx
: horizontal deviation in pixels from the center of the viewportfov_y
: vertical field of viewfov_x
: horizontal field of viewdtheta_y
: relative vertical angle from the center of the viewportdtheta_x
: relative horizontal angle from the center of the viewportGiven
dy
, the vertical offset of the pixel from the center of the viewport (this pixel corresponds to the green ray on the figure), we are trying to finddtheta_y
(the red angle), the relative vertical angle from the center of the viewport (the pitch of the center of the viewport is known to betheta_y0
).From the figure, we have:
so:
and finally:
This is the relative pitch angle for the pixel at
dy
from the center of the viewport, simply add to it the pitch angle at the center of the viewport to get the absolute pitch angle (i.e.theta_y = theta_y0 + dtheta_y
).similarly:
This is the relative heading angle for the pixel at
dx
from the center of the viewport.Complements:
Both relations can be inverted to get the mapping from relative heading / pitch angle to relative pixel coordinates, for instance:
The vertical and horizontal fields of view
fov_y
andfov_x
are linked by the relation:so:
The vertical and horizontal deviations from the viewport center
dy
anddx
can be mapped to absolute pixel coordinates:Proof of concept fiddle