-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_gcp_pxcoords.py
More file actions
50 lines (42 loc) · 1.81 KB
/
compute_gcp_pxcoords.py
File metadata and controls
50 lines (42 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Metashape
import csv
# Read input file containing photo id, gcp name, xImg (mm), yImg (mm)
def read_csv_file(filename):
data = []
with open(filename, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
data.append(row)
return data
def compute_pxcoords(chunk, gcp_file, output_file):
print("Script started...")
#Save active cameras information
photos = []
for camera in chunk.cameras:
photos.append({
"name": camera.label.replace('.tif',''),
"sensorheight": camera.sensor.height,
"sensorwidth":camera.sensor.width,
"pxsize": camera.sensor.pixel_size
})
with open(output_file, 'w') as f: # Open output file
for marker in chunk.markers:
if marker.reference.enabled: #If the marker is selected in the reference pane
for row in gcp_file:
if (row['gcp'] == marker.label):
for p in photos:
cx = (camera.sensor.width-1)/ 2 #Coordinates of the center of the photo in px
cy = (camera.sensor.height-1)/ 2
if(row['photo_id'] == p['name']):
xpx = cx + float(row['ximg'])/p['pxsize'][0] #CRS conversion and translation
ypx = cy - float(row['yimg'])/p['pxsize'][1] # yImg and yPx axis are opposite, hence -
print (marker.label + "," + row['photo_id'] + "," + str(xpx) + "," + str(ypx), file=f)
print("Script finished")
return 1
chunk = Metashape.app.document.chunk
#Input file path. Format: PHOTONAME (without '.tif'),GCPNAME,xIMG,yIMG
path_input = r"C:\Users\admin\Desktop\GRAZ\metashape\scripts\imgcoords_gcp.txt"
#Output file path. Format: GCPNAME,PHOTONAME,xPX,yPX
path_output = r"C:\Users\admin\Desktop\GRAZ\metashape\scripts\pxcoords_gcp.txt"
gcp_img_coords = read_csv_file(path_input)
compute_pxcoords(chunk, gcp_img_coords, path_output)