Forum

Author Topic: Define Bounding Box By Camera Locations  (Read 4192 times)

Dragline

  • Newbie
  • *
  • Posts: 34
    • View Profile
Define Bounding Box By Camera Locations
« on: December 14, 2018, 09:04:31 PM »
I would like to define a bounding box based on the locations of where photos were taken. It would also be nice to add a buffer around the perimeter defined by the cameras. Does anyone have any idea how to approach this?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Define Bounding Box By Camera Locations
« Reply #1 on: December 17, 2018, 05:42:08 PM »
Hello Dragline,

Please try the following script:

Code: [Select]
import PhotoScan, statistics
BUFFER = 10 #percent

def cross(a, b):
result = PhotoScan.Vector([a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y *b.x])
return result.normalized()

chunk = PhotoScan.app.document.chunk
new_region = PhotoScan.Region()
xcoord = PhotoScan.Vector([10E10, -10E10])
ycoord = PhotoScan.Vector([10E10, -10E10])
avg = [[],[]]
T = chunk.transform.matrix
s = chunk.transform.matrix.scale()
crs = chunk.crs
z = PhotoScan.Vector([0,0])

for camera in chunk.cameras:
if camera.transform:
coord = crs.project(T.mulp(camera.center))
xcoord[0] = min(coord.x, xcoord[0])
xcoord[1] = max(coord.x, xcoord[1])
ycoord[0] = min(coord.y, ycoord[0])
ycoord[1] = max(coord.y, ycoord[1])
z[0] += coord.z
z[1] += 1
avg[0].append(coord.x)
avg[1].append(coord.y)
z = z[0] / z[1]
avg = PhotoScan.Vector([statistics.median(avg[0]), statistics.median(avg[1]), z])

corners = [PhotoScan.Vector([xcoord[0], ycoord[0], z]),
PhotoScan.Vector([xcoord[0], ycoord[1], z]),
PhotoScan.Vector([xcoord[1], ycoord[1], z]),
PhotoScan.Vector([xcoord[1], ycoord[0], z])]
corners = [T.inv().mulp(crs.unproject(x)) for x in list(corners)]

side1 = corners[0] - corners[1]
side2 = corners[0] - corners[-1]
side1g = T.mulp(corners[0]) - T.mulp(corners[1])
side2g = T.mulp(corners[0]) - T.mulp(corners[-1])
side3g = T.mulp(corners[0]) - T.mulp(PhotoScan.Vector([corners[0].x, corners[0].y, 0]))
new_size = ((100 + BUFFER) / 100) * PhotoScan.Vector([side2g.norm()/s, side1g.norm()/s, 3*side3g.norm() / s]) ##

xcoord, ycoord, z = T.inv().mulp(crs.unproject(PhotoScan.Vector([sum(xcoord)/2., sum(ycoord)/2., z - 2 * side3g.z]))) #
new_center = PhotoScan.Vector([xcoord, ycoord, z]) #by 4 corners

horizontal = side2
vertical = side1
normal = cross(vertical, horizontal)
horizontal = -cross(vertical, normal)
vertical = vertical.normalized()

R = PhotoScan.Matrix ([horizontal, vertical, -normal])
new_region.rot = R.t()

new_region.center = new_center
new_region.size = new_size
chunk.region = new_region

It should work for georeferenced chunk with some aligned cameras. The buffer extension (in percent) can be modified via the corresponding variable in the second line of the script.
« Last Edit: January 25, 2019, 01:27:47 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dragline

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Define Bounding Box By Camera Locations
« Reply #2 on: December 20, 2018, 02:23:06 AM »
Perfect! It works just as expected. Thanks for taking the time to put that together.

Dragline

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Define Bounding Box By Camera Locations
« Reply #3 on: December 20, 2018, 02:39:31 AM »
I made one minor modification that may be be helpful to others. On this line "z[0] += coord.z" I changed it to "z[0] += coord.z-1000" This ensures that the ground will be within the bounding box during reconstruction.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Define Bounding Box By Camera Locations
« Reply #4 on: December 20, 2018, 03:20:39 PM »
Hello Dragline,

In principle, the ground level can be estimated by checking the coordinates of the tie points (if they are present in the project), for example, checking every 100th to speed up the procedure and then taking the median value to filter the outliers.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dragline

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Define Bounding Box By Camera Locations
« Reply #5 on: December 20, 2018, 08:20:53 PM »
Alexey,

I think that would probably be a better way to do this. However my programming skills are no where near that level. I'm just a humble engineer.

pastouf

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Define Bounding Box By Camera Locations
« Reply #6 on: February 07, 2019, 01:59:52 PM »
Hi All,
I try to test this script but i dont understand how to apply this?

In fact, i want to define bounding box with 4 markers or 4 gps.
I have 20,000 photos and to save computing time and export only the area where I want to work on the point cloud. I would like to define a smallest possible calculation area for me and thus export a las file corresponding to my work area, but not the entire area.
I can work on lighter files and focus on my work area.

thank you in advance