How to get the camera intrinsics matrix?

104 views
Skip to first unread message

Austin Stone

unread,
Nov 9, 2022, 1:34:02 PM11/9/22
to VPython-users
Hello,

I'm interested in getting the camera intrinsics matrix such that I can calculate the pixel coordinates of an object from it's world coordinates. I'm not sure how to access the focal length of the camera and I also don't know the "pixels per meter" in order to compute the matrix. Is there some utility for this? 

I'm able to get the extrinsic matrix from the  `scene.camera.pos` and the `scene.camera.axis`, I think. 

Thanks!

- Austin

Bruce Sherwood

unread,
Nov 9, 2022, 2:49:31 PM11/9/22
to VPython-users
The only information provided by VPython is described here:


I don't know whether this is sufficient for your needs, but it's all there is. Note that there is a (little-used?) "pixel_to_world" attribute that may be relevant.

Bruce

Austin Stone

unread,
Nov 9, 2022, 3:41:18 PM11/9/22
to VPython-users
It appears the `pixel_to_world` attribute is always 0 for me in the scripts I have tried. Do you know why this is? The below script prints out 0 for pixel_to_world.

```
import vpython as vp

HEIGHT = 600
# The focal length.
scene = vp.canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable
#scene.autoscale = False
scene.height = scene.width = HEIGHT
scene.background = vp.color.black
scene.fov = vp.pi / 3.
scene.ambient = 0.5 * vp.color.white
box_pos = (1, 2, 3)
b = vp.box(pos=vp.vec(*box_pos), color=vp.color.red)
print(scene.pixel_to_world)
```

Bruce Sherwood

unread,
Nov 9, 2022, 5:34:16 PM11/9/22
to VPython-users
It turns out that pixel_to_world is implemented in Web VPython but not in the vpython module which, as you say, returns zero. This wasn't noticed because you might be the first person to try to use it.

That's the bad news. The good news is that here is the JavaScript pixel_to_world function, which you can easily emulate::

           // Convert number of pixels into distance in real-world coordinates
                var w = this.__width    // e.g. scene.width
                var h = this.__height   // e.g. scene.height
                var d = 2*this.range   // e.g 2*scene.range
                if (w >= h) {
                    return d/h
                } else {
                    return d/w
                }

Note that the scene.camera functions in that Help page may also be useful to you.

Bruce
Reply all
Reply to author
Forward
0 new messages