Forum

Author Topic: Differentiating between Aligned and Non-Aligned Photos/Cameras  (Read 3779 times)

lvargo13

  • Newbie
  • *
  • Posts: 14
    • View Profile
Differentiating between Aligned and Non-Aligned Photos/Cameras
« on: October 21, 2016, 06:26:35 AM »
Hello,

I'm looking for a way in python to determine, after aligning photos, if certain photos aligned. Is there a command for determining this?

I've attached a screenshot of the Photos Pane, essentially I want to be able to write a loop so that if a camera in a list that I have does not align (camera 0313_002 in this example), I can skip the next step.

Thanks

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15029
    • View Profile
Re: Differentiating between Aligned and Non-Aligned Photos/Cameras
« Reply #1 on: October 21, 2016, 07:16:42 AM »
Hello lvargo13,

You can check if camera.transform is not None:
Code: [Select]
if camera.transform:
      print("camera is aligned")
else:
      print("not aligned")
Best regards,
Alexey Pasumansky,
Agisoft LLC

lvargo13

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Differentiating between Aligned and Non-Aligned Photos/Cameras
« Reply #2 on: October 25, 2016, 01:35:40 AM »
Hi Alexey,

It worked perfectly, thanks for the help.

And since so much of my work has been sourced from codes posted here, I thought I'd share mine as well. Below is my code that loops through multiple directories (representing different years), imports images from the year, and aligns images with an existing project. If non of the new images align, that is all, but if any of the new images align, the script continues through the PS workflow, with the ultimate goal of orthorectifying the images. The code takes a while to run depending on the accuracy of aligning points and the resolution of the dense cloud, so it also writes the results of each loop to a text file so that the progress can be checked while it's running.

Code: [Select]
#!/usr/bin/python

# python script for photoscan- loop through all years (directories) of glacier photos, run through PS workflow, and orthorectify glacier photos after matching

import PhotoScan
import os
import time
import glob

project_mask = "/Volumes/Brew_mask.psx"  # link to existing mask to use
old_photos_path = "/Volumes/BrewPhotos/"      # path to old photos
save_path =  "/Volumes/BrewProjects/"    # where to save projects
save_filepre = "brew"  # file name to save
file = open(save_path+"generate_orthophoto_log.txt", "w")   # log file
file.write("Generate Orthophotos, mask: %s\n" % project_mask)
file.close()

# loop through directory for each year of old photos
year_list = os.listdir(old_photos_path)  # list all directories (years)

for i in year_list:

    start = time.time()  # time each loop
    file = open(save_path+"generate_orthophoto_log.txt", "a")
    file.write("Year: %s\n" % i)

    # open existing mask
    doc = PhotoScan.app.document
    doc.open(project_mask)
    chunk = doc.chunk

    # go to correct directory, list photos in that directory (for that year)
    os.chdir(old_photos_path+i)
    photo_list = glob.glob("*.tif")  # list of paths for each tiff

    # build model
    chunk.addPhotos(photo_list)
    #crs = PhotoScan.CoordinateSystem("EPSG::2193")   # 2193 is NZTM 2000, this shouldn't be needed since mask is already in NZTM
    chunk.matchPhotos(accuracy=PhotoScan.LowAccuracy,preselection=PhotoScan.NoPreselection,filter_mask=True,keypoint_limit=100000,tiepoint_limit=4000)
    chunk.alignCameras()

    aligned_photos = []   # empty list
    for camera in chunk.cameras:
        if camera.label in photo_list and camera.transform:
            aligned_photos.append(camera)           # creates list of glacier photos that aligned

    a = len(aligned_photos)
    if a > 0:      # if loop to only go through workflow and orthorectify if glacier photos were matched
        chunk.buildDenseCloud(quality=PhotoScan.LowestQuality,filter=PhotoScan.ModerateFiltering)
        chunk.buildModel(surface=PhotoScan.HeightField,source=PhotoScan.DenseCloudData,face_count=PhotoScan.HighFaceCount,interpolation=PhotoScan.EnabledInterpolation)
        chunk.buildUV(mapping=PhotoScan.GenericMapping)
        #chunk.buildTexture(blending=PhotoScan.MosaicBlending, size=4096)     # don't  need this to build ortho, but can uncomment if wanted to build model
        chunk.buildOrthomosaic(surface=PhotoScan.ModelData,blending=PhotoScan.MosaicBlending,color_correction=False) # can add 'projection=crs' if need to specify projection

        # orthorectify photos
        for camera in chunk.cameras:
            camera.enabled = False     # disables all photos
        for camera in chunk.cameras:
            if camera.label in photo_list:
                camera.enabled = True    # enables EoSS photos
        chunk.exportOrthophotos(save_path+"ortho_{filename}.tif", projection = chunk.crs)

        doc.save(path = save_path+save_filepre+i+".psx")
        file.write("Photos aligned: %s\n" % aligned_photos)

    else:
        doc.save(path = save_path+save_filepre+i+".psx")
        file.write("No EoSS photos aligned from this year\n")

    end = time.time()
    compt = end - start
    file.write("time: %f\n" % compt)
    file.close()



« Last Edit: October 25, 2016, 01:37:56 AM by lvargo13 »