Forum

Author Topic: Render image constrained to bounding box  (Read 5779 times)

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Render image constrained to bounding box
« 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.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Render image constrained to bounding box
« Reply #1 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.
Best regards,
Alexey Pasumansky,
Agisoft LLC

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #2 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?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Render image constrained to bounding box
« Reply #3 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")
Best regards,
Alexey Pasumansky,
Agisoft LLC

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #4 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 :)

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #5 on: May 15, 2018, 11:58:41 PM »
One other thing, Is there a way to change the rendering background colour?

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #6 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
  • chunk center Vector([-0.26232785948007087, 0.3646970820972305, -4.446110936333984])
  • Point Vector([-0.26232785948007087, 0.3646970820972305, -1.8394649697945527])

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

  • chunk center Vector([-0.08863683743844113, 1.0348724772492839, -3.6829016954626757])
  • Point Vector([-0.08863683743844113, 1.0348724772492839, -2.1615090949770757])

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?

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #7 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?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Render image constrained to bounding box
« Reply #8 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.
Best regards,
Alexey Pasumansky,
Agisoft LLC

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #9 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 :)

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #10 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.
« Last Edit: May 18, 2018, 08:14:31 PM by DaveRig »

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #11 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.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Render image constrained to bounding box
« Reply #12 on: June 06, 2018, 04:15:43 PM »
Hello DaveRig,

Sorry for the delay, give me some more time to think out the solution.
Best regards,
Alexey Pasumansky,
Agisoft LLC

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #13 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?

DaveRig

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Render image constrained to bounding box
« Reply #14 on: July 20, 2018, 07:04:22 PM »
Bump, still looking for a solution for this, taking all suggestions :)