Lab (2) - Image Processing
In this lab you will learn about:
DO NOT Print this lab as it will not appear correctly
Please save your files and images under My Document folder
on your PC. Also, you need to open a MS Word file so you can keep
your results in that file. Save that file also under the same
directory. You can simply copy images from MATLAB screen to MS
Word. To copy a MATLAB screen shot into the MS Word file, first
click on the screen you wish to copy, then hold down the Alt or Ctrl
key (there is small difference between the two), and press PrntScrn key
at the same time. This copies the screen shot into the buffer,
you now need to go to MS Word screen and paste it either by using the
Edit option or by pressing Ctrl V. Make sure the cursor is at the
location you want to copy the image. Also it is possible to save
MATLAB images in different image formats. For this use the option
Save As under File to save the files.
What is a histogram?
Histogram is a statistical technique that helps us learn about the
data. The way it works is that we break the intensity range in
some intervals that are usually called bins. Then we go through
the entire image and we count the number of intensity values in each
range. At the end we make a table or plot the intensity vs. the
number of times
those intensity values are seen. Let's look at an example so we
understand this better. Consider the matrix (you can imagine this
matrix as an image) given below:
myImage = [ 23 120 34 255 4 120 200 200
120 87 62 120 23 4 0 87
23 4 0 0 120 200 200 120
23 62 250 0 120 200 34 62
23 120 34 255 4 250 200 200
120 23 62 120 23 4 0 87
23 4 0 0 120 200 255 120
23 255 250 0 120 200 34 62]
Let's find out what intensity values we have in this image, and the
count how many of each.
Intensity Count (number
of times we observe the intensity value)
Suppose we want to divide the range of intensity values into 5 equally
sized intervals (bins) and count the occurrences of each intensity
values in each bin. 5 equally sized bins between 0 to 255 are 51
units apart (almost). Thus, we set our 5 bins between 0-50,
51-101, 102-152, 153-203, and 204-255.
I had an extra number (255) and put that in the last bin. So the
bin is a bit larger, but we don't have any 254 anyway. So now we
will count the numbers in each bin as:
Intensity greater than or equal to 0 AND less than 50 (0, 4, 23,
and 34) -- counts for Bin1 = 27
Intensity greater than or equal to 51 AND less than 101 (62, and
87) -- counts for Bin2 = 8
Intensity greater than or equal to 102 AND less than 152
counts for Bin3 = 13
Intensity greater than or equal to 153 AND less than 203
counts for Bin4 = 9
Intensity greater than or equal to 204 AND less than or equal
counts for Bin5 = 7
Note that I had to make an exception in the last bin by including
255. We will make a table for these data:
BinNo Count (in that bin)
In MATLAB, type:
bin = [1 ; 2 ; 3; 4; 5]
counts = [27; 8; 13; 9; 7]
This another representation of the 8-by-8 matrix you just had.
Remember I asked you to imagine that the initial 8-by-8 matrix was an
image. I think we can agree that such a histogram can be another
representation of an
Generate a histogram for bins of size 5. i.e, use 0-4, 5-9,
... add the last bin the same way as above. Include the
histogram in your MS Word file.
Generate a histogram for bins of size 1. Thus every intensity
value is a bin.
Reading and Creating the
histogram of an image in MATLAB
When you work with a real image, of course, there are many pixels
and it is much harder to count them one-by-one. We can use a
computer program to do this for us. In MATLAB we can use a
predefined function to do it. If you have already save the
following images and still have the original ones, you can skip
Step 1. If you do not have them or are not sure, just resave them
Step 0: Right click on each image and save it in your My Document
leftImage = imread('The name you have given to the left image.jpg');
rightImage = imread('The name you have given to the right
Now that you have your image data stored in leftImage and
rightImage, you can create their histograms. Note that by default the
total number of intensities will be used as the number of bins, thus in
this example since the image is an 8-bit gray-scale image we use 256
figure, imhist(leftImage); % To create the histogram of the
figure, imhist(rightImage); %To create the histogram of the
Make sure to save the resulting images and corresponding histograms
in your MS Word file.
Make your observations. What similarities do you observe?
What differences do you observe?
do you think an importance of image histogram is?
Can you explain why you see such a large number at the low intensity
You can also change the number of bins using the imhist command as:
where imageData is the same as what you have used before and N is
number of bins you want to use. For example if we want to create
a histogram for the left image with 10 bins we will use:
figure, imhist(leftImage, 10);
Create histograms with 5, 15, and 30 bins for each of the
images. What do you learn from this experiment?
Consider the original two images once again. We want to compute
the inverted image for each one. The invert image is an image
where the black intensities are replaced with the white ones and the
gray levels are adjusted depending on their positions in the intensity
Here is an example for an 8 bit image (an image that has intensities
between 0 to 255).
A = [2 123 200 0
24 56 86 1
255 211 200 10
255 255 0 0]
The inverted image of A is:
InvertedA = [253 132 55 255
231 199 169 254
I am sure you have already figure out how I have computed the
inverted image for A. Now you need to compute the inverted image
for the two images I have given you in the lab and find their default
histograms as well.
Make sure to copy the resulting images and their histograms in your
MS Word file.
Make your observations by comparing the histograms of inverted
images to the corresponding histograms of the original images.