Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: DaveRig on April 25, 2018, 06:21:30 PM

Title: Render image constrained to bounding box
Post by: DaveRig on April 25, 2018, 06:21:30 PM
As part of a python batch script I'd like to render a image of the completed mesh with the camera facing down -X (Front) and -Z (Top) and zoomed in to the bounding box so the mesh is centered and fills the frame .

I'm using the "renderImage" command which works but am not sure how to figure out the camera transform to position the camera as described.

Does anyone know or can anyone point me at a example of how to  calculate the camera transform and sensor calibration?
My mesh is always centered at world 0 on X,Y. Z can vary with the object's height.
Title: Re: Render image constrained to bounding box
Post by: Alexey Pasumansky on April 25, 2018, 07:36:23 PM
Hello DaveGig,

You can refer to the following script that makes the mesh render for each aligned camera:
https://github.com/agisoft-llc/photoscan-scripts/blob/master/src/render_photos_for_cameras.py

The parameters to renderImage() function should be passed in internal coordinate system, if your chunk if referenced or transformed.
Title: Re: Render image constrained to bounding box
Post by: DaveRig on April 25, 2018, 08:29:59 PM
I found a script similar to this which got me to the point of rendering from a aligned camera, but my issue is that I want to render from a point in space which frames the bounding box in the frustum that isn't a aligned camera.

Is it possible to do this?
Title: Re: Render image constrained to bounding box
Post by: Alexey Pasumansky on April 25, 2018, 09:11:43 PM
Hello DavidRig,

I think you can try something like the following (just a quick test for TopXY render for unreferenced models):

Code: [Select]
chunk = PhotoScan.app.document.chunk
point = chunk.region.center + PhotoScan.Vector([0,0, chunk.region.size.z * 3])

T = PhotoScan.Matrix([[1,0,0, point.x], [0,-1,0, point.y], [0,0,-1, point.z], [0,0,0,1]])

image = chunk.model.renderImage(T, chunk.sensors[0].calibration)
image.save("D:/render.jpg")
Title: Re: Render image constrained to bounding box
Post by: DaveRig on April 25, 2018, 10:29:43 PM
Awesome !!!!
this does exactly that I needed. Now just fiddling with the parm's for different cam locations

Thanks :)
Title: Re: Render image constrained to bounding box
Post by: DaveRig on May 15, 2018, 11:58:41 PM
One other thing, Is there a way to change the rendering background colour?
Title: Re: Render image constrained to bounding box
Post by: DaveRig on May 16, 2018, 12:41:09 AM
Actually a bigger issue is I'm getting random directions the images are generated from.
I added prints to see the locations and they are similar between renders but almost 180 around the mesh
This is the script section I'm using:
Code: [Select]
point = chunk.region.center + PhotoScan.Vector([0,0, chunk.region.size.z * 2])
print ('chunk center ' + str(chunk.region.center))
print ('Point ' + str(point))

T = PhotoScan.Matrix([[1,0,0, point.x], [0,-1,0, point.y], [0,0,-1, point.z], [0,0,0,1]])

image = chunk.model.renderImage(T, chunk.sensors[0].calibration)
image.save(fileName)

For file 1 the image is rendered looking at the front of the model and I get these values

and for the 2nd file the image is rendered from behind and the left, about 130 Deg around clockwise with these values


All the camera are in the same location as they are statically mounted and both meshes have been aligned using markers so they are in the same world space.

any thoughts as whats going on?
Title: Re: Render image constrained to bounding box
Post by: DaveRig on May 16, 2018, 09:03:32 PM
So I think I found the problem, It seems the problem is local pivot of the mesh.
So this script is pushing the camera position back along the X axix but that is not always aligned with the front of the model.

Is there a way in script to reset the meshs pivot to align it to the world without effecting the mesh?
Title: Re: Render image constrained to bounding box
Post by: Alexey Pasumansky on May 16, 2018, 09:34:54 PM
Hello DaveRig,

Can you please clarify, how the script should estimate where's the "front" of the model?

The easiest options are to use region (like in the provided script version) or the coordinate system orientation.

Performing some calculations based on the coordinates of the mesh vertices is also possible, but could take longer time for high-poly meshes.
Title: Re: Render image constrained to bounding box
Post by: DaveRig on May 16, 2018, 10:24:25 PM
I have the model aligned to be facing down +X using markers so if the camera was at 2,0,0 world space and facing down -X it should be looking at the front of the model.

Using the script you supplies seemed to work and the location is based off the region but it seems when adding the PhotoScan.Vector([0,0, chunk.region.size.z * 2]) it is pushing the point away from the center of the region relative to the models pivot and not the world pivot.

I attached a image on the top image you can see the blue camera is where I expect it to end up using the script. The red camera is where it actually ends up.
In the bottom image I have reset the object transform so I could pick the "Rotate Object" mode and used the "Align pivot to bounding box" script to move the mesh back to world 0 for easy viewing, but in Rotate object mode you can see the pivot of the mesh lines up with where the camera is, I tried this with a different file and found the same results. So it seems it's using the mesh's pivot to determine the vector.

So my option are set a location in world space as the meshes will always be at the origin and facing +X or reset the pivot of the mesh, then the script you provided will work.

Thanks :)
Title: Re: Render image constrained to bounding box
Post by: DaveRig on May 18, 2018, 06:53:50 PM
To be more specific it seems when getting the initial point
Code: [Select]
point = chunk.region.center + PhotoScan.Vector([0,0, chunk.region.size.z * 2])Even though the location is being pulled from the regions location it seems it's pivot orientation is coming from the mesh.
If I translate the region around the render image changes accordingly, but rotating the region has no effect.

So when adding the vector "0,0,chunk.region.size.z * 2" it is moving the camera away from the mesh along what looks like +X in relative to the pivot of the mesh and not the region. So if I could reset the mesh's pivot or have the vector addition be relative to the region's pivot then I could place the render location to where I need it to be.
Title: Re: Render image constrained to bounding box
Post by: DaveRig on June 05, 2018, 09:31:15 PM
*Bump*
Any idea's on how to resolve this issue?
It's so close to working, just needs the rotations fixed.
Title: Re: Render image constrained to bounding box
Post by: Alexey Pasumansky on June 06, 2018, 04:15:43 PM
Hello DaveRig,

Sorry for the delay, give me some more time to think out the solution.
Title: Re: Render image constrained to bounding box
Post by: DaveRig on June 14, 2018, 10:40:41 PM
Anyone have any ideas on a solution to my issue?
Are there any python commands that cam change the orientation of the mesh's pivot without moving the mesh?
Title: Re: Render image constrained to bounding box
Post by: DaveRig on July 20, 2018, 07:04:22 PM
Bump, still looking for a solution for this, taking all suggestions :)
Title: Re: Render image constrained to bounding box
Post by: vizav on June 21, 2021, 03:58:46 PM
Hello DaveRig,
Were you able to resolve this. I am stuck at the exact same place too.