Forum

Author Topic: unwanted z-shift in bounding box after transformation  (Read 11718 times)

jeremyeastwood

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
unwanted z-shift in bounding box after transformation
« on: May 24, 2015, 01:55:08 PM »
Hi,

I am using a python script to reset my model bounding box x and y locations and dimensions, as well as its z-rotation, while keeping the bounding box z extent and center the same.  The x and y shift, scaling and rotation work well, but the bounding box seems to change its z-location relative to the model (even though a console output of the box center z value is the same before and after the transformation).

For clarity, I have attached screenshots before and after applying the transformation, showing the unwanted shift in z-location of the bounding box (which transfers the bounding box away from the model, making further processing fail); you can also see the console which shows the region center values before and after the transformation, showing the z value remains the same.

My code is below:

Code: [Select]
import PhotoScan
import math

bbox_center = [-119.43639088933767, 36.57570587655139]  # deg - wgs84
bbox_size = [193.71339323337548, 99.76327818514577]  # m
bbox_rot =  180.58462719482992  # deg

doc = PhotoScan.app.document
chunk = doc.chunk
reg = chunk.region
trans = chunk.transform.matrix

print("region center: {}".format(reg.center))
print("region size: {}".format(reg.size))
print("region rot: {}".format(reg.rot))

newregion = PhotoScan.Region()
center_geo = PhotoScan.Vector([bbox_center[0], bbox_center[1], 0.])  # uses existing region height

v_temp = chunk.crs.unproject(center_geo)
v_temp.size = 4
v_temp.w = 1
centerLocal = chunk.transform.matrix.inv() * v_temp
centerLocal.size = 3
newregion.center = PhotoScan.Vector([centerLocal[0], centerLocal[1], reg.center[2]])  # uses existing region height
print("newregion center: {}".format(newregion.center))

# Set region size:
# generate scale factor
rot_untransformed = PhotoScan.Matrix().diag([1, 1, 1, 1])
rot_temp = trans * rot_untransformed
s = math.sqrt(rot_temp[0, 0] ** 2 + rot_temp[0, 1] ** 2 + rot_temp[0, 2] ** 2)

# scale desired size in metres to chunk internal coordinate system
inter_size = PhotoScan.Vector([0, 0, 0])
geo_size = PhotoScan.Vector([bbox_size[0], bbox_size[1], 0])  # uses original chunk region z size

inter_size = geo_size / s
newregion.size = PhotoScan.Vector([inter_size[0], inter_size[1], reg.size[2]])
print("newregion size: {}".format(newregion.size))

# Set region rotation
# build z-axis rotation matrix
SinRotZ = math.sin(math.radians(bbox_rot))
CosRotZ = math.cos(math.radians(bbox_rot))
RotMat = PhotoScan.Matrix([[CosRotZ, -SinRotZ, 0, 0], [SinRotZ, CosRotZ, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])

#  rotate region bounding box
v = PhotoScan.Vector([0, 0, 0, 1])
v_t = trans * v
v_t.size = 3
m = chunk.crs.localframe(v_t)
m = m * trans
m = RotMat*m
s = math.sqrt(m[0, 0]**2 + m[0, 1]**2 + m[0, 2]**2)  # scale factor
R = PhotoScan.Matrix([[m[0, 0], m[0, 1], m[0, 2]], [m[1, 0], m[1, 1], m[1, 2]], [m[2, 0], m[2, 1], m[2, 2]]])
R = R * (1. / s)
newregion.rot = R.t()
print("newregion rot: {}".format(newregion.rot))

# put newregion to chunk
chunk.region = newregion

Any ideas what I'm doing wrong and how to fix are greatly appreciated.

Thanks

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #1 on: May 24, 2015, 05:35:33 PM »
Hello Jeremy,

I think that you need to correctly define altitude and z-dimension when assigining center_geo and geo_size variables. Using zero as third component is not correct.
Best regards,
Alexey Pasumansky,
Agisoft LLC

jeremyeastwood

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #2 on: May 26, 2015, 08:04:59 PM »
Hi Alexey,

Thanks for the quick reply.  The reason I'm not currently setting those variables is that I do not know them explicitly beforehand (I do know the x-y center and extent, but want to leave the z dimensions the same).  What would you suggest setting the z values of the center_geo and geo_size variables?

I did try setting the z-dimension values to reasonable numbers but got the same result (bounding box below the model) - any ideas?

Thanks again for your help

jeremyeastwood

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #3 on: May 27, 2015, 01:48:24 AM »
Hi Alexey,

Any more ideas on this?  I've tried a few variations but can't get the bounding box to align properly in the z-direction.  Interestingly, this only happens occasionally with the models - the code I linked above works fine for 95% of the models I run.

Thanks

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #4 on: May 27, 2015, 11:38:33 AM »
Hello Jeremy,

Could you please send the project file (without dense cloud and mesh) to support@agisoft.com, I'll test it with the script.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #5 on: May 27, 2015, 01:30:26 PM »
Hello Jeremy,

In the script I've changed line 18:
Code: [Select]
center_geo = PhotoScan.Vector([bbox_center[0], bbox_center[1], 14.]) and put approximate ground level in meters as third component.

and also changes line 25:
Code: [Select]
newregion.center = PhotoScan.Vector([centerLocal[0], centerLocal[1], centerLocal[2]]) so that third component is taken from the geo-coordinates recalculation, since originally z-value of the existing bounding box has been used.
Best regards,
Alexey Pasumansky,
Agisoft LLC

jeremyeastwood

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #6 on: May 28, 2015, 01:55:41 AM »
Thanks Alexey - worked great!

Seems as though the issue is that for some models, the estimated ground level differs from the 'real world' ground level, which should always be centered on 0 (these are aerial images and the exif altitude is set as the height above the ground).  This is probably due to poor image quality / overlap leading to large errors in model height estimation (?).

Is there a way of generalising this process, i.e. obtaining the correct center (which in this case was 14m) for the bounding box from the sparse cloud / original region center, which can then be used when adjusting the bounding box x and y extents?  Ideally I'd like to automate the entire procedure.

Thanks again

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #7 on: May 28, 2015, 01:23:20 PM »
Hello Jeremy,

If you are confident in the center of the existing bounding box, you can use it for z estimation.

Alternatively you can check the coordinates of the sparse cloud points or, if the flight altitude is known, subtract it from the average camera altitude. In this case the altitude of the center of the new region will be approximately on the ground level.
Best regards,
Alexey Pasumansky,
Agisoft LLC

jeremyeastwood

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: unwanted z-shift in bounding box after transformation
« Reply #8 on: May 28, 2015, 08:33:46 PM »
Thanks Alexey - great suggestions; all working nicely now.

n.b. for anyone wanting to do something similar, the code to retrieve the original bounding box center (again thanks to Alexey for providing this) is:

Code: [Select]
old_center = chunk.crs.project(chunk.transform.matrix.mulp(reg.center))
Jeremy