3

Hy! I have two images(same dimension) as numpy array imgA - imgB i would like to iterate each row and column and get somenthing like that:

for i in range(0, h-1):
  for j in range(0, w-1):
    final[i][j]= imgA[i,j] - imgB[i-k[i],j]

where h and w are the height and the width of the image and k is and array with dimension[h*w].

i have seen this topic: Iterating over a numpy array but it doens't work with images, i get the error: too many values to unpack Is there any way to do that with numpy and python 2.7?

thanks

edit I try to explain better myself. I have 2 images in LAB color space. these images are (288,384,3). Now I would like to make deltaE so I could do like that(spitting the 2 arrays):

 imgLabL=np.dsplit(imgL,3)
 imgLabR=np.dsplit(imgR,3)
 imgLl=imgLabL[0]
 imgLa=imgLabL[1]
 imgLb=imgLabL[2]
 imgRl=imgLabR[0]
 imgRa=imgLabR[1]
 imgRb=imgLabR[2]
delta=np.sqrt(((imgLl-imgRl)**2) + ((imgLa - imgRa)**2) + ((imgLb - imgRb)**2)   )

Till now everything is fine. But now i have this array k of size (288,384). So now i need a new delta but with different x axis,like the pixel in imgRl(0,0) i want to add the pixel in imgLl(0+k,0)

do you get more my problems?

4
  • Don't you mean imgA[i][j] - imgB[i-k[i]][j] ?? Commented Dec 13, 2012 at 16:36
  • yes sorry i wrote wrong but i meant that. My images are 384x288 but it goes in a infinity loop, also i can't write final[i][j] but just final because i get: valueError:output operand requires a reduction,but reduction is not enabled. Commented Dec 13, 2012 at 16:47
  • What's inside imgA[i][j]? Is it a tuple? If so, that could be your problem. The assignment expects one value. Commented Dec 13, 2012 at 16:48
  • imgA is an numpy array with imgA.shape is 288,384,1. Commented Dec 13, 2012 at 16:50

2 Answers 2

2

I'm pretty sure that whatever it is you are trying to do can be vectorized and run without any loops in it. But the way your code is written, it is no surprise that it doesn't work...

If k is an array of shape (h, w), then k[i] is an array of shape (w,). when you do i-k[i], numpy will do its broadcasting magic, and you will get an array of shape (w,). So you are indexing imgB with an array of shape (w,) and a single integer. Because one of the items in the indexing is an array, fancy indexing kicks in. So assuming imgB also has shape (h, w, 1), the return value of imgB[i-k[i], j] will not be an array of shape (1,), but an array of shape (w, 1). When you then try to substract that from imgA[i, j], which is an array of shape (1,), broadcasting magic works again, and so you get an array of shape (w, 1).

We do not know what is final. But if it is an array of shape (h, w, 1), as imgA and imgB, then final[i][j] is an array of shape (1,), and you are trying to assign to it an array of shape (w, 1), which does not fit. Hence the operand requires a reduction,but reduction is not enabled error message.

EDIT

You don't really need to split your arrays to compute DeltaE...

def deltaE(a, b) :
    return np.sqrt(((a - b)**2).sum(axis=-1))

delta = deltaE(imgLabL, imgLabR)

I still don't understand what you want to do in the second case... If you want to compare the two images displaced along the x-axis, I would suggest using np.roll:

deltaE(imgLabL, np.roll(imgLabR, k, axis=0))

will have at position (r, c) the deltaE between the pixel (r, c) of imgLabL and the pixel (r - k, c) of imgLAbR. Is that what you want?

Sign up to request clarification or add additional context in comments.

2 Comments

in this k i have the disparity of the two images. I m working with stereo images and i have found the disparity (docs.opencv.org/modules/calib3d/doc/…) So now i want to make the deltaE with the matching point and i should use this disparity. If i get right the point in the imgA(x,y) has a matching point in imgB(x+disp,y). therefore i have to move along the x-axis. I m gonna try this np.roll! btw the deltaE method work very good! thanks!
Using np.roll i get this error: only length-1 arrays can be converted to python scalars
1

I usually use numpy.nditer, the docs for which are here and have many examples. Briefly:

import numpy as np
a = np.ones([4,4])

it = np.nditer(a)
for elem in a:
    #do stuff

You can also use c style iteration, i.e.

while not it.finished:
    #do stuff
    it.iternext()

If you need to access the indices of your arrays. In your situation, I would zip your two images together to create an array of shape [2,h,w] and then iterate over this, filling an empty array with the results of the computation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.