簡體   English   中英

圖像過濾 python

[英]Image filtering python

我正在嘗試修復此 function 嘗試並行過濾 python 中的圖像,但 function 不起作用:

def parallel_filtering_image(r):
    # r: is the image row to filter
    # image is the global memory array
    # my_filter is the filter shape to apply to the image
    
    global image
    global my_filter
    
    #from the global variaable, gets the image shape
    (rows,cols,depth) = image.shape

    #fetch the r row from the original image
    srow=image[r,:,:]
    if ( r>0 ):
        prow=image[r-1,:,:]
    else:
        prow=image[r,:,:]
    
    if ( r == (rows-1)):
        nrow=image[r,:,:]
    else:
        nrow=image[r+1,:,:]
    
    #defines the result vector, and set the initial value to 0
    frow=np.zeros((cols,depth))
    frow=srow
    
    #return the filtered row
    return frow

這是調用者:

def filter_image(my_image):
    shape=my_image.shape
    rows=shape[0]
    v=range(rows)
    with mp.Pool(NUMCORES,initializer=my.init_globalimage,initargs=[image,filter_mask]) as p:
        result=p.map(my.parallel_filtering_image,v)
    return result

如果我需要計算 position (x,y) 中的過濾像素,我需要計算位置 (x-1,y-1)+(x,y-1)+(x +1,y-1)+(x-1,y)+(x,y)+(x+1,y)+(x-1,y+1)+(x,y+1)+(x +1,y1+1),對着過濾面罩。

需要注意的事項:

  • 該算法必須與圖像大小無關
  • 這些值將介於 0 到 255 之間,結果應該是這些值之間的 integer 值。
  • 為了計算邊界,我們使用下一個可用值。 例如:如果 y 是行,對於上邊界(其中 y=0),我們將替換 (x-1,y)+(x,y)+(x+1,y)+(x-1,y )+(x,y)+(x+1,y)+(x-1,y+1)+(x,y+1)+(x+1,y1+1)。
  • 圖像有 3 層深度,因此,我們需要對 3 層中的每一層應用濾鏡蒙版
  • 您將有 2 個矩陣:圖像和過濾器。 第一個是預加載的圖像,第二個矩陣是過濾器掩碼。

prownrow未使用,因此它們的分配是無用的。 frow=np.zeros((cols,depth))也是沒用的,因為frow是在那之后分配的。 由於srow=image[r,:,:]frow=srow ,那么frow=image[r,:,:]也是。 這意味着您的代碼在語義上等同於:

def parallel_filtering_image(r):
    global image
    return image[r,:,:]

這就是 output 相同的原因:缺少計算部分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM