5
« on: March 24, 2021, 03:24:26 PM »
Hi everyone,
I return here, to ask a second question for my project. In fact now, I have a working algorithm that give me a list of best image for each 3d mark convert in 2d coordination. You can see the script below. Now ne problem is if it's possible sing the camera that I found with the algorithm to cut off for each camera of interest a piece of the best image. For do this I need to use My 2d coordinate for each mark, and create a buffer shape mask to use for cut off the selected image.
My working script for now it's this:
import Metashape
import csv
path = "I:/Backsjon190527/save_progress/"
doc = Metashape.Document()
doc.open(path + "Finale.psx")
chunk = doc.chunk
crs = chunk.crs
T = chunk.transform.matrix
print(crs)
csvfile = 'I:/Backsjon190527/save_progress/hs1.csv'
def ReadCSVasList(csvfile):
try:
with open(csvfile) as csv_file:
reader = csv.reader(csv_file, dialect='excel')
datalist = []
datalist = list(reader)
return datalist
except IOError as err:
print("I/O error({0})".format(err))
csv_data_list = ReadCSVasList(csvfile)
print(csv_data_list)
# To Ignore 1st Element (Headers)
csv_data_list.pop(0)
print(csv_data_list)
result = []
for x in csv_data_list:
x.pop(0)
print(csv_data_list)
for i in range(0, len(csv_data_list), 1):
result.append(list(map(float, csv_data_list[i])))
print(result)
selcam = list()
for x in result:
## script to find the best image near each marker in the Vector list.
P = Metashape.Vector(x) # point of interest in geographic coord
mindis = 1e09
# P = Metashape.Vector((459307.345426, 7192625.624622, 158.808)) # vector with X,Y,Z coordinates of point in same CRS as chunk ( I need to understand )
cam_dist_P = {} # dict containing distance in pixels of P to each camera Principal Point
for camera in chunk.cameras:
sensor = camera.sensor
w = sensor.width
h = sensor.height
cx = sensor.calibration.cx # Principal point coordinates x
cy = sensor.calibration.cy # Principal point coordinates y
if not camera.project(T.inv().mulp(chunk.crs.unproject(P))): # check if projected Point within sensor size
continue
xp = camera.project(T.inv().mulp(chunk.crs.unproject(P))).x # Prrojected point coordinates x
yp = camera.project(T.inv().mulp(chunk.crs.unproject(P))).y # Prrojected point coordinates y
if xp < 0 or xp > w or yp < 0 or yp > h: # check if projected PointI within sensor size
continue
dist = (camera.project(T.inv().mulp(chunk.crs.unproject(P))) - Metashape.Vector(
(w / 2 + cx, h / 2 + cy))).norm() # distance in pixels from projected point and P
cam_dist_P[camera.label] = dist
if dist < mindis:
mindis = dist
mincam = camera # camera with min distance to Principal Point
selcam.append(mincam)
# for image in mincam:
# Metashape.ModelKeyPoint(Metashape.PointCloud.renderImage(selcam), x)
print(mincam)
mincam.selected = True
print(selcam)
This script give to me this result:
[<Camera '100_0015_0047'>, <Camera '100_0015_0047'>, <Camera '100_0015_0048'>, <Camera '100_0015_0036'>, <Camera '100_0015_0036'>, <Camera '100_0015_0049'>, <Camera '100_0015_0048'>, <Camera '100_0015_0046'>, <Camera '100_0015_0046'>, <Camera '100_0015_0040'>, <Camera '100_0015_0047'>, <Camera '100_0015_0045'>, <Camera '100_0015_0040'>, <Camera '100_0015_0040'>, <Camera '100_0015_0046'>].
For the new problem I found on the metashape python reference manual some probably use full commands like these:
# c = Metashape.ImageCompression()
# c.jpeg_quality = 50
# chunk.cameras[mincam].image().save(path + fr"{test50}.jpg", c) #create a loop for new file names saving for each cutted image.
Metashape.Mask
# new_shape = chunk.shapes.addShape() #create a new mask shape to cut off a chunk of the selected photo.
# new_shape.type = Metashape.Shape.Polygon
# new_shape.has_z = False
# new_shape.center = [xxx] #center for the new shape?
Someone can give me some advice, or have any Idea to how continue this work?
Thank you in advance to who will help me!
Best regards,
Emanuele.