From Open Foris Wiki
oft-seg - image segmentation tool
Usage: oft-seg [-aw] [-region] [-ttest] [-th threshold] [-automax] [-4n] [-automin] [-um <maskfile>] <infile> <outfile>
-aw = Ask input band weights. -automax = Use automatically computed maximum distance threshold -4n = Describes the pixel connectivity. Default is -8n. -automin = Use automatically computed minimum distance threshold -region = Use "Segmentation with directed trees" -method for initial segmentation -th theshold = give a user defined threshold value for abovementioned initial segmentation -ttest = use t-value based merging criteria
-um maskfile = Use mask/initial segment file
If -4n is indicated, the neighbourhood is reduced to consider only top, bottom, left and right pixels.
Additional Options upon Execution:
-Min. segment size?: Minimum segment size in pixels. -Min. spec. dist. btw segs?: Not asked if -automin is specified above. -Max. spec. dist. btw segs?: Not asked if -automax is specified above. -Use size weighting?: 0 indicates no size weighting, 1 indicates use size weighting.
- oft-seg uses a simple iterative region merging algorithm to merge each segment with its spectrally nearest adjacent segment. The spectral distance (D) between the segments is computed using all input bands and Euclidean distance or t-value. In the latter case the t-value is computed using the equation for equal or unequal sample sizes, unequal variances
- The algorithm is controlled by three parameters: minimum segment size in pixels (MinSize), and minimum required (MinDist) and maximum allowed (MaxDist) spectral distances in the feature space. The conditional merging is done in two phases. First, all segments which are 1) smaller than MinSize and 2) have a neighbouring segment to which the spectral distance is < MaxDist are merged. This step is iterated until no such segments exist. After that, all segments which have an adjacent segment with D < MinDist are merged with their spectrally nearest neighbour.
- In addition, the user can choose to weight the distance computation with the size (pixels) of the neighbouring segment.
- The tool can also compute the MinDist and MaxDist thresholds automatically. To do that, use -automin and/or -automax options. Otherwise the tool will ask for user input.
- If you do not want to use MinDist or MaxDist parameters or size weighting, reply 0 when the parameter is asked.
- If the given MinSize is 0 and the program is being run without -region option, an image with unique labels for every pixel is produced. Otherwise using MinSize 0 will output initial segments produced using the other method.
- If a mask is given, initial segments are read from the mask.
- To do a hierarchical segmentation, the user should run the first iteration without a mask. In the subsequent iterations the resulting output of the previous segmentation step should be fed to the process using -um option.
- In case the input image is large and computing resources are low, an alternative method can be used. The initial segmentation can be produced using oft-cluster.bash oft-clump and the final removal of undesired small segments with oft-seg.
Note: A further tool oft-segstat can then be used to extract segment level shape (size, bounding box, # edge pixels) and spectral statistics (averages and standard deviations) to a text file.
1. Open your working directory using
2. Now we run oft-seg to do the hierarchical segmentation with Input: landsat_t1.tif; Output: landsat_t1_min50.tif
oft-seg landsat_t1.tif landsat_t1_min50.tif
The tool will ask you now further details which we will define in this exercise as followed:
- Please give NODATA value: 0
- Min. segment size?: 50
- Min. spec. dist. btw segs?: 0
- Max. spec. dist. btw segs?: 0
- Use size weighting?: 0
3. In the next step we create a shapefile where pixels of the same value, with other words of the same segment, combined into one polygon. Input: landsat_t1_min50.tif, Output: landsat_t1_min50.shp
gdal_polygonize.py landsat_t1_min50.tif -f "ESRI Shapefile" landsat_t1_min50.shp
4. Open your file landsat_t1_min50.tif in QGIS and overlay it with Llandsat_t1_min50.shp
- Right click of the shapefile -> Properties -> Label -> tick display label and under Field containing label chose DN
- Right click of the shapefile -> Properties -> Style -> Transparency eg 50%
- Now zoom in and you will see something similar to the image displayed, depending on the area you are zooming in, where each polygon refers to one segment and the displayed number is the corresponding ID.
Note: some segments have the same ID, but they still belong to the same segment as they are connect through neighbouring corner pixels
5. The segmentation image landsat_t1_min50.tif can be used in a further step for oft-segstat.