1

I have an image of size 128x128. I have applied imread function of matlab on it, A= imread(first.jpg); but this is returning me a 3-D array 128x128x3 , how can I convert it to a 2-D matrix consisting of 3 columns R, G, B (one column for each color)?

jklm
  • 171
  • 11

1 Answers1

2
A = imread('first.jpg');
Npixels = size(A,1)*size(A,2);
newA = reshape(A,[Npixels,3]);
itzik Ben Shabat
  • 927
  • 11
  • 24
  • thanks. it's working absolutely fine, can you also tell me how can I convert this back to jpg image? – jklm Nov 14 '16 at 13:53
  • @Tehreem Fatima To get back to the original size you will need to save the image size after loading it `Asz = size(A);`. Then perform a reshape operation after processing `origA = reshape(newA, Asz);` – jodag Nov 14 '16 at 16:15
  • yes, what jodag wrote is correct, if you wish to save it `imwrite(origA,'newImage.jpg','jpg');` @TehreemFatima – itzik Ben Shabat Nov 14 '16 at 18:47