Friday, April 03, 2009

MATLAB Image Basics


One nice feature of MATLAB is its provision of handy functions which are not part of the programming language proper. An excellent example of this is its support for images. The base MATLAB product provides routines for the loading from disk, manipulation, display and storing to disk of raster images. While it's true that one can find code libraries to perform these functions for other programming languages, like C++, the MATLAB model offers several advantages, not the least of which is standardization. If I write image-handling code in MATLAB, I know that every other MATLAB user on Earth can run my code without modification or the need for extra header files, libraries, etc. This article will serve as a brief introduction to the use of image data within MATLAB.

Image Data

Images are nearly always stored in digital computers in one of two forms: vector or raster . Vector images store images as line drawings (dots, line segments, polygons) defined by the spatial coordinates of their end points or vertices, and are most often used these days in artistic settings. A raster image is simply a 2-dimensional array of colored pixels (represented by numbers). This article will concentrate on the much more common raster form.

Raster images, being arrays of numbers, are a natural fit for MATLAB, and indeed MATLAB is a convenient tool for applications such as image processing. Raster images always have 2 spatial dimensions (horizontal and vertical), and 1 or more color planes. Typically, grayscale images are stored as a 2-dimensional array, representing 1 color plane with values of 0.0 indicating black, 1.0 indicating white and intermediate values indicating various shades of gray. Color images are similar to grayscale images, but are most often stored as a 3-dimensional array, which is really a stack of three 2-dimensional color planes: one for each primary color: red, green and blue ("RGB"). As with grayscale images, values in the RGB color planes represent brightness of each color. Note that when all three color values are the same, the resulting color is a shade of gray.

For the reader's knowledge, there are also index images which will not be covered here, but which are full of index numbers (integers) which do not represent colors directly, but instead indicate locations in a palette. Also, brightness values are often stored in files as integers, such as 0 - 255 instead of 0.0 to 1.0.

Loading Images from Disk

In MATLAB, images are read from disk using the imread function. Using imread is easy. The basic parameters are the location of the image file and the file format:

>> A = imread('c:\QRNG.png','PNG');
>> whos
Name Size Bytes Class Attributes

A 942 x 1680 x 3 4747680 uint8

This image is 942 pixels vertical by 1680 pixels horizontal, with 3 color planes (red, green and blue). Note that image data has been store in MATLAB as unsigned 8-bit integers (uint8). Since I often make multiple calculations on images, I typically convert the data type to double-precision real (double) and scale to 0.0 - 1.0 (though this will slow calculation):

>> B = double(A) / 255;

Displaying Images

Showing images on the screen is most easily accomplish using the image function:


Grayscale images will display using a default palette, which can be changed via the colormap command:

>>colormap gray

Images will be fit to the screen, which may distort their aspect ratio. This can be fixed using:

>>axis equal

...meaning that pixels will use equal scales horizontally and vertically.

Manipulating Images

As arrays, images can be modified using all the fun things we usually do to arrays in MATLAB (subsetting, math operations, etc.). I will mention one other useful base MATLAB tool for image processing: the rgb2hsv function, which converts an RGB image to an HSV one. HSV is a different colorspace (way of representing colors). HSV arrays are similar to RGB arrays, except their 3 color planes are hue, saturation and value (in that order). It is often convenient to work on the value ("brightness") plane, to isolate changes in light/dark from changes in the color. To get back to the land of RGB, use the function hsv2rgb.

Saving Images to Disk

Images can be saved to disk using the imwrite command. This is essentially the inverse of the imread command:

imwrite(A,'New Image.bmp','BMP')

...with the parameters indicating the array to be saved as an image file, the file location and image file format, in that order.

Note that MATLAB understands images as both 0 - 255 uint8s and 0.0 - 1.0 doubles, so there is no need to reverse this transformation before image storage.


Working on images in MATLAB is very convenient, especially when compared to more general-purpose languages. I urge the reader to check the help facility for the functions mentioned here to learn of further functionality.

Further Reading

For more information on image processing, I recommend either of the following books:

Digital Image Processing (3rd Edition) by Gonzalez and Woods
(ISBN-13: 978-0131687288)

Algorithms for Image Processing and Computer Vision by J. R. Parker
(ISBN-13: 978-0471140566)


Vincent said...

I remember the imread function! MATLAB makes image processing so much easier, since the underlying image data becomes arrays and matrices.

Will Dwinnell said...

Yes, and many common manipulations of images (lightening, darkening, binarizing, etc.) are quite easy to code in MATLAB.

Cris Luengo said...

Nice to see people getting interested in images! Thanks for writing this introduction, I'm sure it'll be useful to many.

Amaracchia said...

Dear Mr Dwinnell, i'm an italian student and i found your blog searching infos in google.
I'm looking for a solution to a trivial matter in Matlab that is driving me crazy.

I need to change an histogram's y axes from a linear scale to a logarithmic one; but when the scale is changed, my histogram change his aspect, because mATLAB displays only non log(0) points.
How can i solve this problem?

I haven't found any response searching in the blog.
I'm sorry for my silly question, but maybe you can help me, this blog is so helpful for other matters.

Thank you so much.

runiteking1 said...

Thank you for commenting on my blog!

Steve L said...


Take a look at this document from the MathWorks support website:

The bottom of the bar plot is at y = 0, which can't be drawn on a log scale (unless you have an infinitely tall monitor.) That document talks about how to adjust the plot to take that into account.

Nathan said...

Wow, nice - to be honest, I'd never even heard of MATLAB, but I think you've sparked my interest now!

And PS - thanks for the blog comment; I apologise if I came across as attacking of religions, that wasn't my intend.

PAT said...

Hi, thanks for you post. I have an issue when I try and calclate the std dev:

AStd = std(A)

it is giving me this error:

??? Attempt to execute SCRIPT std as a function.


Will Dwinnell said...

PAT, it sounds as though you have a MATLAB program called std, which conflicts with the name of the internal MATLAB function, std.

PAT said...
This comment has been removed by the author.
PAT said...

Thanks Will! yes you where right! problem sorted. Once the principle components are found using PC = B * V in your example - how would I go about creating a loading plot of the diiferent PC's. Thank you very much for the posts.

Santana said...

Hello Will
do you matlab code for Genetic algorithm to optimize ANN. can you give an example in your blog.
Many Thanks :)

Santana said...

Sorry :)
do you have a matlab code for Genetic algorithm to optimize ANN. can you give an example in your blog.
Many Thanks :)

Anonymous said...

For some basic intro to Image processing in matlab visit:

Misl said...
This comment has been removed by the author.
Misl said...

hi Mr. Dwinnell,

I am working on an imaging processing problem and i am trying to detect (unperfect) squares of material deposited on a surface.
the way I see the steps needed to image processing is as follow:
1- load the image
2- try to do edge detection to draw squares around my irregular shapes (they are supposed to be squares of known dimensions)
3- ounce the edge are contoured or drawn, I think I should try to fill the shape
4 Locate the centroid of the squares and store their x and y in a matrix

Could you please comment and help with the needed Matlab functions (and their order) to do the above. i can send you the piece of code I wrote so far to clean up the image and maybe a sample of the images i am working with.I guess a lot is still to be done and any comment you can give will be highly appreciated