MATLAB Answers

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

422 views (last 30 days)
Terence Britton
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
Image = imread('image.jpg');
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.

  0 Comments

Sign in to comment.

Answers (4)

Sean de Wolski
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)

  5 Comments

Show 2 older comments
KIRAN kumar
KIRAN kumar on 17 Mar 2013
I=imread('inputImage.extenstion')
imshow(I)
title('Input Image')
I1=rgb2gray(I);
figure,imshow(I1)
title('gray converted Image')
M Shajeeh Mustafa
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
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.

Sign in to comment.


Walter Roberson
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.

  0 Comments

Sign in to comment.


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

  1 Comment

Sign in to comment.


dhia jamaa
dhia jamaa on 21 May 2015
Edited: Walter Roberson on 2 May 2017
I = imread('image.jpg');
image=mat2gray(I);

  1 Comment

Walter Roberson
Walter Roberson on 21 May 2015
The question turned out to be able converting the colors to temperature values. As the mapping of brightness of color to temperature was not necessarily linear, the color itself needed to be looked up in a conversion table to get temperature. The easiest way to do that is to use rgb2ind() to get a color index for each point, that could be used to index into a vector of conversions of color to associated temperature.

Sign in to comment.

Sign in to answer this question.