From Open Foris Wiki

Jump to: navigation, search

Regression based gap and cloud filler

Usage: oft-gapfill [-la nbrLargeAreaWindows] [-nolocal] [-smooth]

[-pm] [-da] [-sd sampling density] [-ws WindowSize] <-um maskfile> <inputfile> <outputfile>

-pm = print Large Area (LA) models and some additional information in <outputfile>.models file
-nolocal = use only LA models in filling
-smooth = smooth the LA models using models of the adjacent LA windows

oft-gapfill fills the gaps in an input image using locally built regression models. The models can be built

  1. separately for every gap pixel using a local model built using its adjacent pixels or
  2. for a given number of Large Area subsets or
  3. using both of these methods

  • In the case 2), the option -la followed by the number of requested Large Area (LA) subsets in X direction should be given. The total number of LA subsets is the square of the given parameter. If the user wants to use only Large Area -models, the option -nolocal should be used.
  • Maskfile, inputfile and outputfile are all required inputs. They may be in any of the formats understood by GDAL.
  • The input image is a stack of the Anchor image and the Filler image. The output values for Anchor are computed using Filler and the model. The input image bands should be organized as follows:
  • band 1 to nbr_bands/2 = Anchor image
  • bands nbr_bands/2 + 1 to nbr_bands = Filler image
  • The mask file shows the locations of the gaps, areas which are suitable for collecting training data, and areas which should not be processed. The mask values are as follows:
1 = fill these pixels (unusable data in anchor, good data in filler)
2 = collect training data for regression model (good data in both images)
3 = do nothing, i.e., use the original values (2 cases: good in anchor, bad in filler OR non-good in both images)
0 = do nothing (image margins)


  • -la (nbrLargeAreaWindows) = number of LA windows in X direction. The total number of LA windows will be the square of this parameter.
  • -da (do4allpixels) = use to built model to predict output value for every pixel of the anchor using the built models and the values of the Filler.
  • -sd (sampling density) = sampling density used to build the LargeArea model. Value two, for example, would force the algorithm to collect every other valid pixel within the scene to be used in building the model.
  • -ws (WindowSize) = size of the neighbourhood from which the data for local model construction is collected


oft-gapfill -la 2 -nolocal -sd 5 -ws 13 -um mymask.img my14bandimage.img filled.img

The program performs 2 passes over the image:

  • Pass1: collect the data to build the model
  • Pass2: fill the gaps with Large Area models.

Note: The input image can be produced from 2 image stacks (for instance, 2 Erdas imagine composites consisting of 7 bands). The script stack2images.bash produces the composite. It can also be produced from HDF-images that are stored in folders. The script stack2images_hdf.bash is for that purpose.

The model may be very sensitive to outliers. Therefore it is important that the mask value 2 is present only in location where both Anchor and Filler have valid data.

IMPORTANT: The stack and the mask must have been re-projected to the same geographical window and they do must have the same number of rows and cols


cd /home/...
  • As oft-gapfill only allows even number of bands, first, we need to adjust the number of bands of landsat_t1.tif (7 bands) landsat_t2.tif (6 bands):
gdal_translate landsat_t1.tif landsat_t1_6bands.tif -b 1 -b 2 -b 3 -b 4 -b 5 -b 6
  • oft-gapfill takes as input an image stack of the anchor (landsat_t2.tif) and the filler (landsat_t1.tif):
oft-stack -o stack.tif landsat_t2.tif landsat_t1_6bands.tif
  • Gapfilling with mask of the scan-line using a simple mask created with oft-calc in two steps


  • -if band 1 or band 6 are 0 put 1 (fill)
  • if band 7 or band 12 are 0 put 3 (do nothing)
  • else put 2 (collect training data for regression models)

Step 1:

oft-calc stack.tif tmp.tif
#1 0 = #6 0 = + 0 > 2 1 ?
#7 0 = #12 0 = + 0 > 2 3 ? 

Step 2:

oft-calc tmp.tif simple_mask.tif
#2 3 = #1 3 ?
  • Now, use oft-gapfill to fill the areas indicated as "1" in the mask:
oft-gapfill -la 1 -nolocal -pm -sd 2 -um simple_mask.tif stack.tif filled_la1_sd2_simplemask.tif

Output automatically processed: filled_la1_sd2_simplemask.tif

Original Landsat 7 After gap fill

Back to Open Foris Toolkit Main Page

Back to Tools & Exercises

Personal tools