[英]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),對着過濾面罩。
需要注意的事項:
prow
和nrow
未使用,因此它們的分配是無用的。 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.