我无法在Python中使用切片正确更改数组的元素 请帮助我!!
这是我的代码:
maxVal=np.max(gradIntensity2)
thrGradIntensity=gradIntensity2.copy()
highThr=maxVal/5
lowThr=maxVal/40
indHT=np.argwhere(gradIntensity2>=(highThr))
indLT=np.argwhere(gradIntensity2<=(lowThr))
ind1=(lowThr)<gradIntensity2
ind2=gradIntensity2<(highThr)
ind3=ind1 & ind2
ind=np.argwhere(ind3)
thrGradIntensity[indHT]=1
thrGradIntensity[indLT]=0
thrGradIntensity[ind]=0.5
print(maxVal) #-----------------------------result= 425.9426808716647
print(highThr)#-----------------------------result= 85.18853617433294
print(lowThr) #------------------------------result= 10.648567021791617
print(np.max(thrGradIntensity)) #--------------result= 0.5
print((thrGradIntensity==0.5).all()) #---------------result= true
我希望
np.max(thrGradIntensity) == 1
但这不会发生
为什么?????
print((thrGradIntensity==0.5).all())
为什么这是真的?!
我的阈值不起作用。
使用 np.argwhere 作为 2D 数组的索引会将每个坐标对视为一对行索引,而不是(行,列)。
示例:
test = np.array([[1, 2],
[3, 4]])
where_3 = np.argwhere(test == 3)
print(where_3)
print(test[where_3])
输出:
[[1 0]]
[[[3 4]
[1 2]]]
它正确地将 [1, 0] 识别为正确的索引,但使用
test[where_3]
返回 1 行和 0 行。
你最好直接删除 np.arg 并使用布尔掩码。
maxVal=np.max(gradIntensity2)
thrGradIntensity=gradIntensity2.copy()
highThr=maxVal/5
lowThr=maxVal/40
indHT=gradIntensity2>=(highThr)
indLT=gradIntensity2<=(lowThr)
ind1=(lowThr)<gradIntensity2
ind2=gradIntensity2<(highThr)
ind = ind1 & ind2
thrGradIntensity[indHT]=1
thrGradIntensity[indLT]=0
thrGradIntensity[ind]=0.5
print(maxVal) #-----------------------------result= 425.9426808716647
print(highThr)#-----------------------------result= 85.18853617433294
print(lowThr) #------------------------------result= 10.648567021791617
print(np.max(thrGradIntensity)) #--------------result= 0.5
print((thrGradIntensity==0.5).all()) #---------------result= true