算法简介
BCD(Boustrophedon Cellular Decomposition)是一种栅格地图的分解方法。完成该分解后,每个cell都可以通过一个牛耕式路径遍历。不同cell之间通过旅行商问题获得最优路径即可做到全地图的覆盖式清扫。详情可以参考论文
Choset, H. (2000). Coverage of Known Spaces: The Boustrophedon Cellular Decomposition. Autonomous Robots (Vol. 9).
算法原理非常简单。地图中的每一列称为一个slice
。遍历地图中的slice
,并计算区块的连通性。每当连通性增加,则触发一个IN事件,封闭当前cell,并开启两个新的cell。若遇到连通性降低,则触发一个OUT事件,当前的两个cell封闭,并开启一个新的cell。
Python代码
# This is the code of Boustrophedon Cellular Decomposition algorithm.
# I write it in python3.6.
# For more details, please read the paper:
# Choset, H. (2000). Coverage of Known Spaces: The Boustrophedon Cellular Decomposition. Autonomous Robots (Vol. 9).
#
#
# - Dechao Meng
import numpy as np
import cv2
from PIL import Image
from matplotlib import pyplot as plt
import matplotlib
from typing import Tuple, List
import random
# matplotlib.rcParams['figure.figsize'] = [30, 20]
Slice = List[Tuple[int, int]]
def calc_connectivity(slice: np.ndarray) -> Tuple[int, Slice]:
"""
计算一个slice的连通性,并且返回该slice的连通区域。
Args:
slice: rows. A slice of map.
Returns:
The connectivity number and connectivity parts.
Examples:
>>> data = np.array([0,0,0,0,1,1,1,0,1,0,0,0,1,1,0,1,1,0])
>>> print(calc_connectivity(data))
(4, [(4, 7), (8, 9), (12, 14), (15, 17)])
"""
connectivity = 0
last_data = 0
open_part = False
connective_parts = []
for i, data in enumerate(slice):
if last_data == 0 and data == 1:
open_part = True
start_point = i
elif last_data == 1 and data == 0 and open_part:
open_part = False
connectivity += 1
end_point = i
connective_parts.append((start_point