簡體   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