[英]Thresholding a Two dimensional array in python
我試圖在二維數組上實現最小-最大閾值,這需要一些時間,我可能需要在進一步的過程中,任何人都有更好的想法如何做到這一點
def thresholding(arr,rows,cols,max,min):
for x in range(arr.shape[0]):
for y in range(arr.shape[1]):
for i in range(rows):
for j in range(cols):
if(arr[x,y,i,j] >= max):
arr[x,y,i,j] = max
elif(arr[x,y,i,j] <= min):
arr[x,y,i,j] = min
return arr
使用numpy.clip
:
np.clip(np.array([[1,2,3], [4,5,6]]), 2, 4) # [[2,2,3], [4,4,4]]
編輯了你的代碼
def thresholding(arr,max_,min_):
'''
input
------
arr : 2d array
max_ : Intended max value for threshold
min_ : Intended min value for threshold
Notes
------
Takes an array with the elements of a,
and returns an array where values < min are replaced with min(provided),
and those > max with max(provided)
'''
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
if arr[i,j] > max_:
arr[i,j] = max_
if arr[i,j] < min_:
arr[i,j] = min_
return arr
你也可以使用 np.clip,它有更多的功能。參考這里。 np.clip
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.