Description
Step purpose
Purpose: to apply the Otsu threshold to the normalized difference.
This step is highlighted below:
Code
The otsu
Python script is a command-line tool for applying the Otsu threshold to a single input raster image.
It uses the click
, rasterio
, numpy
, skimage.filters
, and loguru
libraries.
Here's an overview of what the script does:
-
It defines a command-line interface using the
click
library, with a single argument for providing the file path of the input raster image on which you want to apply the Otsu threshold. -
The
otsu
function is the main entry point. It opens the input raster file specified as the argument. -
It reads the data from the input raster using
rasterio
and also copies the metadata (e.g.,projection
,geotransform
) of this raster to be used for the output. -
It applies the Otsu threshold to the input array by calling the
threshold
function. Thethreshold_otsu
function fromskimage.filters
is used to calculate the Otsu threshold. The thresholding process marks pixels as True or False based on whether they are greater than the calculated threshold. -
It creates an output raster named "otsu.tif" using
rasterio
. This output raster will have the same metadata as the input raster. -
It writes the binary image to the output raster using
dst_dataset.write()
.
The result, a binary image where pixel values are either True or False based on the thresholding, will be saved as "otsu.tif" in the same directory where the script is executed.
The script is executable as a command-line tool as its usage is:
Usage: app.py [OPTIONS] RASTER
Applies the Otsu threshold
Options:
--help Show this message and exit.
The Python code is provided here:
water-bodies/command-line-tools/otsu/app.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|