nxn维黑白图像中,3x3游标历遍算每点最小距离

import matplotlib.pyplot as plt
import numpy as np
def oushijuli(b):
#im = plt.imshow(b, cmap=‘Greys’, interpolation=‘none’, vmin=0, vmax=1, aspect=‘equal’) # 设置图像参数
liebiao = []#存放每点最小距离
zuobiao = []#每点对应的0点的坐标
for p in range(0,b.shape[0]):#把1全都变成99,文中赋值为无穷大
for q in range(0,b.shape[0]):
small_num1 = 99 # 定义一个无限大的值,这里用99代替
if b[p,q] == 1:
b[p,q] = small_num1
print(b)#1变为无穷后的二维数组
for x in range(2 , b.shape[0]):
for y in range(2 , b.shape[0]):#以下为正序扫描,x,y从2到n,b.shape[0]为每行元素个数
small_num2 = 99
h = b[x-2:x+1,y-2:y+1]#建立3x3游标
if 0 in h:#如果游标中有0
if h[1][1] == small_num2:#如果游标中心点为无限大
for i in range(x-2,x+1):
for j in range(y-2,y+1):#i,j历遍游标中每个数
can_1 = np.array([x-1,y-1])#can_1意思是参数1,中心点坐标,用np.array表示
can_2 = np.array([i,j])#can_2同上,历遍位置坐标
if b[i][j] != 0:#如果b中i,j点不为0则忽略
continue
L1 = np.sum(np.square(can_1 - can_2))#平方欧氏距离计算
if L1 < h[1][1]:
h[1][1] = L1#L1若小于游标中心数值,L1的值赋值给中心点
z = (i,j)#z记为该游标中心点距离最近的0点的坐标
zuobiao.append(z)
liebiao.append(h[1][1])
else:#如果游标中心为0
zuobiao.append((0, 0))
liebiao.append(0)
else:#游标中没有0
flag=False
for i in range(x-2,x+1):
if flag == True:
break
for j in range(y-2,y+1):
if i == (x-1) and j == (y-1) :#当扫描到中心点时,停止扫描,跳出循环
flag=True
break
can_3 = np.array([x-1,y-1])-np.array([i,j])#中心点坐标-扫描点坐标
can_4 = np.array([i, j]) - np.array(zuobiao[(i-1)(b.shape[0]-2)+j-1])#扫描点坐标-距离扫描点最近的0点坐标
L2 = np.sum(np.square(can_3))#平方欧氏距离计算
L = b[i][j] + L2 + 2
abs(np.dot(can_3,can_4))#总距离
if L < h[1][1]:
h[1][1] = L#中心点赋值为总距离的值
can_5 = zuobiao[(i - 1) * (b.shape[0] - 2) + j - 1]#该点最近的0点坐标
zuobiao.append(can_5)
liebiao.append(h[1][1])
for m in range(b.shape[0] , 2,-1):#以下为倒序扫描,含义同上
for n in range(b.shape[0] , 2,-1):
small_num3 = 99
h = b[m-3:m, n-3:n]#建立游标
if 0 in h:
continue
else:
fan_flag = False
for i in range(m-1,m-4,-1):
if fan_flag == True:
break
for j in range(n-1,n-4,-1):
if i == m-2 and j == n-2 :
fan_flag = True
break
can_6 = np.array([i, j])-np.array([m-2,n-2])
can_7 = np.array([i,j])-np.array(zuobiao[(i-1)(b.shape[0]-2)+j-1])
can_8 = zuobiao[(i-1) * (b.shape[0]-2)+j-1]
L2 = np.sum(np.square(can_6))
L = b[i][j] + L2 + 2
abs(np.dot(can_6,can_7))
if L < h[1][1]:
h[1][1] = L
liebiao[(m-3) * (b.shape[0]-2)+n-3] = h[1][1]
zuobiao[(m-3) * (b.shape[0]-2)+n-3] = can_8
juli = np.array(liebiao).reshape(b.shape[0] - 2, b.shape[0] - 2)#把liebiao内容变为二维数组形式,方便转化为距离图像
imm = plt.imshow(juli,cmap=‘Blues’, interpolation=‘none’, vmin=0, vmax=6, aspect=‘equal’)
print(juli)

a = np.array([[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,1,0,0,0,0],
[0,0,0,1,1,1,1,0,0,0],
[0,0,1,1,1,1,1,1,0,0],
[0,0,0,1,1,1,1,0,0,0],
[0,0,0,0,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]])
oushijuli(a)
plt.show()#显示图像
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值