Forum

Author Topic: Export SHAPE local coordinate for every picture  (Read 3070 times)

flyzk

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Export SHAPE local coordinate for every picture
« 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
« Last Edit: August 19, 2021, 09:46:04 AM by flyzk »

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Export SHAPE local coordinate for every picture
« Reply #1 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
« Last Edit: August 20, 2021, 08:28:57 AM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

flyzk

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Export SHAPE local coordinate for every picture
« Reply #2 on: August 20, 2021, 08:27:44 PM »
Hi Paulo,

Thanks you so much for this start. I'll try it right now  :)
« Last Edit: August 20, 2021, 08:59:31 PM by flyzk »