|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
from tifffile import imread, imwrite
from skimage import filters, feature, color
import matplotlib.pyplot as plt
import numpy as np
from osgeo import gdal, osr
# 读取TIFF格式无人机影像数据
image_path = 'F:\duneline\dune\dune.tif'
output_path = 'overlay_image5.tif'
image = imread(image_path)
# 转换为灰度图像
gray_image = color.rgb2gray(image)
# 边缘检测,提取沙丘脊线
edges = filters.sobel(gray_image)
# 设置合适的阈值来确定沙脊线的二值化图像
threshold = 0.03
binary = edges > threshold
# 叠加沙脊线在原始影像上
overlay = np.copy(image)
overlay[binary] = [255, 0, 0] # 将沙脊线部分标记为红色
# 配置输出的空间参考信息
in_ds = gdal.Open(image_path)
projection = in_ds.GetProjection()
geotransform = in_ds.GetGeoTransform()
# 创建新的输出图像
driver = gdal.GetDriverByName('GTiff')
out_ds = driver.Create(output_path, image.shape[1], image.shape[0], 1, gdal.GDT_Byte) # 修改数据类型为gdal.GDT_Byte
# 设置输出图像的空间参考信息
out_ds.SetProjection(projection)
out_ds.SetGeoTransform(geotransform)
# 将沙脊线部分写入输出图像
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(binary.astype(np.uint8)) # 修改为正确的数据类型
# 设置颜色表
color_table = gdal.ColorTable()
color_table.SetColorEntry(0, (0, 0, 0, 0)) # 黑色,对应值为0
color_table.SetColorEntry(255, (255, 255, 255, 255)) # 白色,对应值为255
out_band.SetColorTable(color_table)
# 释放资源
out_band.FlushCache()
out_ds = None
in_ds = None
print("保存成功!") |
|