Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: UAV_project on September 17, 2020, 05:30:04 PM

Title: Adding markers for georeferencing of an orthomosaic
Post by: UAV_project on September 17, 2020, 05:30:04 PM
I have looked through a number of threads and pieced together what I thought should work for importing markers and assigning them their image and real world coordinates. However, after running the script I fear that something is wrong since when I check the reference tab in the GUI, it doesn't show that my markers have their real world coordinates assigned.

Here is the code I am using:

Code: [Select]
        import Metashape as MS

        # set the coordinate ref system of the markers
        crs_ = MS.CoordinateSystem("EPSG::%d" % crs)
        chunk.marker_crs = crs_
       
        # set the z-accuracy very high because we don't have a good measure for this
        chunk.marker_location_accuracy = [0.05, 0.05, 1000]
       
        file = open('./export.csv', "rt")#input file
       
        # skips header
        lines = file.readlines()[1:]

        for line in lines:
            line = line.strip('\n')
            sp_line = line.rsplit(separation, 6) #splitting line
            # Strip extra quotation marks if they exist
            for i, item in enumerate(sp_line):
                sp_line[i] = item.strip('"')
               
            camera_name = sp_line[0]
            marker_name = sp_line[1]
            x = float(sp_line[2]) #x-coord in pixels
            y = float(sp_line[3]) #y-coord in pixels
            cx = float(sp_line[4]) #world x-coord of marker
            cy = float(sp_line[5]) #world y-coord of marker
            cz = float(sp_line[6]) #world z-coord of marker

            # flag to indicate if marker is found
            flag = 0
           
            # Create a Projection from the pixel coordinates
            pixel_coord = MS.Marker.Projection(MS.Vector([x, y]), True)
           
            # get the camera with same name as the name in current line
            for cam in chunk.cameras:
                if cam.label == camera_name:
                   
                    #searching for a marker with the same ID as in txt file
                    for marker in chunk.markers:
                        if marker.label == marker_name:
                            # assign the marker's x,y coords in the current cam
                            marker.projections[cam] = pixel_coord
                            flag = 1
                            break
                   
                    # if marker ID not yet existing in the chunk then create
                    if flag == 0:
                        marker = chunk.addMarker()
                        marker.label = marker_name

                        # assign the marker's x,y coords in the current cam
                        marker.projections[cam] = pixel_coord
                        # Add the world coordinates to the marker
                        marker.Reference.location = MS.Vector([cx, cy, cz])
                        # Set the accuracy of the marker's coords
                        marker.Reference.accuracy = MS.Vector(accuracy)
                        # enable the marker
                        marker.Reference.enabled = True

                    # move onto the next line of the text file
                    break

        file.close()
        print("Import finished.")

One additional question to this. If I add the markers like this, can I then later use the reference_preselection mode when I am trying to match the images? If so, would the following be correct (after having added the markers as above):

Code: [Select]

chunk.crs = chunk.marker_crs
chunk.updateTransform()
chunk.matchPhotos(generic_preselection=False, reference_preselection=True)


Thanks!
Title: Re: Adding markers for georeferencing of an orthomosaic
Post by: UAV_project on September 17, 2020, 05:50:45 PM
Here is a screenshot of the markers in the GUI after I ran the Python script to add them. As you can see they lack Easting and Northing coordinates, although they should be there if my code is written correctly.
Title: Re: Adding markers for georeferencing of an orthomosaic
Post by: Alexey Pasumansky on September 17, 2020, 07:26:58 PM
Hello UAV_project,

I think that in the second part of the script you should use:
marker.reference.location = ...
marker.reference.location_accuracy = ...
marker.reference.enabled = ...

instead of marker.Reference...


Reference preselection (source values) can be used only if the camera location coordinates have been loaded to the source tab of the Reference pane.
Title: Re: Adding markers for georeferencing of an orthomosaic
Post by: UAV_project on September 18, 2020, 02:53:29 PM
Great it works now, thanks!