Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: Chris_1979 on March 05, 2019, 05:19:49 PM

Title: Adding scalebar to images in separate groups based on camera.label
Post by: Chris_1979 on March 05, 2019, 05:19:49 PM
Hi,

I know there have been similar questions to this one asked previously, but I'm struggling to apply those solutions here.

I have a script which places images in to two separate groups based on the image resolution. Now I want to compare part of the camera.label of the images in these two groups and if there's a match I'll apply a scalebar between the two cameras.

The script is working, apart from the section which compares the camera.label of the images in the two groups.

Any help would be greatly appreciated.

Thanks

Code: [Select]
def generate_point_cloud(input_dir, output_dir, calibration_pathA, calibration_pathB, scale):   
    print("Script started")
    doc = PhotoScan.app.document
    doc.save(os.path.join(output_dir, "projectGenerated.psx"))
    chunk = doc.addChunk() 
    chunk.label = "New Chunk"
    Distance = scale
   
    #Create a list to store the image paths
    image_list = list()

    #Read the image paths from the .txt file. Strip off the \n using s.strip()
    f = open(input_dir, "r")
    image_list = f.readlines()
    image_list = map(lambda s: s.strip(), image_list)
    print(image_list)


    #Create two camera groups within the main chunk. One for the HD images and one for the UHD images
    HD = chunk.addCameraGroup()
    HD.label = "HD"
    UHD = chunk.addCameraGroup()
    UHD.label = "UHD"
    #print(chunk.camera_groups)
   
   
    #Create two camera calibration files for each image set
    calibs = dict()
    calibs["HD"] = PhotoScan.Calibration()
    calibs["UHD"] = PhotoScan.Calibration()
    calibs["HD"].load(calibrationA, "xml")
    calibs["UHD"].load(calibrationB, "xml")

    #Create two sensors for the two image sizes
    sensors = dict()
    sensors["HD"] = chunk.addSensor()
    sensors["UHD"] = chunk.addSensor()
    sensors["HD"].label = "HD"
    sensors["UHD"].label = "UHD"

   
   
    #Add the photos to the project Chunk
    chunk.addPhotos(image_list)
    print("no cameras ", len(chunk.cameras))

    #Place the UHD images in the UHD group and assign the calibration file for that camera. Assign HD images to HD group
    #and assign the camera calibration file for those images.
    for camera in chunk.cameras:
                    if camera.sensor.width == 2048:
                            camera.group = HD
                            camera.sensor = sensors["HD"]
                    elif camera.sensor.width == 4096:
                            camera.group = UHD
                            camera.sensor = sensors["UHD"]
                           
                           
    for sensor in sensors.keys():
            calib = calibs[sensor]
            sensors[sensor].width = calib.width
            sensors[sensor].height = calib.height
            sensors[sensor].type = calib.type
            sensors[sensor].user_calib = calib
            sensors[sensor].fixed = True     
 
    PhotoScan.app.update()
    #align photos
    chunk.loadReferenceExif(load_rotation=False, load_accuracy=False)

    chunk.matchPhotos(accuracy = PhotoScan.HighAccuracy, preselection = PhotoScan.Preselection.GenericPreselection, filter_mask = False, keypoint_limit = 50000)

    print("aligning ", len(chunk.cameras), " images")   
    chunk.alignCameras() # instead of alignPhotos
   
    chunk.buildDenseCloud(quality = PhotoScan.LowQuality, filter = PhotoScan.AggressiveFiltering)

    for camera in chunk.cameras:
        #if camera.label[0:26] == the camera.label of any of the other camera labels
           # sb = chunk.addScalebar(camera, other_camera)
            sb.reference.distance = float(Distance)
           
    chunk.updateTransform()   
           
     
    chunk.exportCameras(os.path.join(output_dir, "cameras.xml"))
    chunk.exportPoints(path=os.path.join(output_dir, "points.xyz"))
   
    #doc.save(path = project_path)
    doc.save(os.path.join(output_dir, "projectGenerated.psx"))
    PhotoScan.app.update()
    print("Script finished")
    return 1
Title: Re: Adding scalebar to images in separate groups based on camera.label
Post by: Alexey Pasumansky on March 09, 2019, 06:09:01 PM
Hello Chris,

I can suggest something like the following:

Code: [Select]
group1 = dict()
group2 = dict()

#this part creates two dictionaries which keys are part of the camera labels:
for camera in chunk.cameras:
    if camera.sensor == chunk.sensors[0]:
        group1[camera.label[0:26]] = camera
    elif camera.sensor == chunk.sensors[1]:
        group2[camera.label[0:26]] = camera

#this part tries to find the corresponding elements between two dictionary keys:
for label in group1.keys():
    if label in group2.keys():
         scalebar = chunk.addScalebar(group1[label], group2[label])
         scalebar.reference.distance = float(Distance)
Title: Re: Adding scalebar to images in separate groups based on camera.label
Post by: Chris_1979 on March 21, 2019, 01:30:07 PM
Thank you Alexey, that's exactly what I was trying to do.

Many Thanks,
Chris