I am new to 3d graphics. I have been creating scenes in blender and rendering them. Knowing the world locations of the object and camera, as well as the focal length of the lens, I would like to calculate the x1 and x2 ( or x and y) location of the object in the rendered 540x480 image. I would like to model blender's camera as a pinhole camera
I would also like to create a python script to do this for me for many images and various objects in the scene
Thank you!
I have been googling camera matrix, 3D projection and camera models. However, although the equations make sense on their own, I am having a hard time really grasping the way they work and applying them to my problem.
A very simple scene I am working on now is a camera pointing at a cube. I know the location of both the camera and the cube (center of it) with respect to the same world coordinates and I know the focal length of the camera. Knowing this information how can I Solve for the location of the cube in the photo that is rendered by the camera?
Camera : Vector((7.358891487121582, -6.925790786743164, 4.958309173583984)) Quaternion((0.7804827094078064, 0.483536034822464, 0.20870360732078552, 0.33687159419059753))
Cube: Vector((0.0, 0.0, 0.0)) Quaternion((1.0, 0.0, 0.0, 0.0))
I know that I need to make the camera the origin for i would just have to do x-x10, x-x20, and x-x30 ( where x10, x20, and x30 are the camera's location)
So far I have seen Y = C x where Y= [y1; y2;1 ], x= [x1-x10; x2-x20; x3-x30;1] and C =[f 0 0 0;0 f 0 0; 0 0 1 0]
I know that Y is suppose to be the object's location in 2d picture, x is the location of the object in 3d space and C is the camera matrix.
Plugging in all of my numbers I get Y= [-.3679;0.3463;4.9583]. I do not think this is right, and if it were I do not know how these numbers would correspond to my 540x480 image
To get the coordinates of the objects, I am using
bpy.data.objects["Camera"].location
bpy.data.objects["Camera"].matrix_world.to_quaternion()
bpy.data.objects["Cube"].location
bpy.data.objects["Cube"].matrix_world.to_quaternion()
I expect an output of the 2 numbers that correspond and match with the location of the object in the 2d picture.