import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
from osgeo import gdal, osr, ogr, gdalconst
import os,glob,time,rasterio
import shutil
import rasterio.plot
import geopandas as gpd
练习2 基于遥感指数的水体提取
1. 方法介绍
阈值法是比较简单但却较为有效的水体提取方法,优点是物理含义明确,计算简单,效率高,缺点一是在一些复杂场景中,受建筑阴影等影响容易出现误识别,二是基于像元计算存在的“椒盐现象”,三是最大的确定是阈值需要人为设定。虽然这是一个非常原始的方法,但即使在今天依然有很大应用价值,比如作为一种特征与其他类型数据做融合处理,结合机器学习优化阈值设定的问题,还可以在一些广域识别应用中起到高置信样本筛选的作用等等。 NDWI(归一化差异水体指数):
\[ NDWI = (GREEN-NIR)/(GREEN+NIR) \]
式中GREEN表示绿光波段的反射率,NIR表示近红外波段的反射率。该指数的构建是利用了水体光谱在近红外区间反射率几乎为零的特性,更适合要素均匀分区且类型相对单一的自然场景中。 MNDWI(改进的归一化差异水体指数): \[ MNDWI=(GREEN-SWIR)/(GREEN+SWIR)\]
式中GREEN表示绿光波段的反射率,SWIR表示中红外波段的反射率。该方法是NDWI的变种,将NDWI中的近红外替换成短波红外波段,提高了水体与建筑物等地物的可区分度,该指数较为适合于城镇水体信息的提取,然而大部分高分卫星数据由于缺少短波红外波段而无法使用该指数。
2. 数据读取
## 读图像文件
def read_img(filename):
= gdal.Open(filename) # 打开文件
dataset
= dataset.RasterXSize # 栅格矩阵的列数
im_width = dataset.RasterYSize # 栅格矩阵的行数
im_height # im_bands = dataset.RasterCount # 波段数
= dataset.GetGeoTransform() # 仿射矩阵,左上角像素的大地坐标和像素分辨率
im_geotrans = dataset.GetProjection() # 地图投影信息,字符串表示
im_proj = dataset.ReadAsArray(0, 0, im_width, im_height)
im_data
del dataset #关闭对象dataset,释放内存
# return im_width, im_height, im_proj, im_geotrans, im_data,im_bands
return im_proj, im_geotrans, im_data, im_width,im_height
## 将numpy形式的遥感影像写出为GeoTiff文件
def Write_Tiff(img_arr, geomatrix, projection,path):
# img_bands, img_height, img_width = img_arr.shape
if 'int8' in img_arr.dtype.name:
= gdal.GDT_Byte
datatype elif 'int16' in img_arr.dtype.name:
= gdal.GDT_UInt16
datatype else:
= gdal.GDT_Float32
datatype
if len(img_arr.shape) == 3:
= img_arr.shape
img_bands, img_height, img_width = gdal.GetDriverByName("GTiff")
driver = driver.Create(path, int(img_width), int(img_height), int(img_bands), datatype)
dataset # print(path, int(img_width), int(img_height), int(img_bands), datatype)
if(dataset!= None) and (geomatrix !='') and (projection!=''):
#写入仿射变换参数
dataset.SetGeoTransform(geomatrix) #写入投影
dataset.SetProjection(projection) for i in range(img_bands):
+1).WriteArray(img_arr[i])
dataset.GetRasterBand(idel dataset
elif len(img_arr.shape) == 2:
# img_arr = np.array([img_arr])
= img_arr.shape
img_height, img_width =1
img_bands#创建文件
= gdal.GetDriverByName("GTiff")
driver = driver.Create(path, int(img_width), int(img_height), int(img_bands), datatype)
dataset # print(path, int(img_width), int(img_height), int(img_bands), datatype)
if(dataset!= None):
#写入仿射变换参数
dataset.SetGeoTransform(geomatrix) #写入投影
dataset.SetProjection(projection) 1).WriteArray(img_arr)
dataset.GetRasterBand(del dataset
## 计算数据头尾分位数的方式进行归一化,剔除异常值
def stretch(band, lower_percent=2, higher_percent=98): #2和98表示分位数
=np.array(band,dtype=np.float32)
band= np.percentile(band, lower_percent)*1.0
c = np.percentile(band, higher_percent)*1.0
d <c] = c
band[band>d] = d
band[band= (band - c) / (d - c)
out return out.astype(np.float32)
def stretch_n(data, n_band=3): #该操作讲改变原始数据,因此data用.copy,不对原始数据进行更改
=np.array(data,dtype=np.float32)
datafor k in range(n_band):
= stretch(data[:,:,k])
data[:,:,k] return data
def rgb(img_data,iftran=True):
= img_data[:3,:,:] # 取前三个波段 B02,B03,B04
img_data_3b if iftran:
= img_data_3b[::-1,:,:] # 将B02,B03,B04转成B04,B03,B02 (BGR转RGB)
img_data_3b = img_data_3b.transpose(1,2,0) # C,H,W -> H,W,C
img_data return img_data
读取多波段影像,剔除异常值,并提取rgb信息进行可视化
= "./data/T50TLK_20220825T030519/T50TLK_20220825T030519_clip.tif"
new_stack ='./data/T50TLK_20220825T030519/'
out_path= read_img(new_stack)
proj, geotrans, img_data, row, column # 显示重投影结果信息
print(f'仿射矩阵信息:{geotrans}',f'投影信息:{proj}',f'图像大小:{img_data.shape}')
=rgb(img_data)
img_data_ plt.imshow(stretch_n(img_data_.copy()))
仿射矩阵信息:(371350.0, 10.0, 0.0, 4480810.0, 0.0, -10.0) 投影信息:PROJCS["WGS 84 / UTM zone 50N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32650"]] 图像大小:(7, 2874, 3789)
<matplotlib.image.AxesImage at 0x246c2062c40>
3. 水体指数计算
- NDWI(归一化差异水体指数):NDWI = (GREEN-NIR)/(GREEN+NIR)
- MNDWI(改进的归一化差异水体指数):MNDWI=(GREEN-SWIR)/(GREEN+SWIR)
- 通过直方图观察数值分布情况,初步定位阈值区间
= img_data[1,:,:]
Green_arr = img_data[3,:,:]
NIR_arr
= np.array(Green_arr + NIR_arr, dtype=np.float32)
denominator = np.array(Green_arr - NIR_arr, dtype=np.float32)
numerator = np.full((Green_arr.shape[0], Green_arr.shape[1]), -2, dtype=np.float32)
nodata = np.true_divide(numerator, denominator, out=nodata, where=denominator != 0.0)
ndwi print(np.min(ndwi),np.max(ndwi))
== -2.0]=None
ndwi[ndwi
=out_path+'ndwi.tif'
ndwipath
Write_Tiff(np.uint8(ndwi), geotrans,proj, ndwipath)
= plt.subplots(1,3,figsize=(20,5))
fig, axes 1,3,1),plt.title('RGB'), plt.axis('off')
plt.subplot(
plt.imshow(stretch_n(img_data_.copy()))1,3,2),plt.title('NDWI'), plt.axis('off')
plt.subplot(
plt.imshow(ndwi)# plt.style.use("ggplot")
1,3,3),plt.title('Histogram'), plt.axis('off')
plt.subplot(=100, density=None, facecolor='green', alpha=0.75)
plt.hist(ndwi.ravel(), bins plt.show()
0.0 30.031637
通过可视化查看不同阈值条件下水体提取效果
= plt.figure(figsize=(20,6))
fig for i in range(5):
= ndwi.copy()
ndwi_ = (i+1) *0.05
threshold 1,5,i+1),plt.title('threshold= %.3f' % threshold), plt.axis('off')
plt.subplot(>threshold] = 255
ndwi_ [ndwi_ <=threshold] = 0
ndwi_ [ndwi_
plt.imshow(Image.fromarray(np.uint8(ndwi_)))
plt.show()= plt.figure(figsize=(20,6))
fig for i in range(5):
= ndwi.copy()
ndwi_ = (i+1) *2.5
threshold 1,5,i+1),plt.title('threshold= %.3f' % threshold), plt.axis('off')
plt.subplot(>threshold] = 255
ndwi_ [ndwi_ <=threshold] = 0
ndwi_ [ndwi_
plt.imshow(Image.fromarray(np.uint8(ndwi_))) plt.show()
对比NDWI和mNDWI的效果
= img_data[1,:,:]
Green_arr = img_data[5,:,:]
SWIR_arr
= np.array(Green_arr + SWIR_arr, dtype=np.float32)
denominator = np.array(Green_arr - SWIR_arr, dtype=np.float32)
numerator = np.full((Green_arr.shape[0], Green_arr.shape[1]), -2, dtype=np.float32)
nodata = np.true_divide(numerator, denominator, out=nodata, where=denominator != 0.0)
mndwi == -2.0]=None
mndwi[mndwi = plt.subplots(1,3,figsize=(20,5))
fig, axes 1,3,1),plt.title('RGB'), plt.axis('off')
plt.subplot(
plt.imshow(stretch_n(img_data_.copy()))1,3,2),plt.title('NDWI'), plt.axis('off')
plt.subplot(
plt.imshow(ndwi)1,3,3),plt.title('mNDWI'), plt.axis('off')
plt.subplot(
plt.imshow(Image.fromarray(np.uint8(mndwi))) plt.show()
= plt.figure(figsize=(20,6))
fig for i in range(5):
= mndwi.copy()
ndwi_ = (i+1) *0.05
threshold 1,5,i+1),plt.title('threshold= %.3f' % threshold), plt.axis('off')
plt.subplot(>threshold] = 255
ndwi_ [ndwi_ <=threshold] = 0
ndwi_ [ndwi_
plt.imshow(Image.fromarray(np.uint8(ndwi_)))
plt.show()= plt.figure(figsize=(20,6))
fig for i in range(5):
= mndwi.copy()
ndwi_ = (i+1) *2.5
threshold 1,5,i+1),plt.title('threshold= %.3f' % threshold), plt.axis('off')
plt.subplot(>threshold] = 255
ndwi_ [ndwi_ <=threshold] = 0
ndwi_ [ndwi_
plt.imshow(Image.fromarray(np.uint8(ndwi_))) plt.show()
阈值分割进行二值化处理得到水体掩膜
= ndwi.copy()
ndwi_ = 2.5
threshold >threshold] = 255
ndwi_ [ndwi_ <=threshold] = 0
ndwi_ [ndwi_ ='gray')
plt.imshow(Image.fromarray(np.uint8(ndwi_)),cmap=out_path+'out_ndwi.tif'
out_ndwi Write_Tiff(np.uint8(ndwi_), geotrans,proj, out_ndwi)
4. 去除小图斑
- 使用阈值分割法会使图像上布满不规则小图斑,影响分割精度
- 利用滤波等候处理方法降低结果噪声
def Speckle_removal(tif_path, save_path, remove_pixels =100, neighbours = 8 ):
= os.path.basename(tif_path)
filename = os.path.join(save_path, filename[:-4] + '_sr.tif' )
output_path
if not os.path.exists(save_path):
os.makedirs(save_path)
shutil.copy(tif_path, output_path)
# remove_pixels =100 #碎斑像素
# neighbours = 8 #连通域, 4或8
= gdal.Open(output_path, 1) # open image in read-write mode
Image = Image.GetRasterBand(1)
Band =Band, maskBand=None, dstBand=Band,
gdal.SieveFilter(srcBand= remove_pixels,
threshold= neighbours,
connectedness=gdal.TermProgress_nocb)
callback
del Image, Band # close the datasets.
return output_path
= Speckle_removal(out_ndwi, out_path, remove_pixels =1000, neighbours = 8 )
sr_out = read_img(sr_out)
proj, geotrans, sr_arr, row, column sr_arr.shape
(2874, 3789)
对比后处理前后变化
= read_img(sr_out)
proj, geotrans, sr_arr, row, column = plt.subplots(1,2,figsize=(20,5))
fig, axes 1,3,1),plt.title('NDWI'), plt.axis('off')
plt.subplot(
plt.imshow(Image.fromarray(np.uint8(ndwi_)))1,3,2),plt.title('NDWI_sr'), plt.axis('off')
plt.subplot(
plt.imshow(sr_arr) plt.show()
C:\Users\Administrator\AppData\Local\Temp\ipykernel_28856\1761671902.py:3: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed.
plt.subplot(1,3,1),plt.title('NDWI'), plt.axis('off')
C:\Users\Administrator\AppData\Local\Temp\ipykernel_28856\1761671902.py:5: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed.
plt.subplot(1,3,2),plt.title('NDWI_sr'), plt.axis('off')
二值化栅格转矢量输出
def tif_to_shp(tif_path, shp_save_path):
= gdal.Open(tif_path) # 读取路径中的栅格数据
inraster = inraster.GetRasterBand(1) # 这个波段就是最后想要转为矢量的波段,如果是单波段数据的话那就都是1
inband = osr.SpatialReference()
prj # 读取栅格数据的投影信息,用来为后面生成的矢量做准备
prj.ImportFromWkt(inraster.GetProjection()) = shp_save_path + os.path.basename(tif_path)[:-4] + '.shp' # 给后面生成的矢量准备一个输出文件名,这里就是把原栅格的文件名后缀名改成shp了
outshp
= ogr.GetDriverByName("ESRI Shapefile")
drv if os.path.exists(outshp): # 若文件已经存在,则删除它继续重新做一遍
drv.DeleteDataSource(outshp)= drv.CreateDataSource(outshp) # 创建一个目标文件
Polygon = Polygon.CreateLayer(os.path.basename(tif_path)[:-4], srs=prj, geom_type=ogr.wkbMultiPolygon) # 对shp文件创建一个图层,定义为多个面类
Poly_layer = ogr.FieldDefn('value', ogr.OFTReal) # 给目标shp文件添加一个字段,用来存储原始栅格的pixel value,浮点型,
newField
Poly_layer.CreateField(newField)
None, Poly_layer, 0) # 核心函数,执行的就是栅格转矢量操作
gdal.Polygonize(inband,
Polygon.SyncToDisk()= None
Polygon return outshp
# 执行转换函数,获取存储路径
= tif_to_shp(sr_out,out_path) ndwi_shp
5. 成果出图
- 利用rasterio更容易实现将shape文件与栅格影像进行叠加显示
- plt.imshow更适合临时出图,更专业的出版需求通常采用axs这种方式实现
= plt.subplots(2, 2, figsize=(12,10))
fig, axs
= gpd.read_file(ndwi_shp)
vector =rasterio.open(out_path+"T50TLK_20220825T030519_quickclip.tif")
img
=rasterio.open(ndwipath)
ndwiimg=rasterio.open(sr_out)
srimg=rasterio.plot.show(img, ax=axs[0,0],title='RGB')
p1 =rasterio.plot.show(ndwiimg, ax=axs[0,1],title='NDWI')
ndwi =rasterio.plot.show(srimg, ax=axs[1,0],title='Water Mask')
sr =rasterio.plot.show(img, ax=axs[1,1],title='Water Ploygon')
p1 =axs[1,1],edgecolor='red', linewidth=0.5,facecolor='none')
vector.plot(ax'Water Extraction Steps', fontsize=40)
fig.suptitle(
for ax in fig.get_axes():
ax.label_outer()='plain')
ax.ticklabel_format(style
fig.tight_layout()=None, bottom=None, right=None, top=0.9, wspace=None, hspace=0.2)
plt.subplots_adjust(left
+"T50TLK_20220825T030519_result.png",facecolor='white') fig.savefig(out_path
想了解更多请关注[45度科研人]公众号,欢迎给我留言!