Author Topic: Adding scalebar to images in separate groups based on camera.label  (Read 210 times)


  • Newbie
  • *
  • Posts: 3
    • View Profile

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.


Code: [Select]
def generate_point_cloud(input_dir, output_dir, calibration_pathA, calibration_pathB, scale):   
    print("Script started")
    doc =, "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)

    #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"
    #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
    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:
                   = HD
                            camera.sensor = sensors["HD"]
                    elif camera.sensor.width == 4096:
                   = 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  
    #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.exportCameras(os.path.join(output_dir, "cameras.xml"))
    chunk.exportPoints(path=os.path.join(output_dir, "")) = project_path), "projectGenerated.psx"))
    print("Script finished")
    return 1

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 10307
    • View Profile
Re: Adding scalebar to images in separate groups based on camera.label
« Reply #1 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)
Best regards,
Alexey Pasumansky,
AgiSoft LLC


  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Adding scalebar to images in separate groups based on camera.label
« Reply #2 on: March 21, 2019, 01:30:07 PM »
Thank you Alexey, that's exactly what I was trying to do.

Many Thanks,