Generic Geometric Correction

These recipes are for casual geospatial imagery users. They use generic math models to improve the geometric accuracy of images so that they better align with other data sources, such as GIS road layers.

For many casual users of geospatial imagery, budgets may not be in place to obtain raw imagery from a vendor that can be processed with specific satellite and or aerial models. Rather, many GIS departments have to use available imagery. As a result, they often obtain imagery with nominal georeferencing, but due to unknown processing procedures, the imagery may not line up well with their GIS data (i.e. road vectors). Since the imagery is already processed, they cannot use the recipes in the Satellite Photogrammetry or Aerial Photogrammetry sections. These recipes can be used to improve the geometric accuracy of images.

Automatic GCP collection and 2D geometric correction with Polynomial Model

This recipe provides a quick and automatic method to geometrically correct imagery with georeferencing, but missing a math model or orbital information. This process includes automatic GCP collection and refinement.

This may be useful for improving the georeferencing of archive imagery.

from pci.link import link
from pci.autogcp import autogcp
from pci.gcprefn import gcprefn
from pci.polymodel import polymodel
from pci.ortho import ortho

#create PIX link file of input tif image for GCP collection
input_image = r"C:\generic\miscellaneous_satellite.tif"
ingest_link_file = r"C:\generic\miscellaneous_satellite.pix"

link(fili=input_image, filo=ingest_link_file)

#Automatic GCP collection using initial georeferencing only
reference_image = r"C:\generic\reference.tif"
num_gcps = [] #AUTOGCP2 returns the number of GCPs that were collected. Can use this for building intelligent scripts

gcp_seg = autogcp(mfile=ingest_link_file, dbic=[4], filref=reference_image, dbic_ref=[4], searchr=[25], numgcps=num_gcps)

#Refine GCPs based on generic 2D Polynomial model, which is built by the GCPs
refined_gcp_seg = [] #GCPREFN returns the segment number with the refined GCPs so that it can be provided into the next func.
out_stats = [] #GCPREFN populates this list with 11 useful statistics which can be used to build more intelligence into the GCP refinement process

gcprefn(file=ingest_link_file, dbgc=gcp_seg, modeltyp="POLY", reject=[5,1.0,1.0], lasc=refined_gcp_seg, imstat=out_stats)

#Compute a 3D generic math model (Thin Plate Spline) from the GCPs you collected and refined
polymodel(mfile=ingest_link_file, dbgc=refined_gcp_seg, order=[3])

#Geometrically correct the image with the tps model that was previously computed
geo_correct_image = r"C:\Geomatica_Cookbook\generic\corr_random_satellite.tif"
res_x = "2"
res_y = "2"

ortho(mfile=ingest_link_file, filo=geo_correct_image, ftype="TIF")

Automatic GCP collection from road vector layer with generic 3D Geometric Correction

For Some GIS organizations it is important that archive imagery lines up well with GIS layers, such as road vector layers. Unfortunately, these images may have been inaccurately corrected and only contain nominal georeferencing.

This recipe demonstrates how to geometrically correct an image with nominal georeferencing using automatic GCP collection and refinement from a vector road layer.

from pci.fftmvec import fftmvec
from pci.imageinv import imageinv
from pci.fimport import fimport
from pci.iii import iii
from pci.pcimod import pcimod
from pci.gcprefn import gcprefn
from pci.tpsmodel import tpsmodel
from pci.ortho import ortho

input_image = r"C:\generic_road\misc_satellite_image.pix"

#Invert the Near-Infrared channel - Helps FFTMVEC to successfully collect GCPs
inverse_image = r"C:\generic_road\inv_misc_img.pix"
inv_channel = [3] #Near-Infrared channel

imageinv(fili=input_image, filo=inverse_image, dbic=inv_channel)

#copy the inverted image channel into the ingested image we are going to collect GCPs for
pcimod(file=input_image, pciop="ADD", pcival=[0,0,0,1])
iii(fili=inverse_image, filo=input_image, dbic=[1], dboc=[5])

#Run FFTMVEC to automatically collect GCPs from road vectors
roadvec = r"C:\generic_road\roads\toronto_vector.pix"
gcp_seg = []

fftmvec(file=input_image, dbic=[1,2,4], dbinvc=[5], filv=roadvec, dbvs=[2], vecwidth=[15], lasc=gcp_seg)

#Refine GCPs - remove bad GCPs until RMSE falls below 1 pixel in x and y direction
refined_gcp_seg = []

gcprefn(file=input_image, dbgc=gcp_seg, modeltyp="POLY", reject=[5,1.0,1.0], lasc=refined_gcp_seg,)

#Compute a 3D generic math model (Thin Plate Spline) from the GCPs you collected and refined
tpsmodel(mfile=input_image, dbgc=refined_gcp_seg)

#Geometrically correct the image with the tps model that was previously computed
geo_correct_image = r"C:\generic_road\corr_misc_satellite.pix"
dem = r"C:\generic_road\torontodem.pix"

ortho(mfile=input_image, filo=geo_correct_image, ftype="TIF", filedem=dem)

Automatic GCP collection and 3D geometric correction with Thin Plate Spline Model

This recipe demonstrates how to geometrically correct an image with initial georeferencing using automatic GCP collection and refinement from a georeferenced image and DEM.

from pci.link import link
from pci.autogcp import autogcp
from pci.gcprefn import gcprefn
from pci.tpsmodel import tpsmodel
from pci.ortho import ortho

#create PIX link file of input tif image for GCP collection
input_image = r"C:\generic\miscellaneous_satellite.tif"
ingest_link_file = r"C:\generic\miscellaneous_satellite.pix"

link(fili=input_image, filo=ingest_link_file)

#Automatic GCP collection using initial georeferencing only
reference_image = r"C:\generic\reference.tif"
dem = r"C:\generic\dem.pix"
num_gcps = [] #AUTOGCP2 returns the number of GCPs that were collected. Can use this for building intelligent scripts

gcp_seg = autogcp(mfile=ingest_link_file, dbic=[4], filref=reference_image, dbic_ref=[4],
                  filedem=dem, searchr=[25], numgcps=num_gcps)

#Refine GCPs based on generic 2D Polynomial model, which is built by the GCPs
refined_gcp_seg = [] #GCPREFN returns the segment number with the refined GCPs so that it can be provided into the next func.
out_stats = [] #GCPREFN populates this list with 11 useful statistics which can be used to build more intelligence into the GCP refinement process

gcprefn(file=ingest_link_file, dbgc=gcp_seg, modeltyp="POLY", reject=[5,1.0,1.0], lasc=refined_gcp_seg, imstat=out_stats)

#Compute a 3D generic math model (Thin Plate Spline) from the GCPs you collected and refined
tpsmodel(mfile=ingest_link_file, dbgc=refined_gcp_seg)

#Geometrically correct the image with the tps model that was previously computed
geo_correct_image = r"C:\generic\corr_random_satellite.tif"

ortho(mfile=ingest_link_file, filo=geo_correct_image, ftype="TIF", filedem=dem)

Automatic GCP collection and 3D geometric correction with Thin Plate Spline Model - Batch Processing

This recipe demonstrates how to automatically discover all valid images in a directory, automatically collect GCPs, refine the GCPs based on RMSE and finally geometrically correct all valid images found using a Thin Plate Spline model (TPS).

import sys, fnmatch, os
from pci.link import link
from pci.autogcp import autogcp
from pci.gcprefn import gcprefn
from pci.tpsmodel import tpsmodel
from pci.ortho import ortho

raw_dir = r"D:\generic_batch\raw"
file_filter = "*.tif"

raw_image_list = []

for r,d,f in os.walk(raw_dir):
    for file in fnmatch.filter(f, file_filter):
        print "found valid input file: ", file
        raw_image_list.append(os.path.join(r,file))

if len(raw_image_list) > 0:
    for raw_image in raw_image_list:

        #Create .pix link file to store the GCP segement that holds the GCPs
        ingest_link_dir = r"D:\generic_batch\ingest"
        ingest_link_filename = os.path.splitext(os.path.basename(raw_image))[0] + ".pix"
        ingest_link_image = os.path.join(ingest_link_dir, ingest_link_filename)

        link(fili=raw_image, filo=ingest_link_image)

        #Automatic GCP collection using initial georeferencing only
        reference_image = r"D:\PCI_Work\Projects\Open\Marketing\Python_Cookbook\tests\generic_batch\reference\o2_5m.pix"
        dem = r"D:\PCI_Work\Projects\Open\Marketing\Python_Cookbook\tests\generic_batch\reference\torontodem.pix"
        num_gcps = [] #AUTOGCP2 returns the number of GCPs that were collected. Can use this for building intelligent scripts

        gcp_seg = autogcp(mfile=ingest_link_image, dbic=[2], filref=reference_image, dbic_ref=[2],
                  filedem=dem, searchr=[50], numgcps=num_gcps)

        #Refine GCPs based on generic 2D Polynomial model, which is built by the GCPs
        gcprefn(file=ingest_link_image, dbgc=refined_gcp_seg, modeltyp="POLY", reject=[5, 0.5, 0.5], lasc=refined_gcp_seg, imstat=out_stats)

        #Compute a 3D generic math model (Thin Plate Spline) from the GCPs you collected and refined
        tpsmodel(mfile=ingest_link_image, dbgc=refined_gcp_seg)

else:
    print "No image files found in " + raw_dir + " that match your filter criteria."
    sys.exit()

#Geometrically correct the images with the tps model
geo_correct_dir = r"D:\PCI_Work\Projects\Open\Marketing\Python_Cookbook\tests\generic_batch\corrected"

ortho(mfile=ingest_link_dir, filo=geo_correct_dir, ftype="TIF", filedem=dem)