简体   繁体   English

如何获取一个像素周围的所有 position 个像素

[英]how to get all the position of pixels around a pixel

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.我需要获得所有这些 X 的位置。 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因此,我问是否有更简单的方法,我可以通过执行多个 if 语句并将它们的值分配给数组来手动编程

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.]

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.首先我得到像素的邻居数,然后我创建一个一维数组,其大小是邻居的两倍,然后我绑定循环并将 position 添加到数组中。

        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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM