Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: flyzk on August 19, 2021, 09:40:06 AM

Title: Export SHAPE local coordinate for every picture
Post by: flyzk on August 19, 2021, 09:40:06 AM
Hi,

I would like to export for a specific shape a csv file with the x,y local coordinate of the location of the shape in every picture where the shape is located.

the csv file would have this format:
name_shape, name_picture,coordinates_location_xy

any help would be very appreciate

thanks
Title: Re: Export SHAPE local coordinate for every picture
Post by: Paulo on August 20, 2021, 07:54:03 AM
Hi Flyzq,

you can look at shapely.geometry and import Polygon as
Code: [Select]
from shapely.geometry import Polygon
chunk = Metashape.app.document.chunk
T = chunk.transform.matrix
crs = chunk.shapes.crs
given a shape then populate vertices with shape internal vertex coordinates as
Code: [Select]
vertices = [(T.inv().mulp(crs.unproject(vertex))) for vertex in  shape.vertices] # vertices in internal coordinates
for a given camera then the polygon p with the shape projected image coordinates will be defined by:
Code: [Select]
image_ver = list()  # image shape vertices
w = camera.sensor.width
h = camera.sensor.height
frame = Polygon([(0,0),(w,0),(w,h),(0,h)])
num_ver = 0
for vertex in vertices:
    num_ver += 1
    if not camera.project(vertex):
    continue
    x = round(camera.project(vertex).x,3)
    y = round(camera.project(vertex).y,3)
    if num_ver < len(vertices) :
    image_ver.append((x,y))           
p = Polygon(image_ver)
print(camera.label,p.intersection(frame))
Note that we use intersection of polygon p with image frame to only get shape within frame. Result will be something like attached screen copy....
I think this could get you started
Title: Re: Export SHAPE local coordinate for every picture
Post by: flyzk on August 20, 2021, 08:27:44 PM
Hi Paulo,

Thanks you so much for this start. I'll try it right now  :)