- Read and display an image
- Check how the image appears in workspace
- Perform histogram equalization on the image
- Write the image to a disk file
- Get information about a graphics file
1. Read and display an image
First, clear the Matlab of any variables and close open figure windows
>> clear, close all
To read an image, use imread command. Let's read in a TIFF image name pout.tif (which is one of the sample images that is supplied with the Image Processing Toolbox, and stored it in array named.
>> I=imread('pout.tif');
* If you do not put the semicolon (;) after the command, the command window will display like in image below:
Next, to display the image, use imshow command.
>> imshow(I)
*(I) = I value in the bracket is depend on variable name that you have declared in the above command.
An image will be displayed;
2. Check the image in memory
Enter the whos command to see how I is stored in memory.
>> whos
Matlab responds with
pout.tif stored is stored as a 291-by-240 array. Since pout.tif was an 8-bit image, it gets stored in memory as an uint8 array. Matlab can store images in memory as uint8, uint16 or double arrays.
3. Perform Histogram Equalization
pout.tif is in low contrast image. To see the distribution of intensities in pout.tif in its current state, we can create histogram by using the imhist function. And use figure command to see the distribution in figure window. Precede imhist function with figure.
>> figure, imhist(I) %Display a histogram of I in a new figure
From the above figure, the intensity is narrow and does not cover the potential range [0,255], and is missing the high and low values that would result in good contrast.
So, call histeq to spread the intensity values over the full range, thereby improve the contrast of I. Return the modified image in the variable I2.
>> I2=histeq(I); %Equalize I and output in new array I2.
Display the I2 image to see the difference of contrastness.
Call imhist again for I2 to see the difference of distribution.
We can see how the intensity and pixel expanded accross the full range of possible values.
* The storage class of uint8, the full range is:
The storage class of uint16, the full range is:
The storage class of double, the full range is:
4. Write the image
Write the newly adjusted image I2 back to disk. And specify a filename that includes the extension, example as .png, .jpeg and so on.
>> imwrite (I2, 'pout2.png');
The Matlab will write the image into disk. It wrote as 8-bit image because it was stored as uint8 intensity image in memory.
If image is in RGB and class uint8, it will be written as 24-bit image.
If we want to set the bit depth of the output image, use BitDepth parameter with imwrite.
>> imwrite (I2, 'pout2.png', 'BitDepth', 4);
Example above writes a 4-bit PNG file.
5. Check the contents of the newly written file
Type the command below
>> imfinfo ('pout2.png')
Matlab responds with;








