我编写了一个脚本来在二进制映像中执行连接搜索(数组的可能值为0和1)。对于每个像素,代码查看有多少邻居具有强度1,并且如果至少有5个邻居具有I=1
,则它将1指定为所考虑像素的值。我希望代码重复该过程,直到没有为新像素分配强度1.目前代码没有迭代地执行连接搜索;你对如何解决这个问题有任何建议吗?
pixel_count = 0
pixel_counter = [0] * 100
for p in range(1, 100):
if p < 3:
continue
else:
if pixel_counter[p-1] > pixel_counter[p-2]:
continue
else:
break
for q in range(1, ran_x-1):
for r in range(1, ran_y-1):
counter = 0
if neighbours_mask_1[q,r] == 1:
counter = counter +1
if neighbours_mask_2[q,r] == 1:
counter = counter +1
if neighbours_mask_3[q,r] == 1:
counter = counter +1
if neighbours_mask_4[q,r] == 1:
counter = counter +1
if neighbours_mask_5[q,r] == 1:
counter = counter +1
if neighbours_mask_6[q,r] == 1:
counter = counter +1
if neighbours_mask_7[q,r] == 1:
counter = counter +1
if neighbours_mask_8[q,r] == 1:
counter = counter +1
if counter > 5:
mask_1[q,r] = 1
pixel_count = pixel_count + 1
print pixel_count
else:
mask_1[q,r] = 0
pixel_counter[p] = pixel_count
答案 0 :(得分:4)
代码的这一部分:
for p in range(1, 100):
...
if pixel_counter[p-2] > pixel_counter[p-1]:
continue
else:
break
... dead code ...
会破坏所有执行,我标记死代码的部分,包含你的计数器永远不会被执行,因为它们无法访问。
我不太确定你在那里做什么。
答案 1 :(得分:1)
回答标题中的问题:退出嵌套循环的最简单方法是将循环移动到一个函数中,然后从return
移动。 E.g。
def f():
for i in range(10):
for j in range(10):
if i + j == 9: return