[18335fall08] matlab notes on problem 3
Steven G. Johnson
stevenj.mit at gmail.com
Thu Oct 9 16:06:46 EDT 2008
When you are reading an image into Matlab with "imread", it reads it
in as an integer array, and you may have noticed that this gives an
error with SVD etc. To perform an SVD, you need to convert this to
floating-point. You can do this with the "double" command, e.g.:
A = double(imread('myfile.jpg'));
To plot it with pcolor, I noticed that pcolor displays it upside-
down. Also, the grid lines are annoying, and the aspect ratio is
distorted. You can plot it nicely with:
pcolor(flipud(A)); colormap(gray); shading interp; axis equal
Finally, I should mention that things are more complicated for a color
image, which is why I recommend sticking to a grayscale image. For a
color image, it reads it in as a three-dimensional array where the
third dimension (for an RGB image) is red, green, blue. To convert a
slice of this, e.g. A(:,:,1) into a 2d array for SVD, you would need
to use the "squeeze" command: squeeze(A(:,:,1)). Then, I suppose you
could do SVD compression on each color component individually, then
combine them back together and write it out to a color file ..... but
it is much easier to just use a grayscale image.
Steven
More information about the 18335fall08
mailing list