In this example, I had a shape file I wanted to use as a mask. However, the features were those I wanted to keep. In other words, I wanted to keep all the pixels that had values (i.e. not “no data”) that intersected with the shapes, and set those outside the shapes to 0. Then, I needed to set the no data value to 0, and only keep “clumps” of pixels w/values that were 88 or more.
gdal_rasterize -i -burn 0 mask.shp input.tif gdal_translate -a_nodata 0 input.tif output.tif gdal_sieve.py -st 88 -8 output.tif
Voila! (The mask can also be a geo package, with the layer specified using the -l option.)