Forum

Author Topic: resize bounding box to sparse point cloud  (Read 22783 times)

Milderinne

  • Newbie
  • *
  • Posts: 34
    • View Profile
resize bounding box to sparse point cloud
« on: August 11, 2017, 11:55:25 PM »
Hello everybody,
is there any solution to adjust/resize the bounding box to the complete extent of the sparse point cloud before caculation the dense point cloud?
Thanks for any help!
All the best,
Susann

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15124
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #1 on: August 14, 2017, 09:43:00 AM »
Hello Susann,

It may be a not good idea, since there could be outlier points that are far away from the main point cloud.

However, for your task you can loop through all the points of the sparse cloud, check their coordinates and assign chunk.region size and origin to fit min and max values of the point coordinates.

It may be easier, if you set up the box sides orientation parallel to the coordinate system axis. (also note that the coordinates of the points and chunk region are in the internal coordinate system, so for the referenced chunks you may need to convert the coordinates to the real coordinate system values).
Best regards,
Alexey Pasumansky,
Agisoft LLC

Milderinne

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #2 on: August 14, 2017, 11:07:15 AM »
Hello Alexey,
thank you for your fast reply (as always)!

I understand the problem of outliers, I also thought about that before.

I can imagine to loop through the sparse point cloud points, but how do I check their coordinates? How do I than assign chunk.region size and origin. Is there something like min max?

At this point or for this specific idea I am not able to assign coordinates at this moment. Am I still able to grow the the extent of the bounding box?

Thank you so much for your help!
Kind regrads.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15124
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #3 on: August 14, 2017, 12:20:55 PM »
Hello Susann,

You can use the following code as a reference (it should work for georeferenced chunks:

Code: [Select]
import PhotoScan, math

doc = PhotoScan.app.document
chunk = doc.chunk
region = chunk.region
T = chunk.transform.matrix

m = PhotoScan.Vector([10E+10, 10E+10, 10E+10])
M = -m

for point in chunk.point_cloud.points:
if not point.valid:
continue
coord = T * point.coord
coord.size = 3
coord = chunk.crs.project(coord)
for i in range(3):
m[i] = min(m[i], coord[i])
M[i] = max(M[i], coord[i])

center = (M + m) / 2
size = M - m

region.center = T.inv().mulp(chunk.crs.unproject(center))
region.size = size * (1 / T.scale())

v_t = T * PhotoScan.Vector( [0,0,0,1] )
v_t.size = 3
R = chunk.crs.localframe(v_t) * T
region.rot = R.rotation().t()

chunk.region = region
print("Script finished.")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Milderinne

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #4 on: September 04, 2017, 05:55:45 PM »
Hello Alexey,
thank you for your reply! Sorry for the late reply on my side.

You say the code work for georeferenced chunks? But my chunk is not yet georeferenced and might never be? Will it still work?

Best regards!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15124
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #5 on: September 04, 2017, 06:54:05 PM »
Hello Susann,

For the unreferenced chunks it should be shorter:
Code: [Select]
import PhotoScan, math

doc = PhotoScan.app.document
chunk = doc.chunk
region = chunk.region
T = chunk.transform.matrix

m = PhotoScan.Vector([10E+10, 10E+10, 10E+10])
M = -m

for point in chunk.point_cloud.points:
if not point.valid:
continue
coord = T * point.coord
for i in range(3):
m[i] = min(m[i], coord[i])
M[i] = max(M[i], coord[i])

center = (M + m) / 2
size = M - m
region.center = T.inv().mulp(center)
region.size = size * (1 / T.scale())

region.rot = T.rotation().t()

chunk.region = region
print("Script finished.")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Milderinne

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #6 on: September 05, 2017, 12:14:07 PM »
Thank you, I will try it.

Milderinne

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #7 on: October 04, 2017, 06:38:57 PM »
Thank you, it works fine!

Milderinne

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #8 on: October 25, 2017, 11:13:31 AM »
Hello Alexey,
I had some time to experiment with different projects automatically resizing the bounding box...
Of course, now the problem arises that I also include far away points making the bounding box unnecessary big. It there a way to restrict the bounding box like to the most dense points or so?
I would leave that bounding box thing out, but I had some cases where it really cuttet my point cloud which I still needed.
Any suggestions?
Best regards.

Milderinne

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #9 on: November 01, 2017, 06:43:33 PM »
I wonder if anyone else had the same problems and How they solved it?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15124
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #10 on: November 02, 2017, 11:49:52 AM »
Hello Milderinne,

You can create new list of points and filter out outliers in it, before using this list of points for the bounding box estimation, for example.

Alternatively, when checking the points' coordinates you can skip the points that a away from the default region center more than twice of the box size.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Seboon

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #11 on: January 05, 2018, 04:15:40 PM »
Hello Susann,

You can use the following code as a reference (it should work for georeferenced chunks:

Code: [Select]
import PhotoScan, math

doc = PhotoScan.app.document
chunk = doc.chunk
region = chunk.region
T = chunk.transform.matrix

m = PhotoScan.Vector([10E+10, 10E+10, 10E+10])
M = -m

for point in chunk.point_cloud.points:
if not point.valid:
continue
coord = T * point.coord
coord.size = 3
coord = chunk.crs.project(coord)
for i in range(3):
m[i] = min(m[i], coord[i])
M[i] = max(M[i], coord[i])

center = (M + m) / 2
size = M - m

region.center = T.inv().mulp(chunk.crs.unproject(center))
region.size = size * (1 / T.scale())

v_t = T * PhotoScan.Vector( [0,0,0,1] )
v_t.size = 3
R = chunk.crs.localframe(v_t) * T
region.rot = R.rotation().t()

chunk.region = region
print("Script finished.")

Hello,

Would it be possible to get the same script but working with model faces?
I've tried myself to create it, but got  : "Error: matrix * vector: vector length does not match matrix row size"
See the script attached.
Any help would be welcome!
S.Poudroux
Archaeologist - Topographer - Drone remote pilot

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15124
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #12 on: January 05, 2018, 05:36:54 PM »
Hello Seboon,

Please check this one:

Code: [Select]
import PhotoScan
doc = PhotoScan.app.document
chunk = doc.chunk
crs = chunk.crs
region = chunk.region
T = chunk.transform.matrix

m = PhotoScan.Vector([10E+10, 10E+10, 10E+10])
M = -m

for point in chunk.model.vertices:

coord = T.mulp(point.coord)
coord = chunk.crs.project(coord)
for i in range(3):
m[i] = min(m[i], coord[i])
M[i] = max(M[i], coord[i])

center = (M + m) / 2.
side1g = crs.unproject(M) - crs.unproject(PhotoScan.Vector([m.x, M.y, M.z]))
side2g = crs.unproject(M) - crs.unproject(PhotoScan.Vector([M.x, m.y, M.z]))
side3g = crs.unproject(M) - crs.unproject(PhotoScan.Vector([M.x, M.y, m.z]))
size = PhotoScan.Vector([side2g.norm() , side1g.norm(), side3g.norm()])


region.center = T.inv().mulp(crs.unproject(center))
region.size = size * (1 / T.scale())

v_t = T.mulp(region.center)
R = crs.localframe(v_t) * T
region.rot = R.rotation().t()

chunk.region = region
print("Script finished.")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Seboon

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #13 on: January 05, 2018, 07:32:30 PM »
Thanks Alexey,

I've switched your script in line 23 to:

size = PhotoScan.Vector([side1g.norm() , side2g.norm(), side3g.norm()])

Works like a charm, great!!
 
However, could it be possible to fit the resulted Bbox oriented to the mesh, like your "bounding_box_to_coordinate_system" script does ?
« Last Edit: January 05, 2018, 08:10:19 PM by Seboon »
S.Poudroux
Archaeologist - Topographer - Drone remote pilot

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15124
    • View Profile
Re: resize bounding box to sparse point cloud
« Reply #14 on: January 11, 2018, 04:14:21 PM »
Hello Seboon,

It is possible to modify the box rotation (orientation), but you need to suggest some logic that will be calculating the rotation angles.
Best regards,
Alexey Pasumansky,
Agisoft LLC