Lately I have posted a question about this but in the end I solved it in a non-elegant manner.
I have 2 images that are numpy arrays and I would like to create a new array iterating the 2 images. I have 3 cases and I have done the following:
delta= np.empty((h, w, 3),int)
for z in range (0,3):
for i in range(0, (h-1)):
for j in range(0, (w-1)):
delta[i][j][z]=np.sqrt(((imgLlab[i][j][0]-imgRlab[i][j-disp[i][j]][0])**2) + ((imgLlab[i][j][1]-imgRlab[i][j-disp[i][j]][1])**2) + ((imgLlab[i][j][2]-imgRlab[i][j-disp[i][j]][2])**2) )
delta= np.empty((h, w, 3),int)
for z in range (0,3):
for i in range(0, (h-1)):
for j in range(0, (w-1)):
delta[i][j][z]=np.sqrt(((imgLlab[i][j][0]-imgRlab[i][j-disp[i][j]][0])**2) )
for z in range (0,3):
for i in range(0, (h-1)):
for j in range(0, (w-1)):
delta[i][j][z]=np.sqrt(((imgLlab[i][j][1]-imgRlab[i][j-disp[i][j]][1])**2) + ((imgLlab[i][j][2]-imgRlab[i][j-disp[i][j]][2])**2) )
I would like to not repeat iterating every time and do it as quickly as possible.
Is there another way to do this with numpy?
EDIT after Jaime help, i have changed my code like that:
disp= np.hstack([disp, disp, disp]).reshape(h,w,3).astype(np.int)
rows = np.arange(h).reshape(h, 1, 1)
cols = np.arange(w).reshape(1, w, 1)
planes = np.arange(3).reshape(1, 1, 3)
print rows.shape, cols.shape, planes.shape, disp.shape, h
data = imgLlab[rows, cols, planes] - imgRlab[rows ,cols - disp[rows, cols, planes], planes]
data = data**2
data = np.sum(data, axis=-1)
data = np.sqrt(data)
i had to reshape dist because it didn't have the same shape of imglLab and imgRLab, i mean imglLab is (288, 384,3) and disp was (288,384). Also if i print disp(288,384,1) i get the same error, its like there is no value there but the dimension is the same like the others ones. But now that the 3 arrays got the same dimension i get an indexError: index (384) out of range (0<=index(383) in dimension 1.
for j in range(h):? Also, your iteration overzis computing the same value every time, is that what you really want?dispis what is giving you the problem. Using 'hstack would be consistent with areshape(h, 3, w), for yours you would want to usenp.dstack.(h, w, 1)and when you createdatachange the indexing of ìmgRlab` toimgRlab[rows ,cols - disp[rows, cols, :], planes].