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, min(image.shape[1], i+ground_spacing), 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 cv2.imwrite(output_path, shadow_mask.astype(np.uint8) * 255) return shadow_mask这段代码算法具体过程是什么
时间: 2024-02-10 15:20:58 浏览: 17
这段代码实现了从遥感图像和数字表面模型(DSM)中提取建筑物的阴影。算法的具体过程如下:
1. 初始化一个与遥感图像大小相同的全零矩阵作为阴影掩模。
2. 对于每个以地面间隔为单位的格子(即每个像素的真实地面间隔),从左上角开始,遍历整个遥感图像。如果当前像素的掩模值为0,则表示该像素没有被标记为阴影,可以进行下一步处理;否则跳过该像素,继续遍历下一个像素。
3. 对于每个未被标记为阴影的像素,以该像素为中心,沿着一个指定半径的螺旋线搜索周围的像素。如果搜索到的像素已经被标记为阴影,则跳过该像素,继续搜索下一个像素。
4. 对于未被标记为阴影的,并且没有被搜索到的像素,计算其高度。如果该像素的高度大于中心像素的高度,则标记该像素为阴影;否则,如果该像素的高度等于中心像素的高度,并且该像素距离中心像素更近,则标记该像素为阴影。
5. 将阴影掩模保存为二进制图像,并返回该掩模。
该算法的基本思路是,通过遍历遥感图像中的每个像素,寻找该像素周围是否存在阴影。如果存在,则将该像素标记为阴影,并继续搜索周围的像素。通过不断扩大搜索半径和更新阴影掩模,可以逐渐提取出整个建筑物的阴影。
相关问题
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 cv2.imwrite(output_path, shadow_mask.astype(np.uint8) * 255) return shadow_mask image = cv2.imread('C:\yingxiang\DJI_20230322140516_0026_V.JPG') dsm_path = 'C:/sanwei/jianmo/Productions/Production_2/Production_2_DSM_part_2_2.tif' dsm_dataset = gdal.Open(dsm_path) output_path = 'C:\yingxiang\mask.png'这段代码为什么运行后不能将掩膜结果保存
这可能是由于在保存 PNG 文件之前,尝试在代码中访问 `output_path` 变量,而此时该变量尚未定义。您可以将 `output_path` 变量的定义移动到函数之前,以确保在访问变量之前它已经存在。例如:
```
output_path = 'C:\yingxiang\mask.png'
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
cv2.imwrite(output_path, shadow_mask.astype(np.uint8) * 255)
return shadow_mask
image = cv2.imread('C:\yingxiang\DJI_20230322140516_0026_V.JPG')
dsm_path = 'C:/sanwei/jianmo/Productions/Production_2/Production_2_DSM_part_2_2.tif'
dsm_dataset = gdal.Open(dsm_path)
# 调用函数
extract_building_shadow(image, dsm_dataset.ReadAsArray(), 10, 10)
```
请注意,此代码还将 `dsm` 数据集转换为数组,并将其传递给函数。
def spiral_search(center, radius, step): x, y = center yield x, y for r in range(1, radius + step, step): for i, (dx, dy) in enumerate(((1, 0), (0, 1), (-1, 0), (0, -1))): for j in range(r - (i % 2)): x += dx y += dy yield x, y # 定义建筑物遮蔽区域提取函数 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 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这段代码怎么改避免以下错误The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
这个错误通常是由于在条件语句中使用了多个元素的数组而导致的。可以使用 `np.all()` 或 `np.any()` 函数来解决这个问题。在这段代码中,可以将 `if not shadow_mask[i, j]:` 改为 `if not np.any(shadow_mask[i, j]):`,将 `if np.any(shadow_mask[x, y]):` 改为 `if np.any(shadow_mask[x:x+1, y:y+1]):`。修改后的代码如下:
```
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:x+1, y:y+1]):
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
```