Forum

Author Topic: Import Polygon with Python Script  (Read 2253 times)

Sami26beniaouf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Import Polygon with Python Script
« on: July 13, 2021, 10:23:53 AM »
Hello,

Is there a way i can draw a polygon or import one on an image using a Python script.

Thank you!
« Last Edit: July 27, 2021, 10:44:42 AM by Sami26beniaouf »

Sami26beniaouf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Import Polygon with Python Script
« Reply #1 on: July 16, 2021, 03:01:29 PM »
a
« Last Edit: July 27, 2021, 10:46:07 AM by Sami26beniaouf »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 13995
    • View Profile
Re: Draw Polygon with Python Script
« Reply #2 on: July 17, 2021, 12:32:48 AM »
Hello Sami26beniaouf,

Which information do you have for such shapes? For example, camera label, coordinates of consecutive shape vertices in 2D image coordinates?
Best regards,
Alexey Pasumansky,
Agisoft LLC

Sami26beniaouf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Import Polygon with Python Script
« Reply #3 on: July 19, 2021, 06:23:38 PM »
Hello Alexey,

Thank you for response,

I have binary masks that i can convert to :
polygons with coordinates in 2D image coordinates, on segmentation format:
[
    [x1, y1, x2, y2, x3, y3, ...],
    [x1, y1, x2, y2, x3, y3, ...],
    ...
]
 or point format:
[
    [[x1, y1], [x2, y2], [x3, y3], ...],
    [[x1, y1], [x2, y2], [x3, y3], ...],
    ...
]
« Last Edit: July 27, 2021, 10:45:16 AM by Sami26beniaouf »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 13995
    • View Profile
Re: Draw Polygon with Python Script
« Reply #4 on: July 26, 2021, 03:20:52 PM »
Hello Sami26beniaouf,

Just to clarify, do you want to create a mask on the image based on these coordinates or to create a 3D shape (polygonal)?
Best regards,
Alexey Pasumansky,
Agisoft LLC

Sami26beniaouf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Import Polygon with Python Script
« Reply #5 on: July 27, 2021, 10:44:21 AM »
Hi Alexey,

What we want to do, is classifying 3d points from objects detected in the images. So the method we thought about would be importing the polygons of the detected objects in the images on Metashape, and then Select points by Shapes so we can classify the selected 3d points.

Thanks for reply,
Looking forward an answer hopefully
Best regards

Sami

Paulo

  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: Import Polygon with Python Script
« Reply #6 on: July 27, 2021, 04:23:07 PM »
Hi Sami,

given a camera (first image in chunk or IMG_6256.JPG) and a given image_polygon ([(2057.55, 1153.64),(3552.43, 1461.32),(2435.64, 2431.38), (994.53, 1688.06),(1093.95, 763.83)] then following script will project the image polygon from given camera onto the dense cloud, create a corresponding 3d polygon and select all points inside the 3d polygon:
Code: [Select]
chunk = Metashape.app.document.chunk
T = chunk.transform.matrix
if not chunk.shapes:
chunk.shapes = PhotoScan.Shapes()
chunk.shapes.crs = chunk.crs
camera = chunk.cameras[0]     # given camera (first image in chunk)
image_polygon = [(2057.55, 1153.64),(3552.43, 1461.32),(2435.64, 2431.38), (994.53, 1688.06),(1093.95, 763.83)]    # given image polygon vertex list
if not chunk.dense_cloud:
        raise Exception("No dense cloud!")   
surface = chunk.dense_cloud
polygons = chunk.shapes.addGroup()
polygons.label = "3d_Polygons"
polygons.color = (30, 239, 30)
points = list()
# projecting image polygon on dense cloud
for (x, y) in image_polygon:
    ray_origin = camera.unproject(Metashape.Vector([x, y, 0]))
    ray_target = camera.unproject(Metashape.Vector([x, y, 1]))
    points.append(surface.pickPoint(ray_origin, ray_target))
    if not points[-1]:
        points[-1] = chunk.point_cloud.pickPoint(ray_origin, ray_target)
    if not points[-1]:
        break
    points[-1] = chunk.shapes.crs.project(T.mulp(points[-1]))
# adding 3d shape with points vertices
shape = chunk.shapes.addShape()
shape.type = PhotoScan.Shape.Type.Polygon
shape.group = polygoms
shape.vertices = points
shape.has_z = True
shape.selected = True
chunk.dense_cloud.selectPointsByShapes()

See attached example...
Hope this can get you started
« Last Edit: July 28, 2021, 01:15:04 PM by Paulo »
Best Regards,

Paul Pelletier,
Surveyor

Sami26beniaouf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Import Polygon with Python Script
« Reply #7 on: July 28, 2021, 10:55:04 AM »
Hi Paul,

Thank you, it looks good! i'll try it.

Best regards
Sami