You are on page 1of 4

Digital Image Processing: Lab Assignements

#1: Getting Started with Matlab, Grey Level Quantization and Image Histograms

Todor Stoyanov and Achim Lilienthal, Örebro University

Issue date: 2011-04-07 Due date: 2011-04-14

Instructions
• One report has to be handed in per team. Nevertheless, you must be able to answer
questions about your lab report individually.

• Clearly state your names on the lab report.

• The printed lab report (including source code listings) must be handed in within one (1)
week. Additionally, you have to send your source code (Matlab m-files) by electronic
mail to todor.stoyanov@oru.se. Use the subject “DIP Lab”, clearly state your names
and the lab session in the message and attach your files (in plain ASCII format).

• Each lab report is either marked as fail or pass. To pass the labs, all reports must have
been marked as pass. If the report is handed in more than three days after the deadline,
the report is marked as fail. Up to five bonus points may be awarded to the team for
very good lab assignments that comply with the criteria described below:

+1p ← Report is clearly written and easy to follow.


+1p ← Code is well documented.
+1p ← Efficient and correct use of matlab API (vectorization, API functions, plots,
etc.)
+1p ← Awarded for particularly well explained procedures – succinct, but descriptive,
use of references from scientific articles, textbooks, etc.
+1p ← Reserved for overall exceptional reports, that conform to all scientific writing
standards .
– No bonus points are awarded to late reports (handed in after the due date).
– If the assignment is not accepted, due to a serious error in one of the problems, no
bonus points will be awarded for this assignment and subsequent resubmissions.
– Any additional materials used during the completion of the assignment must be
cited. Failure to correctly reference sources will result in zero points.

You will find more material (assignments, images, source code, tutorials, etc.) for the labs
on the course web page:
http://aass.oru.se/Research/Learning/courses/dip/2011/index.html.

1
Matlab
For the labs we will use Matlab (short for “Matrix Laboratory”), a large and very powerful
software tool for numeric computation, data analysis and signal processing. It features a
high-level programming language and an interactive graphical environment for algorithm
development and visualization. You will find links to tutorials on the course web page.
After starting the MATLAB application, you will get a “desktop” with several sub-
windows, of which the interactive command line window is the most important one: it prompts
(“>>”) for commands, evaluates and executes them and prints the results. Matlab provides
a built-in manual page system for all available commands as well as an extensive hypertext
on-line help system: on the command line, type help function-name to get help for a spe-
cific function, or type lookfor keyword to search for functions related to keyword; to start
the on-line help system, choose MATLAB Help from the Help menu.

1 Get to Know the Environment


Using a web browser, go to the homepage of the course book at
http://www.imageprocessingplace.com/
and download the “Standard” test images (a set of images found frequently in the literature:
Lena, peppers, cameraman, lake, etc.). Try out the basic MATLAB functions for loading,
saving, displaying and manipulating digital images on the test images. A few test images can
also be found on the course web page.
Try also the following example, using functions from the Image Processing toolbox, on
images from the course Web page:

% load color image and display it


I = imread(’mandrill.tif’);
% convert it to an intensity image (gray scale)
I = rgb2gray(I);
% take some image rows: 8, 64, and 128
row8 = I(8,:); row64 = I(64,:); row128 = I(128,:);
% plot something
figure;
subplot(4, 1, 1); imshow(I); title(’Image’); axis image tight on;
subplot(4, 1, 2); plot(row8); title(’Row 8’); axis tight;
subplot(4, 1, 3); plot(row64); title(’Row 64’); axis tight;
subplot(4, 1, 4); plot(row128); title(’Row 128’); axis tight;

Please hand in the plots enclosed in the report.

2 Grey Level Quantization


Write a new M-function to reduce the number of grey levels in a given image to 8. The input
to your function should be a grey-scale image and the output should be another grey-scale
image. Then modify your function so that the number of grey levels in the output image is
determined by a second input parameter to the function.

2
3 Image Histograms
Image histograms are a simple but very useful method to analyze images and to enhance the
quality of the image (at least for a human observer) by performing point transformations.

3.1 Histogram Computation


In this exercise you should write a Matlab function (an m-file) that computes the histogram
of an intensity (gray scale) image. Do not use specific Matlab functions for histogram
computation (like hist or imhist).
Create a new file my hist.m and start with the following lines:

function [h] = my_hist(im)


% H = MY_HIST(IM) computes the histogram for the intensity
% image IM (with values from 0 to 255) and returns a vector H
% with 256 dimensions

% get the image size: M = number of rows, N = number of columns


[M, N] = size(im);
% initilalize the histogram to zero
h = zeros(1,256);

% ... here goes your code ...

end % -- do not forget the END!

To test your function, save the m-file and type at the prompt (omit the comments):

% read image file


>> I = imread(’boat.tif’);
% compute the histogram
>> H = my_hist(I);
% display the result
>> figure;
>> plot(H);
>> axis tight;

What can you see from the histogram? (Could you identify three regions in the histogram,
belonging to the dark foreground, the sea, and the light sky?)

3.2 Histogram Equalization


In this exercise you should write a function (m-file) that performs a histogram equalization
(see lecture notes and course book). Use your function to compute histograms, but do not
use eg histeq.
Begin with something like this (in file my histeq):

3
function [im_out] = my_histeq(im_in)
% IM_OUT = MY_HISTEQ(IM_IN) performs a histogram equalization on the
% input image IM_IN and returns the result in IM_OUT
% both input and output image are intensity images (values from 0 to
% 255)

% ... here goes your code ...

end

Test your function on the image file boat.tif and plot the original image, the histogram,
the cumulative histogram, the histogram and the cumulative histogram of the transformed
image, and the transformed (enhanced) image. Why is this operation called ‘histogram equal-
ization’ ?

4 Background Modelling and Subtraction


Download the video clip Traffic Video gs.avi from the course Web page.

4.1 Handling Video Files


Use the Matlab help in order to find out how to extract images from video files (search for
mmreader, for example).

4.2 Background Modelling


Compute a model of the background, for example by averaging. Describe the background
model and compare it to individual frames of the video sequence. Discuss advantages and
disadvantages of your background model.

4.3 Background Subtraction


Create a video with the background subtracted. Describe your approach and discuss the
features of the resulting video.

You might also like