So I'm trying to get the positions of pixels that surround a pixel : for example :
0 0 0 0
x x x 0
x 1 x 0
x x x 0
I need to get the positions of all those Xs. The problem for me isn't the general case I can do a double-loop to get them, the problem is the conditions. Because what came to my mind is to manually code every case, but it isn't efficient and takes too many lines. Therefore, I'm asking if there is an easier way to do so I can program it manually by doing several if statement and assign their values to an array
Here is what i wrote for the moment it takes to many lines and isn't efficient
def cond_i_zero(pos_array,i,j):
pos_array[0] = i
pos_array[1] = j-1
pos_array[2] = i
pos_array[3] = j 1
pos_array[4] = i 1
pos_array[5] = j-1
pos_array[6] = i 1
pos_array[7] = j
pos_array[8] = i 1
pos_array[9] = j 1
return pos_array
def cond_j_zero(pos_array,i,j):
pos_array[0] = i-1
pos_array[1] = j
pos_array[2] = i-1
pos_array[3] = j 1
pos_array[4] = i
pos_array[5] = j 1
pos_array[6] = i 1
pos_array[7] = j
pos_array[8] = i 1
pos_array[9] = j 1
return pos_array
"""
i,j : represent the position of the pixel that is equivalent to 1 in the
example above
total_img_nb : 16 for the example
output expected : array of positions so for example since the maximum of pixels that suround a pixel is 8 the output will be an array of size 16
where every pair number represent the i(row) pos and every odd number represent the j (columns) pos
"""
def pos_in_array(total_imgs_nb,i,j):
x = 2
y = 2
size = int(math.sqrt(total_imgs_nb))-1
if ( i == 0 ):
x = 1
if ( j == 0 ):
y = 1
if ( i == size ):
x = size - 1
if ( j == size ):
y = size - 1
pos_array = np.zeros(( total_imgs_nb ))
pos_array = 999
if((i==0 and j == 0) or (i==size and j == size)):
pos_array[0] = i
pos_array[1] = y
pos_array[2] = x
pos_array[3] = j
pos_array[4] = x
pos_array[5] = y
elif (i==0):
pos_array=cond_i_zero(pos_array,i,j)
elif (j==0):
pos_array=cond_j_zero(pos_array,i,j)
elif (i==size):
pos_array[0] = i
pos_array[1] = y
pos_array[2] = x
pos_array[3] = j
pos_array[4] = x
pos_array[5] = y
pos_array[6] = y
pos_array[7] = y
pos_array[8] = y
pos_array[9] = y
else:
count = 0
for w in range(i-1,i 2):
for v in range(j-1,j 2):
pos_array[count] = w
count = count 1
pos_array[count] = v
count = count 1
return pos_array
def main():
pos_array = pos_in_array(16,1,1)
# this usually return
[0. 0. 0. 1. 0. 2. 1. 0. 1. 2. 2. 0. 2. 1. 2. 2.]
CodePudding user response:
Here is how i did it . first i get the number of neighbours for the pixel then i create a 1-D array of double the size of the neighbours and then i bound the loop and add the position to the array .
size = int(math.sqrt(nb_total))
if((i==0 and (j ==0 or j==size-1))or (i==size-1 and (j == size -1 or j==0))):
neighbors = 3
elif(i==0 or j==0 or i==size-1 or j==size-1):
neighbors = 5
else:
neighbors = 8
array_pos = np.zeros((neighbors*2))
count = 0
for x in range(size):
for y in range(size):
if(x == i and y == j ):
continue
if((x < i 2 and y <j 2)and (x> i-2 and y > j-2 )):
array_pos[count] = x
count = count 1
array_pos[count] = y
count = count 1
if(count == neighbors*2):
break
if(count == neighbors*2):
break
return array_pos