def extract_building_shadow(image, dsm, ground_spacing, radius): shadow_mask = np.zeros_like(image, dtype=np.bool) for i in range(0, image.shape[0], ground_spacing): for j in range(0, image.shape[1], ground_spacing): if not np.any(shadow_mask[i, j]): center = (i, j) ground_height = dsm[i, j] for x, y in spiral_search(center, radius, ground_spacing): if x < 0 or x >= image.shape[0] or y < 0 or y >= image.shape[1]: continue if np.any(shadow_mask[x, y:]): continue height = dsm[x, y] if height > ground_height: shadow_mask[x, y] = True elif height == ground_height: if np.linalg.norm(np.array([x, y]) - np.array(center)) < \ np.linalg.norm(np.array([i, j]) - np.array(center)): shadow_mask[x, y] = True return shadow_mask image_path = 'C:\yingxiang\DJI_20230322140516_0026_V.JPG' image_dataset = gdal.Open(image_path) dsm_path = 'C:/sanwei/jianmo/Productions/Production_2/Production_2_DSM_part_2_2.tif' dsm_dataset = gdal.Open(dsm_path) # 读取数据 image = image_dataset.ReadAsArray() dsm = dsm_dataset.ReadAsArray() # 获取地面分辨率 ground_spacing = dsm_dataset.GetGeoTransform()[1] # 关闭数据集 image_dataset = None dsm_dataset = None # 调用函数 shadow_mask = extract_building_shadow(image, dsm, 5, 10) # 打印结果 print(shadow_mask) # 将掩膜叠加在数字表面模型上 masked_dsm = np.copy(dsm) masked_dsm[shadow_mask] = np.nan # 将遮蔽区域的高程值设为nan plt.imshow(masked_dsm, cmap='terrain') plt.show()这段代码怎么改可以让索引是两个的
时间: 2024-02-14 13:11:17 浏览: 67
tight_subplot.zip_matlab_matlab subplot
5星 · 资源好评率100%
不太明白您的问题是什么。如果您是想要让 `extract_building_shadow` 函数的输入参数变为元组 `(image, dsm)`,那么只需要将函数的第一行修改为 `def extract_building_shadow(data, ground_spacing, radius):`,然后在函数内部将 `image` 和 `dsm` 分别赋值为 `data[0]` 和 `data[1]` 即可。调用函数时,将 `(image, dsm)` 作为一个整体传递给函数即可。例如:`shadow_mask = extract_building_shadow((image, dsm), 5, 10)`。
阅读全文