Article directory
The sentinel-1 remote sensing image (float32) downloaded from Google Earth Engine. For SAR images, the following preprocessing can be done before use:
- Remove SAR noise
- Replace points below the 2% percentile and above the 98% percentile
- Linear stretch converts the value range of sentinel-1 to 0-255
Of course, you can not do it, and it should not affect it.
read Tiff
import os
import cv2
import numpy as np
from osgeo import gdal
from scipy import ndimage
import matplotlib.pyplot as plt
def read_Tiff(filename):
dataset = gdal.Open(filename)
im_width = dataset.RasterXSize
im_height = dataset.RasterYSize
im_data = dataset.ReadAsArray(xoff = 0, yoff = 0, xsize = im_width, ysize = im_height)
return dataset, im_data
tiff_file = $FILE_DIR
tiff_dataset, tiff_data = read_Tiff(tiff_file)
median blur filter median filter
The median blur filter is a method for removing noise in SAR images. Specifically, the median value of a matrix of m*n size around a certain point is used to replace the value of a certain point. Specifically, it can be realized with ndimagescipy
in .
filter_data = ndimage.median_filter(tiff_data, size = 5, mode = 'nearest')
replace the percentile value
I don't know why I did this, but it's written in the paper. Seems like edge enhancement is possible.
import copy
per2 = np.percentile(filter_data[np.isnan(filter_data)==False],2)
per98 = np.percentile(filter_data[np.isnan(filter_data)==False],98)
print(per2, per98)
>> -21.94152530670166 2.2695427560806287
new_tiff_data = copy.deepcopy(filter_data)
new_tiff_data[new_tiff_data < per2] = per2
new_tiff_data[new_tiff_data > per98] = per98
print(np.nanmin(new_tiff_data), np.nanmax(new_tiff_data))
>> -21.941525 2.2695427
The value range becomes [0,255]
Convert a tiff image with a value range of [-22.294464, 2.497968] to [0,255]. The simplest linear stretch is used here. It can be implemented with a special function: cv2.normalize()
, refer to here , or write it yourself according to the following relation:
x ′ = x − minmax − min ∗ ( max ′ − min ′ ) + min ′ x' = \frac{x-min}{ max-min} * (max'-min') + min'x′=max−minx−min∗(max′−min′)+min’ Among them, max and min are the maximum and minimum values of the original array, and max' min' corresponds to 255 and 0.
value_convert_data = ((new_tiff_data - np.nanmin(new_tiff_data))/(np.nanmax(new_tiff_data) - np.nanmin(new_tiff_data))) * 255 + 0
hist = cv2.calcHist([value_convert_data[np.where(np.isnan(value_convert_data)==False)]],[0],None,[256],[0,255])
plt.figure()
plt.plot(hist)