Image Processing Toolbox    

Exercise 1 -- Some Basic Topics

Before beginning with this exercise, start MATLAB. You should already have installed the Image Processing Toolbox, which runs seamlessly from MATLAB. For information about installing the toolbox, see the MATLAB Installation Guide for your platform.

1. Read and Display an Image

Clear the MATLAB workspace of any variables and close open figure windows.

To read an image use the imread command. Let's read in a TIFF image named pout.tif (which is one of the sample images that is supplied with the Image Processing Toolbox), and store it in an array named I.

Now call imshow to display I.

2. Check the Image in Memory

Enter the whos command to see how I is stored in memory.

MATLAB responds with

3. Perform Histogram Equalization

As you can see, pout.tif is a somewhat low contrast image. To see the distribution of intensities in pout.tif in its current state, you can create a histogram by calling the imhist function. (Precede the call to imhist with the figure command so that the histogram does not overwrite the display of the image I in the current figure window.)

Notice how the intensity range is rather narrow. It does not cover the potential range of [0, 255], and is missing the high and low values that would result in good contrast.

Now call histeq to spread the intensity values over the full range, thereby improving the contrast of I. Return the modified image in the variable I2.

Display the new equalized image, I2, in a new figure window.

Call imhist again, this time for I2.

See how the pixel values now extend across the full range of possible values.

Here's What Just Happened
Step 3. You adjusted the contrast automatically by using the function histeq to evenly distribute the image's pixel values over the full potential range for the storage class of the image. For an image X, with a storage class of uint8, the full range is , for uint16 it is , and for double it is . Note that the convention elsewhere in this user guide (and for all MATLAB documentation) is to denote the above ranges as [0,255], [0,65535], and [0,1], respectively.
If you compare the two histograms, you can see that the histogram of I2 is more spread out and flat than the histogram of I1. The process that flattened and spread out this histogram is called histogram equalization.
For more control over adjusting the contrast of an image (for example, if you want to chose the range over which the new pixel values should span), you can use the imadjust function, which is demonstrated under 6. Adjust the Image Contrast in Exercise 2.

4. Write the Image

Write the newly adjusted image I2 back to disk. Let's say you'd like to save it as a PNG file. Use imwrite and specify a filename that includes the extension 'png'.

5. Check the Contents of the Newly Written File

Now, use the imfinfo function to see what was written to disk. Be sure not to end the line with a semicolon so that MATLAB displays the results. Also, be sure to use the same path (if any) as you did for the call to imwrite, above.

MATLAB responds with

.

Here's What Just Happened
Step 5. When you called imfinfo, MATLAB displayed all of the header fields for the PNG file format that are supported by the toolbox. You can modify many of these fields by using additional parameters in your call to imwrite. The additional parameters that are available for each file format are listed in tables in the reference entry for imwrite. (See Querying a Graphics File for more information about using imfinfo.)


 Getting Started Exercise 2 -- Advanced Topics