[Record] Tiff image pre-processing, median blur filter and linear stretch


The sentinel-1 remote sensing image (float32) downloaded from Google Earth Engine. For SAR images, the following preprocessing can be done before use:

  1. Remove SAR noise
  2. Replace points below the 2% percentile and above the 98% percentile
  3. 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')

The left picture is the original picture, and the right picture is the result after the median filter.

The left picture is the original picture, and the right picture is the result after the median filter.

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 picture on the left is the result after the median filter is applied, and the picture on the right is the result after the application of the percentage.

The picture on the left is the result after the median filter is applied, and the picture on the right is the result after the application of the percentage.

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=maxminxmin(maxmin)+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

insert image description here

Convert to the result after 0-255.
Finally draw a histogram of pixels
hist = cv2.calcHist([value_convert_data[np.where(np.isnan(value_convert_data)==False)]],[0],None,[256],[0,255])
plt.figure()
plt.plot(hist)

Histogram without percentile processing

Histogram without percentile processing.

Histogram with percentile processing

Histogram for percentile processing.

Guess you like

Origin blog.csdn.net/yaoyao_chen/article/details/128411952