Agisoft Metashape
Agisoft Metashape => Python and Java API => Topic started by: Julian1919 on November 28, 2017, 07:54:45 PM
-
Hello, I‘m having a problem with the bounding box/ region. Is it possible to align the region with your markers? I have different models with markers always in the same locations and if i want to compare the models, they need to have the same bbox/ region alignment. It doesn’t really matter how they’re aligned as long as it is the same for every model. Is this possible or do I need a python script for that? If yes how would it look like; I don’t really speak python :-\
Thank you for your help
-
Hello Julian1919,
Some additional information would be required: how you are planning to define the box size/position/orientation? For example, should there be some kind of selection from the list of markers for the box origin, and same for the markers defining the corners of the box side?
Maybe you can post a screenshot or sketch, demonstrating the markers' positions and name those, that should or could be selected by the user?
-
The outer markers P1,P5,P6,P10 could be the upper corners of the box for example. But do u still need to define a marker for the origin then?
-
Hello Julian,
Please try the following script:
#compatibility PhotoScan Professional version 1.3.4
#sets up the bounding box according to four markers
import PhotoScan, math
CORNERS = ["P5", "P6", "P10", "P1"]
def vect(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()
def get_marker(label, chunk):
for marker in chunk.markers:
if marker.label.lower() == label.lower():
return marker
return None
doc = PhotoScan.app.document
chunk = doc.chunk #active chunk
region = chunk.region
if chunk.transform.matrix:
T = chunk.transform.matrix
s = chunk.transform.scale
else:
T = PhotoScan.Matrix().Diag([1,1,1,1])
s = 1
points2 = [get_marker(x, chunk).position for x in CORNERS]
new_region = chunk.region
new_center = (points2[0] + points2[1] + points2[2] + points2[3]) / 4.
side1 = points2[0] - points2[1]
side2 = points2[0] - points2[-1]
side1g = T.mulp(points2[0]) - T.mulp(points2[1])
side2g = T.mulp(points2[0]) - T.mulp(points2[-1])
new_size = PhotoScan.Vector([side2g.norm()/s , side1g.norm()/s, new_region.size.z])
horizontal = side2
vertical = side1
normal = vect(vertical, horizontal)
horizontal = -vect(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
print("Finished")
It will use the markers P5, P6, P10, P1 to define the origin of the box and vectors P5 -> P6, P5 -> P1 to define the sides of the box. The vertical size of the box would remain unchanged compared to the original box size.
Let me know which kind of modifications are required for the script, if it works more or less as expected.