파이썬에서 두 이진 이미지 배열의 교차를 수행해야하는 상황이 있습니다. 이상적으로는이 작업을 매우 빠르게 수행합니다.
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를 사용하여 이것을 수행하는 올바른 (덜 해킹 같은) 방법은 무엇입니까?
두 가지 접근 방식을 제안 할 수 있습니다.
255*(~((A==0) & (B==0))).astype(A.dtype)
255*(((A!=0) | (B!=0))).astype(A.dtype)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다