Forum

Author Topic: Seamlines re-use  (Read 4718 times)

minoo_h106

  • Newbie
  • *
  • Posts: 1
    • View Profile
Seamlines re-use
« on: October 26, 2023, 11:59:57 PM »
I need to reuse the existing seamlines since I have done manual editing that I 'd like to keep.
I can export the seamlines from the initial project, but how can I import them into subsequent project and ensure that Metashape recognizes and uses them?
Is there a Python code available for this purpose?

James

  • Hero Member
  • *****
  • Posts: 769
    • View Profile
Re: Seamlines re-use
« Reply #1 on: October 27, 2023, 12:16:25 PM »
+1 for a method to do this please.

I often spend a long time manually assigning images only to later realise that I should have done colour correction, or not done it, or added some more photos, or improved the alignment, and then subsequent rebuild of the orthomosaic causes all the shape/image assignments to be lost.

I'm not saying it can't be done currently, but I don't know how.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15407
    • View Profile
Re: Seamlines re-use
« Reply #2 on: November 02, 2023, 06:35:03 PM »
Hello!

What do you mean by re-using? Preserving the selection of image/images for each polygonal patch shape?

Currently it is not possible directly, as patch information is discarded once the patch is applied with Update Orthomosaic button. But there is a script created for similar purposes - it writes to the shape meta data labels of the assigned cameras and allows to read the labels from meta information (providing that the shapes have been transferred to another project with the attributes) and assign corresponding images to patches automatically.

The script is below, please let me know, if it works as described and can be used to solve your request.


Code: [Select]
#compatibility - Metashape Pro 2.0


import Metashape

def get_camera_by_key(key, chunk):

for camera in chunk.cameras:
if camera.key == key:
return camera
return None #camera not found

def patch_to_attr():

doc = Metashape.app.document
chunk = doc.chunk #active chunk

if not chunk:
print("Empty project, script aborted")
return 0
if not len([camera for camera in chunk.cameras if camera.type == Metashape.Camera.Type.Regular]):
print("No cameras, script aborted")
return 0
if not chunk.orthomosaic:
print("No orthomosaic for the active chunk, script aborted")
return 0
if not chunk.shapes:
print ("No shapes, script aborted")
return 0

ortho = chunk.orthomosaic
shapes = chunk.shapes

patches = ortho.patches.keys()
if not len(patches):
print ("No patches, script aborted")
return 0

count = 0

for patch in patches:
assigned_image = get_camera_by_key(ortho.patches[patch].image_keys[0], chunk)
if assigned_image == None:
print("Skipping patch", patch)
continue
patch.attributes["PATCH_CAMERA_LABEL"] = assigned_image.label
if assigned_image.photo.path:
patch.attributes["PATCH_CAMERA_PATH"] = assigned_image.photo.path
count += 1

print(str(count) + " patches saved to shape attributes. Script finished.")
return 1

def attr_to_patch():
doc = Metashape.app.document
chunk = doc.chunk #active chunk

if not chunk:
print("Empty project, script aborted")
return 0
if not len([camera for camera in chunk.cameras if camera.type == Metashape.Camera.Type.Regular]):
print("No cameras, script aborted")
return 0
if not chunk.orthomosaic:
print("No orthomosaic for the active chunk, script aborted")
return 0
if not chunk.shapes:
print ("No shapes, script aborted")
return 0

ortho = chunk.orthomosaic

patches = [shape for shape in chunk.shapes if (shape.geometry.type == Metashape.Geometry.Type.PolygonType) and ("PATCH_CAMERA_LABEL" in shape.attributes.keys())] #list of patches

ortho_patches = dict()
count = 0

for shape in patches:
found_cameras = []
for camera in chunk.cameras:
if camera.label.upper() == shape.attributes["PATCH_CAMERA_LABEL"].upper():
found_cameras.append(camera)
if not len(found_cameras):
print("Skipping " + str(shape) + " patch, no related cameras found")
continue
elif len(found_cameras) > 1:
if "PATCH_CAMERA_PATH" not in shape.attributes.keys():
print("Using first found camera with corresponding label for " + str(shape) + " patch")
camera = found_cameras[0]
else:
for camera in found_cameras:
if camera.photo.path.upper() == shape.attributes["PATCH_CAMERA_PATH"].upper():
break
print("Using first found camera with corresponding label for " + str(shape) + " patch")
camera = found_cameras[0]
else:
camera = found_cameras[0]
###camera - image to be assigned

patch = Metashape.Orthomosaic.Patch()
patch.image_keys = [camera.key]
ortho.patches[shape] = patch
count += 1
print("Assigned patch " + str(shape) + " for camera " + camera.label)

print(str(count) + " patches loaded from shape attributes.")
return 1

Metashape.app.addMenuItem("Scripts/Save existing patches to shape attributes", patch_to_attr)
Metashape.app.addMenuItem("Scripts/Load patches from shape attributes", attr_to_patch)
Best regards,
Alexey Pasumansky,
Agisoft LLC