Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: Milderinne on October 23, 2017, 06:32:00 PM

Title: Rotate chunk by the orientation of markers
Post by: Milderinne on October 23, 2017, 06:32:00 PM
Hello everybody,
I would be happy to get some help with the following issue:

There is this idea to rotate a chunk by the orientation of markers.
Meaning that I have 3 "double or paired markers", where one marker is always up and the other one down. My point cloud is not georeferenced but scaled. Often it is upside down or somehow oblique. I want to rotate it by saying marker[0] should be up and marker [1] down and so on.
I am not quite sure about, how to do that at the moment.

Furthermore is it possible to "call" coded markers not by marker[0] and so on, but by its label? With label I mean the name of the marker assigned after using:
Code: [Select]
chunk.detectMarkers(type = PhotoScan.TargetType.CircularTarget12bit, tolerance = tolerance)
Any help is welcome.
Best regards!
Title: Re: Rotate chunk by the orientation of markers
Post by: Alexey Pasumansky on October 24, 2017, 11:09:11 AM
Hello Milderinne,

Can you give an example? You wish to specify two pairs of markers according to their labels to set up the coordinate system rotation - would that we enough for the script?
Title: Re: Rotate chunk by the orientation of markers
Post by: Milderinne on October 24, 2017, 12:26:40 PM
Hello Alexey,

I have 3 paired coded markers. And I can refer to them by the following:
Code: [Select]
chunk.markers[0], chunk.markers[1]
But I know that they have "labels"(?), for example: target13 - target14. How do I refer to them with their labels?
Or is there something I donĀ“t get correctly?

Then, I would like to set the coordinate system rotation by saying something like:
target13 should be upwards and target14 should be perpendicular to it (downwards). Everything should be rotated in that way. Is that possible?
Best regards.
Title: Re: Rotate chunk by the orientation of markers
Post by: Milderinne on November 01, 2017, 06:42:26 PM
Any suggestions?
Title: Re: Rotate chunk by the orientation of markers
Post by: Alexey Pasumansky on November 02, 2017, 11:37:28 AM
Hello Milderinne,

The following script sample takes two pairs of markers (defined by their labels in the script body) and rotates the bounding box and coordinate system accordingly. The first pair defines vertical direction, the second pair - horizontal direction (OX axis).

In the attached example you can see the relative position of the markers used and the result orientation of the axis applied by the script.

Code: [Select]
import PhotoScan, math

def vect(a, b):
"""
Normalized vector product for two vectors
"""

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):
"""
Returns marker instance from chunk based on the label correspondence
"""

for marker in chunk.markers:
if label == marker.label:
return marker
print("Marker not found! " + label)
return False

chunk = PhotoScan.app.document.chunk
region = chunk.region

vertical = ["point 2", "point 1"]
horizontal = ["point 3", "point 4"]
vertical = get_marker(vertical[0], chunk).position - get_marker(vertical[1], chunk).position
horizontal = get_marker(horizontal[0], chunk).position - get_marker(horizontal[1], chunk).position


normal = vect(horizontal, vertical)
vertical = - vertical.normalized()
horizontal = vect(vertical, normal)

R = PhotoScan.Matrix ([horizontal, -normal, vertical])
print(R.det())
region.rot = R.t()
chunk.region = region
R = chunk.region.rot #Bounding box rotation matrix
C = chunk.region.center #Bounding box center vector

if chunk.transform.matrix:
T = chunk.transform.matrix
s = math.sqrt(T[0,0] ** 2 + T[0,1] ** 2 + T[0,2] ** 2) #scaling # T.scale()
S = PhotoScan.Matrix().Diag([s, s, s, 1]) #scale matrix
else:
S = PhotoScan.Matrix().Diag([1, 1, 1, 1])
T = PhotoScan.Matrix( [[R[0,0], R[0,1], R[0,2], C[0]], [R[1,0], R[1,1], R[1,2], C[1]], [R[2,0], R[2,1], R[2,2], C[2]], [0, 0, 0, 1]])
chunk.transform.matrix = S * T.inv() #resulting chunk transformation matrix

print("Script finished")
Title: Re: Rotate chunk by the orientation of markers
Post by: Milderinne on July 12, 2018, 06:24:17 PM
Hello Alexey,
sorry for the long time not replying!
Thanks for that script. I will test it. Is it also possible to only rotate horizontically.
Best regards
Title: Re: Rotate chunk by the orientation of markers
Post by: mcstieg on December 01, 2018, 06:27:55 PM
Hello Alexey  :)

Is it possible to do the same with a set of markers on the ground plane? (no vertical markers)

Let's say we have markers on the ground, placed like an "L".
How can I change the script above so that the ground plane is rotated well? (all markers on the gnd)


Thank you very much!  :D
Title: Re: Rotate chunk by the orientation of markers
Post by: bbbares on April 28, 2020, 01:01:05 AM
I am bumping this post because an answer to the above question would answer one of my recent posts. I too only have floor markers and am looking for a solution that supports that.