From Open Foris Wiki

Jump to: navigation, search

Perform nearest neighbour estimation or classification of an image

Usage: oft-nn <-i input_image> <-o output_image/-or output text file> [OPTIONS]


  • oft-nn classifies or estimates an output value for every image analysis unit using given training data set and k nearest neighbour algorithm'. Nearest neighbours are determined based on Euclidean distances in the feature space.
  • In a classification, the output is the class having the largest sum of weights. In estimation, the output value is computed as straight or weighted average of the k nearest neighbours.
  • You need to give at least the input image file (-i option) and the output image (-o option) OR the output text file (-or option)
  • Note: Program will ask for the datafile, number and location of target variables, number of neighbours (k) and data type (continuous or class). Other parameters are asked when needed, if you use extra options specified below.


 -h = help
 -ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64} = define output type
 -um <maskfile> = only areas having mask value larger than 0 are processed
 -dem <demfile> = use given dem and vertical distance rules prompted by the program 
 -hrules = use horizontal distance rules (prompted by the program) to restrict the search in horizontal direction
 -segme = use segments in the mask file. If this option is used, the processing is done at the segment level. 
 -speed = approximate k-nn, asks for speed parameter. Experimental.
 -or <output_txtfile> = save weights for training data records for later calculations of large area statistics
 -aw = ask weights for the input bands
 -dw {1/2/3} = weight the nearest neighbor data with 1=equal, 2=inverse distance, 3=inverse distance squared (default) weights
 -norm = normalize the image features and the training data features to mean 0 and std 1 (default is no normalization). 
 -lu <image> = use given land use image for stratification of the reference data. 
 NOT IMPLEMENTED YET -adm <image> = use given administrative borders to collect weights for field plots by 
 administrative unit (e.g. county). This enables you to compute statistics for each adm. unit separately.
  • The last columns of the training data set are used as the feature space. For example, if the input image has four bands, the last four columns of the training data set should correspond to the values for training observations.
  • In cases of -dem or -lu you need to have a corresponding column in your field data text file (prompted by the program).
  • In case of -dem is used, we use absolute difference: if you want to reject observations > 500 m above or below the target pixel, give 500
  • In case of -norm, the normalization parameters are computed from the field data.
NOTE: you may also normalize your features (image and training data) BEFORE using oft-nn, Just be sure that the values come from the same distribution.
  • In case of -or the output text file contains the target variable and collected weight for each training data observation.
  • If the -lu option is given, only observations from the same land use category/class will be used for estimation.


1. You will need for this exercise the following data: landsat_t1.tif and landuse.shp which was digitized manually in QGIS

2. Create the signature file using oft-sigshp.bash

cd /home/.../OFGT-Data
oft-sigshp.bash images/landsat_t1.tif shapefiles/landuse id newcol txt/sig_landuse.txt

3. Take a look at the input signature file sig_landuse.txt:

more txt/sig_landuse.txt
 14 4 54.872263 26.561314 28.113869 58.320438 75.259854 129.021898 33.874453 
 15 4 58.635842 29.131097 35.067535 50.379166 86.387111 131.054293 47.649746 
 16 4 58.217101 29.102204 34.695057 54.351035 82.787575 130.169673 43.795925 
 17 1 54.840000 25.463590 29.768205 43.720000 80.614359 132.413333 42.431795 
 18 2 54.172608 25.085366 28.419325 48.404315 74.633208 131.336773 37.128518 
 19 3 55.198990 26.094949 30.674747 49.970707 76.598990 131.734343 36.209091 
 20 2 57.269874 26.903766 31.171548 42.291841 78.776151 133.120293 41.883891 
 21 5 55.277745 26.597769 29.771580 56.949501 76.772754 128.934234 36.727540 
 22 4 54.130526 24.966316 29.842105 42.627368 85.372632 134.662105 45.390526 
 23 4 54.960094 26.014085 28.808685 54.773474 75.338028 129.531690 34.167840 
 24 1 57.802077 27.928833 34.113622 48.773060 83.804520 132.198839 43.640501 
 25 3 58.298009 28.367690 33.835545 48.340315 82.241186 132.243467 45.336790

Explanation of columns:

 col 1:   ID of the polygon
 col 2:   landuse class of the polygon
 col 3-9: pixel values of band1-band7 of the Landsat imagery

4. Now run oft-nn with

oft-nn -i images/landsat_t1.tif -o results/my_knn.tif

Following variables will be asked:

 Input signature file name?:text/sig_landuse.txt
 Number of k?:5
 Nbr of output variables?:1
 Cols of 1 output vars in sig file?
 Output var 1: 2  //Here we define col2 where the information on landuse-classes is stored in sig_landuse.txt
 Class/Other = (0/1)?: 1

5. Load your result my_knn.tif in QGIS:

You can see the polygons labelled corresponding to their landuse-class on top of our result my_knn.tif, of which the pixel values vary between 1-5 (eg 1.78283) as there are 5 landuse-classes (1,2,3,4,5).

Result my_knn.tif overlayed with landuse.shp.

Back to Open Foris Toolkit Main Page

Back to Tools & Exercises

Personal tools