I'm using a machine learning model to automatically recognize (a specific type of) vegetation in RGB imagery. The output of this model is a binary NumPy 2d array, with '1' meaning vegetation and '0' meaning no vegetation. The output is basically a mask. For writing this array into a .TIF file, I use the Python
rasterio library (see code below).
import rasterio as rs [...] tile_dest = rs.open(tile_path) img = Image.open(tile_path) img = np.array(img) inference = inference_function(img) inference_fname = output_folder_path / tile with rs.Env(): profile = tile_dest.profile profile.update( nodata=0, count=1) # Storing .tif image in original CRS with rs.open(inference_fname, 'w', **profile) as dst: dst.write(inference.astype(rs.uint8), 1)
This all works perfectly fine; I can add the .TIF file as a raster layer in QGIS. My problem, however, is that the raster layer has 2 values: 1 and 2. As you can see in the code above, the 0 value has already been assigned as 'nodata'. I do not want to have two values, because the resulting raster layer should be a mask of the vegetation find in the RGB imagery. The resulting mask, added as a raster layer in QGIS and overlaying the original RGB image, can be seen in the image at the end of this post.
I do not want to have two separate values (1 - black and 2 - white). Instead, I want to have one single value, representing the vegetation mask.
I think that I get the two values due to compression, but from what I've read in the
rasterio documentation, the only types of compression available for .TIF files are LZW and JPEG which both could create 'new' values during compression. How do I avoid altering the data range during the writing of a .TIF file with
EDIT: I've also tried to change the type of compression, but both JPEG and LZW produce the same result. Setting the NBITS parameter to 0 also didn't help.