0

Suppose I have 1D RGB array of the following structure:

I = [r1 r2 ... rN; g1 g2 ... gN; b1 b2 ... bN];

where

N = H*W;

ans H and W are height and width of the picture respectively.

How to reshape it to colored image format HxW, which is represented by 3D matrix so that

I2(1,1,1) = r1
I2(1,1,2) = g1
I2(1,1,3) = b1
I2(2,1,1) = r2
I2(2,1,2) = g2
I2(2,1,3) = b2
...
I2(H,W,1) = rN
I2(H,W,2) = gN
I2(H,W,3) = bN

(if I am correct thinking that normal 1D -> 2D reshape works by columns)

UPDATE

This reshaping can be done the following way

R = I(1,:);
R = reshape(R,H,W);

G = I(2,:);
G = reshape(G,H,W);

B = I(3,:);
B = reshape(B,H,W);

I2 = cat(3, R, G, B);

Can it be done shorter, with one reshape call for example?

2
  • What you have for I in this example isn't 1D in Matlab. Is your actual array just a 1D array of numbers where the first third is your R values, second third your G values, and last third your B values?
    – Ryan J
    Jul 11, 2014 at 17:18
  • Obviously, from Matlab point of view, it is 2D array of numbers. From logical point of view it is 1D array of pixels, where all pixels are laid out along Matlab's 2nd dimension and pixel components are laid out along Matlab's 1st dimension.
    – Suzan Cioc
    Jul 11, 2014 at 17:33

1 Answer 1

3

I think that what you're looking for is: reshape(I', H, W, 3)

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.