python区域增长_基于种子点的区域生长的灰度图像分割(pyhton语言实现)

from PIL import Image

from pylab import *

def region_growth():

print("循环增长,直到len(region_points) = 0")

count = 0

x = [-1, 0, 1, -1, 1, -1, 0, 1]

y = [-1, -1, -1, 0, 0, 1, 1, 1]

while len(region_points) > 0:

if count == 0:

point = region_points.pop(0)

i = point[0]

j = point[1]

print("len = ", len(region_points))

p_val = input_arr[i][j]

# 像素强度差异范围 + - 8

lt = p_val - 8

ht = p_val + 8

for k in range(8):

if seg_img[i + x[k]][j + y[k]] != 1:

try:

if lt < input_arr[i + x[k]][j + y[k]] < ht:

seg_img[i + x[k]][j + y[k]] = 1

p = [0, 0]

p[0] = i + x[k]

p[1] = j + y[k]

if p not in region_points:

if 0 < p[0] < rows and 0 < p[1] < columns:

# 满足条件的点

region_points.append([i + x[k], j + y[k]])

else:

seg_img[i + x[k]][j + y[k]] = 0

except IndexError:

continue

point = region_points.pop(0)

i = point[0]

j = point[1]

count = count + 1

# region_growth(point[0], point[1])

if __name__ == "__main__":

input_img = Image.open("input1.jpg").convert("L")

input_arr = np.asarray(input_img)

rows, columns = np.shape(input_arr)

plt.figure()

plt.imshow(input_img)

plt.gray()

print("请选择初始点...")

p_seed = plt.ginput(1)

print(p_seed[0][0], p_seed[0][1])

# 可以手动设置种子点

# x = int(120)

# y = int(160)

x = int(p_seed[0][0])

y = int(p_seed[0][1])

seed_pixel = [x, y]

print("选择的点为:", seed_pixel)

plt.close()

seg_img = np.zeros((rows + 1, columns + 1))

seg_img[seed_pixel[0]][seed_pixel[1]] = 255.0

img_display = np.zeros((rows, columns))

region_points = [[x, y]]

region_growth()

plt.imsave("result.jpg", seg_img)

plt.figure()

plt.imshow(seg_img)

plt.colorbar()

plt.show()

本方法不确定性较大,不同起始点迭代有不同的收敛结果,也可能不会收敛,并且收敛时间也具有不确定性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值