Forum

Author Topic: Simple export undistorted images  (Read 6589 times)

megm

  • Newbie
  • *
  • Posts: 5
    • View Profile
Simple export undistorted images
« on: November 24, 2015, 09:41:13 PM »
HI all,
I'm a very new PhotoScan & Python user. I am in the process of switching over some Python code that works in older (?) PS1.1 to make it work in 1.2 and I'm stuck on something that should be fairly simple.

I am trying to export undistorted images from my chunk into a folder that I will create at the same time.
I see in the user manual for 1.2 that there is an undistort() command and Image.undistort(), but I'm stuck at how to actually use that in my code?

I have created the chunk and processed all my images, exported the model and cameras, and saved the project. This is my final step.

Any help for this beginner would be greatly appreciated.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14107
    • View Profile
Re: Simple export undistorted images
« Reply #1 on: November 25, 2015, 12:02:32 AM »
Hello megm,

Maybe you can post a part of the code that doesn't work and the error message that you've got in the Console pane?
Best regards,
Alexey Pasumansky,
Agisoft LLC

megm

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Simple export undistorted images
« Reply #2 on: November 25, 2015, 12:19:51 AM »
what worked in previous PS code:

for camera in doc.activeChunk.cameras:
        currentCameraImage = camera.image()
        undistortedCameraImage = currentCameraImage.undistort(camera.calibration,
                                                           center_principal_point=buildSettings.exportData.undistortedPhotos.centerPrinciplePoints,
                                                           square_pixels=buildSettings.exportData.undistortedPhotos.squarePixels)
        undistortedFullImageName = os.path.split(camera.path)[-1]
        undistortedImageName = os.path.splitext(undistortedFullImageName)[0]
        undistortedCameraImage.save(os.path.join(outputUndistortedDirPath, undistortedImageName + ".jpg"))


Error:

File "<console>", line 1
    for camera in doc.activeChunk.cameras:
        currentCameraImage = camera.image()
        undistortedCameraImage = currentCameraImage.undistort(camera.calibration,
                                                           center_principal_point=buildSettings.exportData.undistortedPhotos.centerPrinciplePoints,
                                                           square_pixels=buildSettings.exportData.undistortedPhotos.squarePixels)
        undistortedFullImageName = os.path.split(camera.path)[-1]
        undistortedImageName = os.path.splitext(undistortedFullImageName)[0]
        undistortedCameraImage.save(os.path.join(outputUndistortedDirPath, undistortedImageName + ".jpg"))
                                         
  ^
SyntaxError: invalid character in identifier

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14107
    • View Profile
Re: Simple export undistorted images
« Reply #3 on: November 25, 2015, 06:01:01 PM »
Hello megm,

Please check, if the following works for you:
Code: [Select]
doc = PhotoScan.app.document
chunk = doc.chunk #active chunk

for camera in chunk.cameras:
        currentCameraImage = camera.photo.image()
calibration = camera.sensor.calibration
        undistortedCameraImage = currentCameraImage.undistort(calibration, center_principal_point=buildSettings.exportData.undistortedPhotos.centerPrinciplePoints, square_pixels=buildSettings.exportData.undistortedPhotos.squarePixels)
        undistortedFullImageName = os.path.split(camera.photo.path)[-1]
        undistortedImageName = os.path.splitext(undistortedFullImageName)[0]
        undistortedCameraImage.save(os.path.join(outputUndistortedDirPath, undistortedImageName + ".jpg"))

The differences are the following:
doc.activeChunk --> doc.chunk
camera.image() --> camera.photo.image()
camera.path --> camera.photo.path
camera.calibration --> camera.sensor.calibration
« Last Edit: November 25, 2015, 10:00:03 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

megm

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Simple export undistorted images
« Reply #4 on: November 25, 2015, 10:06:57 PM »
Unfortunately I still can't get it to work. I have modified my code as follows:

Working code :

# photoscan doc
            doc = PhotoScan.app.document
           
            # create a new chunk in the workspace
            chunk = PhotoScan.app.document.addChunk()
 
            # add photos to the chunk
            #this adds them one at a time
            #need for loop to add 20 cameras at once

            chunk.addPhotos(["M:\TCL16\scans\D20\Session1\subject1\AU02_01\Captured_Photos\Tiff\FullColor\camera01_14.tif"])
           
            # Match Photos with accuracy setting and preselection
            chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.GenericPreselection)
 
            # Align Photos
            chunk.alignCameras()
           
            #Build Dense Cloud
            chunk.buildDenseCloud(quality=PhotoScan.LowQuality)
 
            #surface setting = Arbitrary setting is part of the Build Mesh processing in the old Batch Processing menu
            #Interpolation is part of the advanced setting in Build Mesh job

            chunk.buildModel(surface=PhotoScan.Arbitrary, interpolation=PhotoScan.Extrapolated)
           
            #Build Texture choice is blending mode and texture size (atlas height)
            #buildUV is mapping mode choice

            chunk.buildUV(mapping=PhotoScan.GenericMapping)
            chunk.buildTexture(blending=PhotoScan.MosaicBlending, size=1024)                   
                       
            # Export
            chunk.exportCameras("M:\TCL16\scans\D20\Session1\subject1\AU02_01\Scan_Data\subject1_01_ScanCameras.xml")
           
            #Export Model files (.jpg, .mtl, .obj)           
            chunk.exportModel("M:\TCL16\scans\D20\Session1\subject1\AU02_01\Scan_Data\subject1_01_ScanModel.obj")
 
After this code, I added:
               #need for loop to add 20 cameras at once
                camera = chunk.cameras[0] #first camera
                image = camera.photo.image()
                for camera in chunk.cameras:
                                currentCameraImage = camera.photo.image()
                                calibration = camera.sensor.calibration()
                                undistortedCameraImage = currentCameraImage.undistort(calibration, center_principal_point=buildSettings.exportData.undistortedPhotos.centerPrincipalPoints, square_pixels=buildSetting.exportData.undistortedPhotos.squarePixels)
                                undistortedFullImageName = os.path.split(camera.photo.path) [-1]
                                undistortedImageName = oc.path.plitext(undistortedFullImageName)

                                undistortedCameraImage.save(os.path.join(outputUndistortedDirPath, undistortedImageName +”.jpg”))
 
I hit enter twice, it returns:
Traceback (most recent call last):
File "<console>", line 3, in <module>
TypeError: 'PhotoScan.Calibration' object is not callable


I'm stuck at this point. It was suggested I can use the image.undistort() function after I have the image, with the arguments PhotoScan.Calibration() but I'm not sure what this should look like.

Thanks again for this help.
m

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14107
    • View Profile
Re: Simple export undistorted images
« Reply #5 on: November 25, 2015, 11:37:36 PM »
Hello megm,

Please try the following for the single camera:

Code: [Select]
camera = chunk.cameras[0]
image = camera.photo.image()
calibration = camera.sensor.calibration
undist = image.undistort(calibration, True, True)
undist.save(path) #path should be defined

In the previous code acidentally I've typed camera.sensor.calibration with parenthesis, but there shouldn't be any.
Best regards,
Alexey Pasumansky,
Agisoft LLC

megm

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Simple export undistorted images
« Reply #6 on: November 26, 2015, 12:19:19 AM »
I get a false result. (but no errors)

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14107
    • View Profile
Re: Simple export undistorted images
« Reply #7 on: November 26, 2015, 11:09:43 AM »
Hello megm,

Probably the export path is incorrect or application has no rights to write there.

Just tried the same code for random project and it worked for the local drive export: path = "D:\\undist.jpg", but I've got False when trying to save the image on the system drive root folder.
Best regards,
Alexey Pasumansky,
Agisoft LLC

megm

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Simple export undistorted images
« Reply #8 on: November 26, 2015, 10:35:55 PM »
that was it exactly.
Thank you so much for your help!