# Forum

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

#### Dragline

• Newbie
• Posts: 34
##### 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: 14927
##### Re: Define Bounding Box By Camera Locations
« Reply #1 on: December 17, 2018, 05:42:08 PM »
Hello Dragline,

Code: [Select]
`import PhotoScan, statisticsBUFFER = 10 #percentdef 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.chunknew_region = PhotoScan.Region()xcoord = PhotoScan.Vector([10E10, -10E10])ycoord = PhotoScan.Vector([10E10, -10E10])avg = [[],[]]T = chunk.transform.matrixs = chunk.transform.matrix.scale()crs = chunk.crsz = 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 cornershorizontal = side2vertical = side1normal = 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_centernew_region.size = new_sizechunk.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
##### 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
##### 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: 14927
##### 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
##### 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
##### 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.