1
我想要搜索圖片中的矩形。這張照片是從PIL收集的。這意味着我會得到一個2d數組,其中每個項目都是一個帶有三個顏色條目的列表。numpy.equal與嵌套列表
要使用搜索的顏色得到矩形的位置,我使用的是np.equal
。這裏的縮水下來例如:
>>> l = np.array([[1,1], [2,1], [2,2], [1,0]])
>>> np.equal(l, [2,1]) # where [2,1] is the searched color
array([[False, True],
[ True, True],
[ True, False],
[False, False]], dtype=bool)
但我預計:
array([False, True, False, False], dtype=bool)
或
array([[False, False],
[ True, True],
[ False, False],
[False, False]], dtype=bool)
我怎樣才能實現與numpy
嵌套列表比較呢?
注意:然後我想用np.where
從np.equal
的結果中提取矩形的索引。
取決於你想做什麼可能是一種矯枉過正,但是你可以用'scipy.ndimage.filters.correlate'執行模板匹配(在圖片中尋找一個固定結構的對象),並獲取相關位置最大值爲'result [numpy.argwhere(result == result.max())]'。 – heltonbiker