6

I've loaded an RGB image with PIL/OpenCV, and I'd like convert all its channels into a single 1x(3*width*height) sequence into order to feed it to an ANN. I found I can simply do:

rlist = []
glist = []
blist = []
for i in xrange(im.width):
    for j in xrange(im.height):
        r,g,b = im[i,j]
        rlist.append(r)
        glist.append(g)
        blist.append(b)
img_vec = rlist + blist + glist

But obviously this is horribly inefficient. Is there a faster way with some internal OpenCV/numpy routine?

1
  • ANN == artificial neural network Commented Oct 14, 2011 at 13:25

1 Answer 1

8

As a quick example:

import Image
import numpy as np

im = Image.open('temp.png')
data = np.array(im)
flattened = data.flatten()

print data.shape
print flattened.shape

This yields:

(612, 812, 4)
(1987776,)

Alternately, instead of calling data.flatten(), you could call data.reshape(-1). -1 is used as a placeholder for "figure out what the given dimension should be".

Note that this will yield a vector (flattened) of r0, g0, b0, r1, g1, b1, ... rn, gn, bn, while you want a vector of r0, r1 ... rn, b0, b1, ... bn, g0, g1, ... gn.

To get exactly what you want, just call

flattened = data.T.flatten()

instead.

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

1 Comment

Are you sure about data.T.flatten()? My tests show that data.flatten() returns the data in order [r1,r2...,g1,g2,...b1,b2...] whereas data.T.flatten() returns it in order [r1,g1,b1,r2,g2,b2,...]. However, I tested using simple numpy arrays, not actual images.

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.