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