# Converting a jpg image (RGB) into a 2-d array of numbers (2D)

422 views (last 30 days)
Terence Britton on 4 May 2011
Commented: Eric Pleace on 14 Feb 2019
I want to read in an image (a .jpg image) into MATLAB, but using the
function converts the image into a 3-d or maybe 5-d array i.e. x,y,RGB or x,y,R,G,B. What I want is to be able to read the image into MATLAB as a 2-d array of numbers, so instead of each pixel having 3 numbers to define it's colour (i.e. R G B) it would have just one number to define colour. Then I want to use the
imagesc(Image);
function to view the 2-d array graphically.
Any help with this would be much appreciated.
Thanks,
Terry.

Sean de Wolski on 4 May 2011
Assuming your image is uint8, in order to have a unique value for each pixel color combination, you'll need 256^3 (~16.8million) values. These values will have to be stored in floating point or uint32 values and your images will be much larger. You'll then have to make a colormap of [~16.8million x 3] floating point values to represent the colors you want. This approach, to word it correctly, is stupid. What is so wrong with reading in a 3-dimensional matrix? You could always store it as a vector and reshape it after.
my \$0.02.
(unless you just want a gray scale image in which case doc rgb2gray)

KIRAN kumar on 17 Mar 2013
imshow(I)
title('Input Image')
I1=rgb2gray(I);
figure,imshow(I1)
title('gray converted Image')
M Shajeeh Mustafa on 2 May 2017
I needed the 2-D array to binarize my image using imbinarize(); function. It gives error if your image is a 3-D array
Walter Roberson on 2 May 2017
imbinarize() is not suitable for nonlinear measurements such as temperature represented as color.
If you have data that is linear in some property, you need to convert the 3D data to be in terms of that property, after which you can imbinarize()
For example if you are using color images that are linear in brightness, then you can use rgb2gray() and imbinarize() the result.

Walter Roberson on 4 May 2011
rgb2ind() to get out an array of indices and the colors associated with each index. Then translate each of the colors once through the color scale, creating a vector of temperatures. Take the array of indices you got from rgb2ind, add 1 to it (because indices start from 0) and use that array to index the vector of temperatures to get out an array of temperatures.

KIRAN kumar on 17 Mar 2013
Edited: Walter Roberson on 2 May 2017
imshow(I)
title('Input Image')
I1=rgb2gray(I);
figure,imshow(I1)
title('gray converted Image')

#### 1 Comment

Eric Pleace on 14 Feb 2019
Thanks, just what I was looking for :-)

dhia jamaa on 21 May 2015
Edited: Walter Roberson on 2 May 2017