Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: vcc on December 11, 2017, 11:45:32 PM

Title: Help with script
Post by: vcc on December 11, 2017, 11:45:32 PM
Hi,

I am having a few problems with the below script. I am processing Multi-spectral data and an RGB data set of the same paddock. I run the project with one chunk (the first) as the multi-spectral and another chunk as RGB (as the second).

Once I get to the export stage of the script in the RGB section it always defaults to the multi-spectral chunk and exports it, in the script I have this
Code: [Select]
doc.chunk = doc.chunks[1] to change the chunk to the second in the project. 

I am running this script as part of a workflow and I have tried running the script on both chunks and just on the multispec (in the workflow). But it seams to always seems to do the same thing.

Any help would be appreciated

Code: [Select]
chunk = PhotoScan.app.document.chunk
doc = PhotoScan.app.document
doc.chunk = doc.chunks[0]
#Mosaic Blend
chunk.buildOrthomosaic(surface=PhotoScan.ModelData, blending=PhotoScan.MosaicBlending, color_correction=False, projection=chunk.crs)
multispec = "multispec"
RGB = "RGB"
#NDVI Band
chunk.raster_transform.formula = ["(B4-B2)/(B4+B2)"]
chunk.raster_transform.calibrateRange()
chunk.raster_transform.enabled = True
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_NDVI_ortho_mosaic_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize Green band
chunk.raster_transform.formula = ["(B1)"]
chunk.raster_transform.calibrateRange()
greenMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B1)/(" + str(greenMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_Green_ortho_mosaic_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize Red band
chunk.raster_transform.formula = ["(B2)"]
chunk.raster_transform.calibrateRange()
redMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B2)/(" + str(redMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_Red_ortho_mosaic_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize Red Edge band
chunk.raster_transform.formula = ["(B3)"]
chunk.raster_transform.calibrateRange()
rededgeMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B3)/(" + str(rededgeMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_Rededge_ortho_mosaic_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize NIR band
chunk.raster_transform.formula = ["(B4)"]
chunk.raster_transform.calibrateRange()
NIRMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B4)/(" + str(NIRMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_NIR_ortho_mosaic_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Compute normalized WDVI (Green)
chunk.raster_transform.formula = ["(B4)"]
chunk.raster_transform.calibrateRange()
nirMax = chunk.raster_transform.range[1]
chunk.raster_transform.reset()
chunk.raster_transform.formula = ["(B4 / (" + str(nirMax) + ")) - (1.5 * (B1 / " + str(greenMax) + "))"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_WDVI_Green_ortho_mosaic_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
#Average Blend
doc.chunk = doc.chunks[0]
chunk.buildOrthomosaic(surface=PhotoScan.ModelData, blending=PhotoScan.AverageBlending, color_correction=False, projection=chunk.crs)
#NDVI Band
chunk.raster_transform.formula = ["(B4-B2)/(B4+B2)"]
chunk.raster_transform.calibrateRange()
chunk.raster_transform.enabled = True
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_NDVI_ortho_average_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize Green band
chunk.raster_transform.formula = ["(B1)"]
chunk.raster_transform.calibrateRange()
greenMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B1)/(" + str(greenMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_Green_ortho_average_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize Red band
chunk.raster_transform.formula = ["(B2)"]
chunk.raster_transform.calibrateRange()
redMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B2)/(" + str(redMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_Red_ortho_average_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize Red Edge band
chunk.raster_transform.formula = ["(B3)"]
chunk.raster_transform.calibrateRange()
rededgeMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B3)/(" + str(rededgeMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_Rededge_ortho_average_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Normalize NIR band
chunk.raster_transform.formula = ["(B4)"]
chunk.raster_transform.calibrateRange()
NIRMax = chunk.raster_transform.range[1]
chunk.raster_transform.formula = ["(B4)/(" + str(NIRMax) + ")"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_NIR_ortho_average_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
# Compute normalized WDVI (Green)
chunk.raster_transform.formula = ["(B4)"]
chunk.raster_transform.calibrateRange()
nirMax = chunk.raster_transform.range[1]
chunk.raster_transform.reset()
chunk.raster_transform.formula = ["(B4 / (" + str(nirMax) + ")) - (1.5 * (B1 / " + str(greenMax) + "))"]
chunk.raster_transform.calibrateRange()
chunk.exportOrthomosaic("/path/to/dir//SQM/" + multispec + "_WDVI_Green_ortho_average_blend.tif", image_format = PhotoScan.ImageFormatTIFF, raster_transform = PhotoScan.RasterTransformValue, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
#DEM
chunk.exportDem("/path/to/dir//DSM/" + multispec + "_SQM_DSM_ortho.tif", image_format = PhotoScan.ImageFormatTIFF, write_world=True, tiff_big=False)
chunk.exportReport("/path/to/dir//Report/" + multispec + "_SQM_report.pdf")
#RGB
doc.chunk = doc.chunks[1]
chunk.raster_transform.reset()
#Mosaic Blend
chunk.buildOrthomosaic(surface=PhotoScan.ModelData, blending=PhotoScan.MosaicBlending, color_correction=False, projection=chunk.crs)
# RGB
chunk.exportOrthomosaic("/path/to/dir//RGB/" + RGB + "_RGB_ortho_mosaic_blend.tif", image_format = PhotoScan.ImageFormatTIFF, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
chunk.raster_transform.reset()
#Average Blend
chunk.buildOrthomosaic(surface=PhotoScan.ModelData, blending=PhotoScan.AverageBlending, color_correction=False, projection=chunk.crs)
# RGB
chunk.exportOrthomosaic("/path/to/dir//RGB/" + RGB + "_RGB_ortho_average_blend.tif", image_format = PhotoScan.ImageFormatTIFF, write_world=True, write_alpha=False, tiff_big=False, white_background=True)
#DEM
chunk.exportDem("/path/to/dir//DSM/" + RGB + "_RGB_DSM_ortho.tif", image_format = PhotoScan.ImageFormatTIFF, write_world=True, tiff_big=False)
chunk.exportReport("/path/to/dir//Report/" + RGB + "_RGB_report.pdf")
Title: Re: Help with script
Post by: Alexey Pasumansky on December 12, 2017, 12:03:20 AM
Hello vcc,

When you are doing RGB export you should use chunk = doc.chunks[1] assignment, otherwise you are making the second chunk active, but chunk variable hasn't been reassigned.
Title: Re: Help with script
Post by: vcc on December 12, 2017, 12:13:01 AM
Thanks Alexey,

I have that code in the script at

Code: [Select]
(#RGB
doc.chunk = doc.chunks[1]
chunk.raster_transform.reset()
#Mosaic Blend)

but it still doesn't call it can you highlight in my above script where I am going wrong?
Title: Re: Help with script
Post by: Alexey Pasumansky on December 12, 2017, 12:15:26 AM
Hello vcc,

It should be:
Code: [Select]
chunk = doc.chunks[1]
chunk.raster_transform.reset()

Now it e-assigns the chunk variable. In your code chunk is not re-assigned, you just switch the active chunk, but all the operations are applied to another one, since the unchanged variable is used.
Title: Re: Help with script
Post by: vcc on December 12, 2017, 12:18:17 AM
Thank you!!

That makes sense now, you have saved me quite a few hours of frustration