1
我有一種情況,我需要在python中執行兩個二進制圖像數組的交集。理想情況下,我很快就做到了這一點。Numpy:將二維索引數組轉換爲一維數組用於相交計算
numpy的具有intersect1d
功能,將做的工作,如果我可以把我的座標轉換成單一稀土元素。
現在(因爲我知道我的照片的尺寸),我使用乘法,求和,交點將所有內容轉換爲整數格式,然後使用類似的方法進行解包。
def npimg_intersection(A,B):
Aargwhere = np.argwhere(A==0)
Bargwhere = np.argwhere(B==0)
Aargwhere[:,0] = Aargwhere[:,0]*1000
Aargwhere = np.sum(Aargwhere,axis=1)
Bargwhere[:,0] = Bargwhere[:,0]*1000
Bargwhere = np.sum(Bargwhere,axis=1)
Iargwhere0 = np.intersect1d(Aargwhere,Bargwhere)
Iargwhere = np.zeros(shape=(Iargwhere0.shape[0],2),dtype=Iargwhere0.dtype)
Iargwhere[:,0] = Iargwhere0[:]/1000
Iargwhere[:,1] = Iargwhere0[:]%1000
I = np.zeros(shape = A.shape,dtype=A.dtype)
I[:,:] = 255
I[Iargwhere[:,0],Iargwhere[:,1]] = 0
return I
它的工作原理。相當快。
但是,使用numpy來做到這一點的正確方法是什麼(少黑客行爲)?