Search code examples
arraysimagepython-2.7numpyreed-solomon

Making an array of images in numpy python 2.7


I want to have an array of images. For example, I would have a 4x1 array, (called imslice below) where each element is a nxnx3 image.

I want to do this to to do matrix operations on my imslice matrix like it was a normal matrix. For example, multiply it by a regular 2x2 matrix (called V.) When I try an do this right now, I am getting an array with 5 dimensions and when I try and multiply it by my V matrix I am getting the error that the dimensions don't agree (even though mathematically it's fine because the inner dimensions agree.)

Code:

imslice = np.array(([imslice1q, imslice2q, imslice3q, imslice4q]))

print imslice.shape


V = mh.gen_vmonde(4, 2, 1)

V.shape

C = np.dot(np.transpose(V), imslice)

------------------------------------------- ValueError Traceback (most recent call last)

in ()

6 V.shape

7

----> 8 np.dot(np.transpose(V), imslice)

9

ValueError: shapes (6,4) and (4,178,178,3) not aligned: 4 (dim 1) != 178 (dim 2)


Solution

  • Both np.dot and np.matmul treat more-than-two-dimensional arrays as stacks of matrices, so the last and last but one dimensions have to match.

    A simple workaround in your case would be transposing:

    np.dot(imslice.T, V).T
    

    If you need something more flexible, there is np.einsum:

    np.einsum('ji,jklm', V, imslice)