Hello everyone,
I start a project where I need to project some 3D coordinates ( that I don't have on metashape, but in a csv.file) on 2d areal camera/image using python linked to a metashape project. Then I need to cut off and save the best areal image were each point is projected, so I will have a folder with all the best image of the single point in the map.
Someone can help me?
I started using this script that I saw in another post here in the forum:
import Metashape, math, csv
path = "I:/Backsjon190527/save_progress/"
doc = Metashape.Document()
doc.open(path + "Finale.psx")
chunk = doc.chunk
crs = chunk.crs
print(crs)
# export format:
# point_ID X Y Z
# [camera_label x_proj y_proj]
M = chunk.transform.matrix
point_cloud = chunk.point_cloud
projections = point_cloud.projections
points = point_cloud.points
npoints = len(points)
print(npoints)
tracks = point_cloud.tracks
path1 = Metashape.app.getSaveFileName(path + 'New.csv',
filter="Text / CSV (*.txt *.csv);;All files (*.*)")
file = open("New.csv", 'wt')
print("Script started...")
point_ids = [-1] * len(point_cloud.tracks)
for point_id in range(0, npoints):
point_ids[points[point_id].track_id] = point_id
points_proj = {}
for photo in chunk.cameras:
if not photo.transform:
continue
T = photo.transform.inv()
calib = photo.sensor.calibration
for proj in projections[photo]:
track_id = proj.track_id
point_id = point_ids[track_id]
if point_id < 0:
continue
if not points[point_id].valid: # skipping invalid points
continue
point_index = point_id
if point_index in points_proj.keys():
x, y = proj.coord
points_proj[point_index] = (points_proj[point_index] + "\n" + photo.label + "\t{:.2f}\t{:.2f}".format(x, y))
else:
x, y = proj.coord
points_proj[point_index] = ("\n" + photo.label + "\t{:.2f}\t{:.2f}".format(x, y))
for point_index in range(npoints):
if not points[point_index].valid:
continue
coord = M * points[point_index].coord
coord.size = 3
if chunk.crs:
# coord
X, Y, Z = chunk.crs.project(coord)
else:
X, Y, Z = coord
line = points_proj[point_index]
file.write("{}\t{:.6f}\t{:.6f}\t{:.6f}\t{:s}\n".format(point_index, X, Y, Z, line))
file.close()
print("Finished")
This code probably is not good for what I want to do...
Thank you in advance for who will help me!
Best regard.
Emanuele.