Forum

Author Topic: Shape from markers  (Read 1986 times)

cgotelli

  • Newbie
  • *
  • Posts: 4
    • View Profile
Shape from markers
« on: February 07, 2022, 02:02:30 PM »
Hello! I've trying to create an outer boundary shape from markers with the python api. I found this post: https://www.agisoft.com/forum/index.php?topic=8985.0, but I haven't been able to implement it. Have there been any updates since the day this answer was posted? If so, which functions should I use now?

Thanks in advance for the help!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14846
    • View Profile
Re: Shape from markers
« Reply #1 on: February 07, 2022, 05:00:48 PM »
Hello cgotelli,

Do you know the order of the markers to be used as a boundary shape vertices?
Best regards,
Alexey Pasumansky,
Agisoft LLC

cgotelli

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Shape from markers
« Reply #2 on: February 08, 2022, 12:50:05 PM »
Hello!

Yes! The markers are positioned in two parallel lines, from 1 to 23 in one side and 24 to 46 in the other.

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Shape from markers
« Reply #3 on: February 08, 2022, 08:05:17 PM »
Hi  cgotelli,

if the order of the markers corresponds to the order of the vertices of the created shape, then the script you mentioned can be adapted to v. 1.8 as follows:

Code: [Select]
chunk = Metashape.app.document.chunk
T = chunk.transform.matrix
if not chunk.shapes:
chunk.shapes = Metashape.Shapes()
chunk.shapes.crs = chunk.crs
crs = chunk.shapes.crs
shape = chunk.shapes.addShape()
shape.label = "boundary"
shape.geometry.type = Metashape.Geometry.Type.PolygonType
shape.boundary_type = Metashape.Shape.BoundaryType.OuterBoundary

coords = [crs.project(T.mulp(marker.position)) for marker in chunk.markers]
shape.geometry = Metashape.Geometry.Polygon([Metashape.Vector([coord.x, coord.y]) for coord in coords])

it will place outerboundary shape according to markers.
« Last Edit: February 09, 2022, 02:17:03 AM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14846
    • View Profile
Re: Shape from markers
« Reply #4 on: February 08, 2022, 08:08:39 PM »
My version of function for polygon shape creation from markers (list of markers should be defined before using the function, if you do not need to use all the markers from the chunk):


Code: [Select]
import Metashape


def create_shape_from_markers(marker_list, chunk):

if not chunk:
print("Empty project, script aborted")
return 0
if len(marker_list) < 3:
print("At least three markers required to create a polygon. Script aborted.")
return 0

T = chunk.transform.matrix
crs = chunk.crs
if not chunk.shapes:
chunk.shapes = Metashape.Shapes()
chunk.shapes.crs = chunk.crs
shape_crs = chunk.shapes.crs


coords = [shape_crs.project(T.mulp(marker.position)) for marker in marker_list]

shape = chunk.shapes.addShape()
shape.label = "From markers"
shape.geometry.type = Metashape.Geometry.Type.PolygonType
shape.geometry = Metashape.Geometry.Polygon(coords)

print("Script finished.")
return 1


chunk = Metashape.app.document.chunk
m_list = list()
for marker in chunk.markers:
m_list.append(marker)
create_shape_from_markers(m_list, chunk)

Boundary assignment also not included.
« Last Edit: February 08, 2022, 08:11:40 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

cgotelli

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Shape from markers
« Reply #5 on: February 11, 2022, 12:17:18 PM »
Thank you for the replies! I adapted the code and it's running. The only problem is that the shape seems to have a different coordinate system, or projection, because it's displaced (see attached figure). I checked the values of the markers' coordinates, and they're correct. I also checked that both shape and project have the same local coordinate system ('LOCAL_CS["Local Coordinates (m)",LOCAL_DATUM["Local Datum",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]]]'), and it's also correct. What could be the problem? Thank you again for your help.

Clemente

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Shape from markers
« Reply #6 on: February 11, 2022, 06:01:37 PM »
Hello cgotelli,

you may have a shapes projection matrix to apply. In the project console, do following:

Code: [Select]
P = chunk.shapes.projection.matrix
P

and report what you get.  it is not an identity matrix then you may have to change the coords line from code snippet to:
Code: [Select]
coords = [P.mulp(shape_crs.project(T.mulp(marker.position))) for marker in marker_list]
Hope this can help.
Best Regards,
Paul Pelletier,
Surveyor

cgotelli

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Shape from markers
« Reply #7 on: February 11, 2022, 06:52:50 PM »
Thank you Paulo! It worked ;)  ;D