![](/img/trans.png)
[英]AttributeError: 'numpy.ndarray' object has no attribute 'index' in Python
[英]AttributeError- 'numpy.ndarray' object has no attribute 'index'
我不斷收到此錯誤消息:
newmaxleft=cl1count.index(max(cl1count))
AttributeError: 'numpy.ndarray' object has no attribute 'index'
代碼的目的是找到白色像素數量最多的列的第一個出現點。
我的代碼:
cl = cl[top:bottom, left:right]
cl1mask = np.uint8(np.where(cl == 0, 0, 1))
cl1count = cv2.reduce(cl1mask, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32SC1)
cl1count.flatten().tolist()
newmaxleft=cl1count.index(max(cl1count))
發生錯誤是因為 numpy arrays 沒有 index 屬性,如錯誤所述。 因此,不要使用索引,而是使用類似newmaxleft=np.where(cl1count == max(cl1count))
的東西,您可以使用newmaxleft[0]
來獲得第一次出現。 您也可以使用argmax
。
Numpy 沒有索引方法。 它使用 where (用於一般目的)或某些特定功能。 在你的情況下,最好的選擇是
newmaxleft = cl1count.argmax()
你也可以使用
newmaxleft = np.where(a==max(a))[0][0]
但它的效率較低。 第一個 [0] 返回 np.array 位置,第二個 [0] 返回第一次出現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.