繁体   English   中英

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

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

所以我试图获取像素周围的像素位置:例如:

0 0 0 0
x x x 0
x 1 x 0
x x x 0

我需要获得所有这些 X 的位置。 对我来说问题不是我可以做一个双循环来获取它们的一般情况,问题是条件。 因为我想到的是每个案例都手动编码,但是效率不高而且需要太多行。 因此,我问是否有更简单的方法,我可以通过执行多个 if 语句并将它们的值分配给数组来手动编程

这是我目前写的内容,它需要很多行而且效率不高

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

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