Suzan Cioc
Suzan Cioc

Reputation: 30097

How to reshape 1D RGB to 2D RGB image in Matlab?

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?

Upvotes: 0

Views: 1152

Answers (1)

Rui Silva
Rui Silva

Reputation: 338

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

Upvotes: 3

Related Questions