链码的起点归一化、循环归一化、边界的长度、直径、形状数代码实现
时间: 2023-09-26 13:05:52 浏览: 221
链码的起点归一化:
```python
def normalize_start_point(chain_code):
# 计算链码所在位置
min_x = min_y = sys.maxsize
for code in chain_code:
x, y = code_to_coordinate(code)
if x < min_x:
min_x = x
if y < min_y:
min_y = y
# 将链码起点平移到(0,0)位置
normalized_code = []
for code in chain_code:
x, y = code_to_coordinate(code)
normalized_code.append(coordinate_to_code(x - min_x, y - min_y))
return normalized_code
```
循环归一化:
```python
def normalize_loop(chain_code):
# 首先进行起点归一化
normalized_code = normalize_start_point(chain_code)
# 找到最小的循环表示法
min_code = normalized_code[:]
for i in range(len(normalized_code)):
code = normalized_code[i:] + normalized_code[:i]
if code < min_code:
min_code = code
return min_code
```
边界的长度:
```python
def boundary_length(chain_code):
length = 0
for code in chain_code:
if code % 2 == 0:
length += 1
elif code % 4 == 1:
length += math.sqrt(2)
else:
length += 1 + math.sqrt(2)
return length
```
直径:
```python
def diameter(chain_code):
max_distance = 0
for i in range(len(chain_code)):
for j in range(i+1, len(chain_code)):
distance = code_distance(chain_code[i], chain_code[j])
if distance > max_distance:
max_distance = distance
return max_distance
```
形状数:
```python
def shape_number(chain_code):
# 首先进行起点归一化
normalized_code = normalize_start_point(chain_code)
# 计算转角数
corner_count = 0
for i in range(1, len(normalized_code)):
if abs(normalized_code[i] - normalized_code[i-1]) > 1:
corner_count += 1
# 计算形状数
shape_count = (corner_count - 4) / 4
return shape_count
```
阅读全文