|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面代码是想对无人机影像(RGB, TIff格式)处理,提取得到影像中的沙丘脊线,但是在对提取结果保存为带有和原始影像一样的投影坐标栅格结果后,用Arcgis软件打开是一个全黑的(结果图放在了下面)。是不是将结果保存时出了问题,请大佬帮我改写为正确的代码!
from tifffile import imread
import numpy as np
from osgeo import gdal, osr
# 读取TIFF格式无人机影像数据
image_path = 'F:\\duneline\\dune\\dune.tif'
output_path = 'overlay_image.tif'
image = imread(image_path)
# 将输入图像转换为灰度图像
gray_image = np.mean(image, axis=2)
# 自适应阈值二值化
binary = np.zeros_like(gray_image, dtype=np.uint8)
window_size = 51 # 窗口大小
k = 0.3 # 控制阈值的参数
for i in range(window_size//2, gray_image.shape[0]-window_size//2):
for j in range(window_size//2, gray_image.shape[1]-window_size//2):
window = gray_image[i-window_size//2:i+window_size//2+1, j-window_size//2:j+window_size//2+1]
threshold = np.mean(window) - k * np.std(window)
if gray_image[i, j] > threshold:
binary[i, j] = 255
# 配置输出的空间参考信息
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_UInt16)
# 设置输出图像的空间参考信息
out_ds.SetProjection(projection)
out_ds.SetGeoTransform(geotransform)
# 将沙脊线部分写入输出图像
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(binary * 255) # 将二值图像数据缩放到0-255的范围
# 设置颜色表
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("保存成功!")
结果:
你的代码看起来是正确的,但是有一点需要注意。你在二值化处理后,已经将binary数组的值设置为255,然后在写入输出图像时,你又将binary数组的值乘以255,这就导致了值的溢出,因此在Arcgis软件中打开时看到的是全黑的图像。
你应该直接将binary数组写入输出图像,而不是binary * 255。另外,你的输出图像数据类型设置为了gdal.GDT_UInt16,这是16位无符号整数,但是你的binary数组是8位无符号整数,所以你应该将输出图像的数据类型设置为gdal.GDT_Byte。
以下是修改后的代码:
from tifffile import imread
import numpy as np
from osgeo import gdal, osr
# 读取TIFF格式无人机影像数据
image_path = 'F:\duneline\dune\dune.tif'
output_path = 'overlay_image.tif'
image = imread(image_path)
# 将输入图像转换为灰度图像
gray_image = np.mean(image, axis=2)
# 自适应阈值二值化
binary = np.zeros_like(gray_image, dtype=np.uint8)
window_size = 51 # 窗口大小
k = 0.3 # 控制阈值的参数
for i in range(window_size//2, gray_image.shape[0]-window_size//2):
for j in range(window_size//2, gray_image.shape[1]-window_size//2):
window = gray_image[i-window_size//2:i+window_size//2+1, j-window_size//2:j+window_size//2+1]
threshold = np.mean(window) - k * np.std(window)
if gray_image[i, j] > threshold:
binary[i, j] = 255
# 配置输出的空间参考信息
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) # 直接写入binary数组
# 设置颜色表
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("保存成功!")
希望这个修改能解决你的问题。
|
-
|