Forum

Author Topic: Apply patch on multiple shapes  (Read 11223 times)

benj_G

  • Newbie
  • *
  • Posts: 24
    • View Profile
Apply patch on multiple shapes
« on: July 08, 2019, 11:28:16 AM »
hi,

In the api python metashape, I found a really useful script for me. This script applies patch of the first picture of the list to the first shape of the list. But I have several shapes. so I made a loop that runs through all the shapes but this script always applies the same image to different shapes.
how to apply the first rank image of each shapes?
thanks in advance for help

Code: [Select]
import Metashape
chunk = Metashape.app.document.chunk
ortho = chunk.orthomosaic
camera = chunk.cameras[0]
patch = Metashape.Orthomosaic.Patch()
patch.image_keys = [camera.key]
for shape in chunk.shapes.items():
ortho.patches[shape] = patch
ortho.update()
« Last Edit: July 08, 2019, 12:34:07 PM by benj_G »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Apply patch on multiple shapes
« Reply #1 on: July 10, 2019, 03:38:53 PM »
Hello benj_G,

In your code you are always assigning the first camera in the chunk to all the patches.

I'm attaching the script that performs automatic patching for all the polygonal shapes in the active chunk. Please check, if it works as expected. Note that the rank selection by the script is little bit different from the GUI version that is more complex.
Best regards,
Alexey Pasumansky,
Agisoft LLC

benj_G

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Apply patch on multiple shapes
« Reply #2 on: July 10, 2019, 04:12:03 PM »
Thanks !!!! it's working like a charm !!!!
I understand how the script works to find the best image but can you explain me how the selection in the GUI works?

thanks again for your help
« Last Edit: July 10, 2019, 04:15:29 PM by benj_G »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Apply patch on multiple shapes
« Reply #3 on: July 10, 2019, 05:18:22 PM »
Hello benj_G,

GUI ranking of the cameras in the Assign Image dialog is also considering additional factors such as the distance of the selected area projected to the image plane from the image center, distance from camera to the 3D surface being textured, relative orientation of the camera normal and surface normals.
Best regards,
Alexey Pasumansky,
Agisoft LLC

benj_G

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Apply patch on multiple shapes
« Reply #4 on: July 10, 2019, 05:22:27 PM »
Ok thanks again !  :D

Vaidutis Zutautas

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Apply patch on multiple shapes
« Reply #5 on: November 06, 2019, 01:44:59 PM »
Hej,

I did not want to create a new theme since my question in large part overlaps with this solution.

I am looking for a solution to update an orthomosaic with a set of patches, however it is not always the case that the centroid has to be used in assigning images to the polygons.

I was wondering if there is a way to assign names of images as attributes to manually created patch polygons and then call the image names instead of the first image in the list or finding patch centroid etc when recreating orthomosaic?

Any ideas or hints?

Much appreciated!   

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Apply patch on multiple shapes
« Reply #6 on: November 06, 2019, 02:40:59 PM »
Hello Vaidutis,

You can check an example in the following thread, where the shape attributes are considered for the export:
https://www.agisoft.com/forum/index.php?topic=10266.msg48076#msg48076

In the same way they can be checked to apply the patches: loop through all polygonal shapes, check they attributes - if attribute corresponds to the label of any aligned camera in the chunk, then apply patch.

Let me know, if a code example would be helpful here.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Vaidutis Zutautas

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Apply patch on multiple shapes
« Reply #7 on: November 06, 2019, 03:55:18 PM »
Hi again,

That is exactly what I need. An example of code would be much appreciated!

Thank you in advance!  :)

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Apply patch on multiple shapes
« Reply #8 on: November 07, 2019, 03:19:07 PM »
Hello Vaidutis,

Please check the following script. The script requires the polygonal shapes for patching to be grouped to "Patches" shape layer (other layers will be ignored). Each shape should have an attribute with "Patch" name and this attribute value should be set in accordance to the camera label to be used for patching.

Code: [Select]
import Metashape

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

if not chunk:
print("No active chunk, script aborted")
return False
if not chunk.orthomosaic:
print("No orthomosaic for the active chunk, script aborted")
return False
if not chunk.shapes:
chunk.shapes = Metashape.Shapes()
print ("No shapes, script aborted")
return False
ortho = chunk.orthomosaic

patches_list = [shape for shape in chunk.shapes if (shape.type == Metashape.Shape.Type.Polygon) and (shape.group.label.upper() == "PATCHES")] #list of polygonal shapes for patching

patches = dict()
for shape in patches_list:
if "Patch" not in shape.attributes.keys():
continue #no attribute
for camera in chunk.cameras:
if not camera.transform:
continue
if camera.label.upper() ==  shape.attributes["Patch"].upper():
patches[shape] = camera
patch = Metashape.Orthomosaic.Patch()
patch.image_keys = [patches[shape].key]
ortho.patches[shape] = patch
print("Assigned patch to shape " + shape.label + " for camera " + camera.label)
continue


ortho.update() #update orthomosaic patches
print("Script finished.")

Metashape.app.addMenuItem("Custom menu/Automatic orthomosaic patching", auto_patch)
I suggest to check, if the script works as expected. If any modifications are required, please let me know.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Vaidutis Zutautas

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Apply patch on multiple shapes
« Reply #9 on: November 21, 2019, 03:34:55 PM »
Hi again!

The script works as described, yet perhaps I expressed myself in an unclear way. The real challenge here is to assign camera names to attribute (in this case 'Patches') automatically. I am still struggling to find a solution at this point for such scenario:

1. Generate orthomosaic
2. Create polygons as patches
3. *Automatically assign current camera of the patch to an attrribute of polygon (I am talking about hundreds of polygons)* // I am stuck here!
4. Generate another orthomosaic of the same area, say in another Chunk
5. Reuse the patches with the help of the script you provided earlier.

Thanks for reflections!
« Last Edit: November 22, 2019, 09:54:37 AM by Vaidutis Zutautas »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Apply patch on multiple shapes
« Reply #10 on: November 22, 2019, 05:25:16 PM »
Hello Vaidutis,

So you've got an orthomosaic with the drawn patches. How the cameras should be assigned to the patches - what should be a criterion?
Best regards,
Alexey Pasumansky,
Agisoft LLC

Vaidutis Zutautas

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Apply patch on multiple shapes
« Reply #11 on: November 25, 2019, 12:02:37 AM »
Hej,

After manual creation of patches, where cameras are automatically assigned depending where one starts drawing a patch, I would like to run a script which would create an attribute in each polygon with currently assigned camera name (only one!). As a result, this attribute could then be passed on to the script you provided earlier in order to edit another orthomosaic by using the same polygons (patches) followed by already assigned correct cameras.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Apply patch on multiple shapes
« Reply #12 on: November 25, 2019, 12:56:05 PM »
Hello Vaidutis,

Please check the following code. For each polygonal-patch shape it assigns the shape label and "Patch" attribute according to the camera used for patching.

Code: [Select]
def get_camera(chunk, key):
    for camera in chunk.cameras:
        if camera.key == key:
            return camera
    return None
chunk = Metashape.app.document.chunk
layer = chunk.shapes.addGroup()
layer.label = "Patches"
for shape in chunk.orthomosaic.patches.keys():
    camera = get_camera(chunk, chunk.orthomosaic.patches[shape].image_keys[0])
    if not camera:
        continue
    shape.label = camera.label
    shape.attributes["Patch"] = camera.label
    shape.group = layer
print("finished")
Best regards,
Alexey Pasumansky,
Agisoft LLC

mcstieg

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Apply patch on multiple shapes
« Reply #13 on: April 24, 2020, 02:25:39 PM »
Hello!

in the GUI it is possible to right click on a polygon patch to assign images to this patch.
How can the list of suggested cameras (1 to n) be calculated via python?

Thank you!
« Last Edit: April 26, 2020, 04:07:06 PM by mcstieg »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Apply patch on multiple shapes
« Reply #14 on: April 27, 2020, 09:28:52 PM »
Hello mcstieg,

It is not possible to fully replicate via Python the list of the images with the ranks, but as a workaround you can use the following metric:
estimate the XY of the image center projected on the surface and calculate the distance from it to the shape centroid.
Best regards,
Alexey Pasumansky,
Agisoft LLC