문제

I wrote a script to perform a connectivity search in a binary image (the possible values for the array are 0 and 1). For each pixel, the code looks at how many neighbours have intensity 1, and if there are at least 5 neighbours with I=1, it assigns 1 as a value of the considered pixel. I want the code to repeat the process until no new pixels are assigned intensity 1. At present the code is not performing the connectivity search iteratively; do you have any suggestion on how to fix this?

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
도움이 되었습니까?

해결책

This section of the code:

for p in range(1, 100):
    ...
        if pixel_counter[p-2] > pixel_counter[p-1]:
            continue
        else: 
            break
        ... dead code ...

will slurp all of the execution, the part where I've marked dead code, which contains your counters are never executed because they're unreachable.

I'm not quite sure what your trying to do there.

다른 팁

To answer the question in the title: the easiest way to exit a nested loop is to just move the loops into a function and return from that. E.g.

def f():
    for i in range(10):
        for j in range(10):
            if i + j == 9: return
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top