Search code examples
pythonnumpyscikit-imagesatelliteno-data

Scikit-image read satellite image from tif and drop all rows/columns with a nodata value


I'm attempting to run a thresholding algorithm on a satellite-derived raster stored as a tif. The no data values (-3.40282306e+38) that occur on the borders of the scene are being considered in the algorithm and is causing undesired behaviour. I'm looking for a way to remove all rows/columns with a no data value prior to running the thresholding algorithm.

I've attempted to set all cells with the no data value to np.nan then dropping them but I'm not getting the results I'm looking for.

My code:

import numpy as np
from skimage import filters
from skimage import exposure
from skimage.io import imread, show
from skimage.filters import try_all_threshold

no_data_value = -3.40282306e+38

ndwi = imread(<'my.tif'>)
ndwi[ndwi == no_data_value] = np.nan
ndwi = ndwi[~np.isnan(ndwi).any(axis=1)]
val = filters.threshold_otsu(ndwi)

Solution

  • filters.threshold_otsu does not care about the shape of the array passed in, so you can also do:

    threshold_otsu(z[z > -1])