Image Processing With MATLAB
Image Processing With MATLAB
1.1 Introduction
MATLAB is a very powerful, high level language; It is also very easy to use. It comes
with a wealth of libraries and toolboxes, that you can use directly, so that you don't need
to program low level functions. It enables us to display very easily results on graphs and
images. To get started with it, you need to understand how to manipulate and represent
data, how to find information about the available functions and how to create scripts and
functions to generate programs.
You have already had an introduction to MATLAB in Semester 1 in the course Signal
Processing with MATLAB. This course seeks to build on that experience by exposing
you to using MATLAB to manipulate and process Images.
The aim of the course is to build on the semester 1 course on Image Processing and to
apply the methods learned there to real images.
The course is organised into 10 compulsory laboratories. The laboratories will take place
from 9am-12noon every Wednesday in weeks 1-10 of semester 2. In addition, a 2hour lab
slot is booked from 10-12noon for use by students although this slot will be
unsupervised. If you cannot attend the Wednesday laboratory for any reason them you
must inform Prof McLaughlin by email as soon as possible.
These notes are grouped into 5 sessions that students will be expected to work through
the exercises described in these notes at their own pace. The sessions are as follows:
Students will be required to keep a laboratory notebook which will be submitted at the
end of the semester. In each of the sessions described in these notes there are a series of
exercises that students are asked to work through. Students will record all comments and
observations on these exercises in their laboratory notebooks in sufficient detail that
someone reading the notebook could reproduce their results.
The day-book is a diary, a journal, a day by day, hour by hour, record of the work which
you do in the pursuit of the objectives of the practical work. In it, you should make an
accurate and comprehensive record of your activities, the resources used, the results
obtained and such notes on interpretation, planning and other organisation of the work,
that another person, conversant with the area of your work, could reconstruct your
activities, in full.
It is a chronological record; one made at the time and place of the activity.
It is not a formal report, neatly written up after the event, edited to produce a consistent
and logical flow of ideas and actions. It is life in the raw. It contains working notes, made
as thoughts and plans are conceived, modified in real time as the ideas develop. It is a
book of rough working. Nonetheless, the records should be legible.
The records in a laboratory notebook must be dated and the pages are numbered.
By following your laboratory notebook, another person should be able to reconstruct
(literally, if necessary) all the work which you have performed, in the order in which you
performed it. You should expect your day-book to be inspected and commented on
through the course of the semester by the lab supervisor or laboratory demonstrator.
When this happens, they will sign and date the book at the latest record.
Note: All work must be the students own work and plagiarisms and copying will be
subject to penalty.
It is an interactive system (all data always in memory) with the following key facts:
You can get help on all commands in matlab by typing (beware, matlab is case
sensitive!):
For instance, if you are looking for the cosine or related functions, type
>> lookfor cosine
matlab comes back with all matlab functions related to cosine with a short description as:
Note that if you press the "up" and "down" arrows on your keyboard, this enables you to
recall previous commands.
Type :
>> x = [1 2 3 4 5 4 3 2 1];
This will create a vector of size 1 by 9 called x which contains the numbers specified
above
>> x
>> who
This will tell you about all the matrices/vectors you have in your current workspace.
>> whos
>> y = [6; 7; 8; 9; 0; 9; 8; 7; 6]
Create a new matrix called y of size 9 by 1. (note that is there is a semicolon at the end
of the instruction, matlab doesn't expand the result of the instruction)
>> y'
>> z = [1 2 3; 4 5 6; 7 8 9; 0 1 2]
5. Operators:
NOTE: MOST OF THEM WORK ON MATRICES DIRECTLY.
A way to define quickly some vectors is to use the ":" operator. Type for instance (and
observe the results):
>> a = 0:10
>> b = -5:10
>> c = 0:2:10
>> d = 10:-2:-5
>> e = 0:0.01:4.2
>> f = -pi:0.01:pi
You can see that these instructions are of the form lower limit: increment: upper limit. If
no increment is given, it is assumed the increment is 1.
If you wish to define elementary vectors that only contain ones and zeros, then the
commands
zeros and ones are useful. Also randn to create a matrix of random numbers.
Look at the help on zeros, ones, randn. To illustrate ones and zeros try:
Try:
>> g = zeros(2,4)
>> h = ones(5,3)
These are the standard operators. (+ plus, - minus, / divide, * multiply, ^ exponent)
Try
>> 3^2
>> y'
>> x+y'
>> x*y
>> 3*x
The operators ( / , * and ^) have also got a matrix counterpart (respectively ./ , .* and
.^), which apply the operation element by element. For instance, compare: x*y and
x.*y. Type:
>> x^2
>> x.^2
>> x.*x
>> x*x
if you type "whos" now, you will see that you have a lot of variables in the workspace.
Type
clear, and whos again and you will see that your workspace has been cleared.
Type
>> 34 34 34 34
To investigate further
>> A(4,2)
>> A(4,5)
Type:
>> t =-pi:0.01:pi;
>> c = cos(t);
>> s = sin(t);
6. Plotting functions:
Type
>> figure
>> plot(s)
>> figure
>> plot(t,s)
>> figure
>> plot(t,s,'g',t,c,'r')
Type help plot if you haven't already done it to see more possibilities.
Try and modify the axis, put a title and labels on the x- and y- axes…
Add a title and a label on the x-axis.
7. Image processing:
Try
Try
Try
figure(1)
imshow(im1); % Display matrices as images
figure(2)
imshow(im2); % Same thing with 2 windows.
Play with the figure menus and especially the export button that allows saving images
and figures. Type help figure for more information.
Try
subplot(2,1,1)
imshow(im1); % Display matrices as images
subplot(2,1,2)
imshow(im2); % Same thing with 2 images in one
% window.
Try
Try
clf;
subplot(3,1,1)
imshow(im1(:,:,1)) % Displays Red component of the image
title(‘Red Component’);
subplot(3,1,2)
imshow(im1(:,:,2)) % Displays Green component of image
title(‘Green Component’);
subplot(3,1,3)
imshow(im1(:,:,3)) % Displays Blue component of the image
title(‘Blue Component’);
BE CAREFUL, OPERATORS DO NOT WORK ON UINT8 TYPE
Try
Try
Substraction:
Try
ima = im1 - im2;
imshow(ima);
Multiplication:
Try
ima = im1*im2;
This is a matrix multiplication which cannot be applied to multiply images.
The correct way to do this is:
ima = im1 .* im2;
imshow(ima);
Other operators
/ Matrix division
^ Power operator
‘ Complex transpose
As matlab is interpreted, the data are always in the workspace and can be saved. The
workspace can be seen as an area of memory accessible from the command line.
1. Matlab resources:
After this you will know most of what you need to know about Matlab and should
definitely know how to go on learning about it on your own. So the "programming"
aspect won't be an issue any more, and you will be able to use matlab as a tool to help
you with you image processing. Programming languages don't come any easier!
2. Matlab resources:
Ü Environment
o object oriented programming.
Ü Mathematical libraries
o Profiler (evaluate performances)
Just type edit in the command line and the editor starts.
This should start the editor with a new file. Write the following in the file and save the
file as readimage.m in your local directory. To go to your local directory, use the cd
command as seen last week.
You've already written your first elementary script. Standard branching and looping
instructions can be used as well: "for" loops, "if" statements etc… To find out about their
syntax, type "help" for these different instructions.
5.1 if statement:
switch (expression)
case value1
action1;
case value2
action2;
otherwise
default action;
end
for index = 1 : m
r(index) = index;
end
Example:
while (expression)
Action
end
5.6 Functions
Functions differ from scripts in that they take explicit input and output arguments. Type
"help" function to know more and see examples. As all other matlab commands, a
function can be called within a script of from the command line.
function [out1,out2,...,outn] =
function_name(in1,in2,...,inn)
Tips:
nargin: gives the number f input arguments
allow defaults parameters
allow variable number of arguments
As an example, here is a basic function that reads in an image, displays it and finds the
min and max of the image. The function must be saved into a file of the same name as
the function to be accessible from the command line. In our case we save it as
min_max.m.
Try
>>[mini,maxi] = min_max(‘peppers.png’);
Try to modify this function to get the maximum and minimum of each color
6. Debugging a program
Debugging is easy in matlab as it is an interpreted language. In order to start the debug
mode, you just have to type in the command line:
Alternatively, you can go into the editor and set the options and breakpoints where you
want. In any case, the editor will be invoked and show the line where the error has
occurred.
In this case the K symbol appears in the command line.
As matlab is interpreted you can change the values of variables and continue the
execution using:
Finally you can use the keyboard functions. This does not start the debugger but stops the
execution where the keyboard has been typed. You can see the variables and resume
execution typing dbcont.
7. Function/Command Duality
8. General Tips
8.1 Vectorization:
x = 0; x = 0:0.01:10;
for k = 1 : 1001 y = log10(x);
y(k) = log10(x);
x = x +0.01; 10 times faster!
end
plot(x,y);
8.2Preallocation:
8.3 Matrices
These programs must be downloaded from the Image Processing module on vision:
http://www.see.ed.ac.uk/~sml/Image_Processing/
Please stick this page into your bookmarks as it will be useful for the remainder of
the course.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Image inversion
%
% ima_out = invert(ima)
%
% This function inverts the input image
%
% ima : real input image
% ima_out : output real
%
%
% Yvan Petillot, December 2000
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Note in this example, the use of ndims to find out the number of dimensions of the input
array.
Additional Exercise:
Could you alter this script to invert each colour and generate a inverted RGB image?
Try it in your spare time...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 2 Dimensional Fourier Transform
%
% [imafft,mag,phi] = fft2d(ima_in)
%
% This function calculates the complex Fourier transform
% and the associated magnitude and phase (mag, phi) of the input real
% image ima_in.
%
% ima_in : real input image
% imafft : output complex image
% real and imaginary part can be accessed through real
% and imag function (see help real, imag)
%
% mag,phi: magnitude and phase of the fourier transform
%
% Yvan Petillot, December 2000
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Note that ima_out is an array of complex numbers stored as real and imaginary parts.
real(ima_out) returns the real part while imag(ima_out) returns the imaginary
part. Magnitude and phase can be obtained from the real and imaginary part using the
abs and angle functions. This functions, as always in matlab work directly on arrays.
Additional Exercise:
What would happen if the image was rotated?
Try it using the imrotate function to rotate the image with and without the crop
parameter (see help imrotate) on the ‘circuit.tif’ image.
Try
>> ima = imread(‘circuit.tif’);
>> ima1 = fft2d(ima);
>> ima2 = fft2d(imrotate(ima,30));
This function will perform the inverse Fourier Transform. First try to take the Fourier
Transform and then the inverse Fourier Transform to return to the original image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 2 Dimensional Inverse Fourier Transform
%
% [ima] = ifft2d(ima_in)
%
% This function calculates the complex Inverse Fourier transform
% of the complex image ima_in.
%
% ima_in : complex input image
% ima : output (possibly complex) image
% real and imaginary part can be accessed through real
% and imag function (see help real, imag)
%
%
% Yvan Petillot, December 2000
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ima = fftshift(ifft2(fftshift(ima_in)));
%Display results
clf; %Clear display
figure(1); %Create figure
imagesc(abs(ima)); % Scale image to [0 255] range and display it
axis square; % Display equal on x and y axes
colormap(gray); % Sets gray scale colormap;
drawnow; % Ready to dipslay: Do it
This is simply to illustrate how matlab functions can be called from other functions. This
function can perform either the Fourier Transform or the inverse, depending on the
setting of the direction argument.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 2 Dimensional generic Fourier Transform
%
% [ima_out] = fft2(ima_in,direction)
%
% This function calculates the complex Fourier transform
% of the input image ima_in. If direction is set to 1, the direct fft
% is performed, otherwise, the inverse one performed.
%
% ima_in : input image. Can be real or imaginary
% direction: optional parameter. By default, direct Fourier
transform is assumed
% imafft : output image
%
%
% Yvan Petillot, December 2001
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin == 1
direction = 1;
end
if (direction == 1)
[ima_out,mag,phi] = fft2d(ima_in);
else
ima_out = ifft2d(ima_in);
end
1. Objectives:
We will investigate the notion of spectrum and simple filtering in the frequency domain.
As we will see, in all cases, we can interpret the effect of these filters in the frequency
domain easily while their effect is not obvious in the time domain.
2. Resources required:
In order to carry out this session, you will need to download images and matlab files from
the Image Processing module on vision.
http://www.see.ed.ac.uk/~sml/Image_Processing/
• Image of Lena
• Landsat image
• sonar image
• sar image
We will also use the circuit and flowers images (circuit.tif, peppers.png).
You are of course encouraged to try these programs out on images of your choice.
Please download the following functions and script:
1. Objectives:
We will investigate the properties of the histogram and its various uses for image
preprocessing, visualisation and segmentation. During this session, you will learn &
practice:
2. Resources required:
In order to carry out this session, you will need to download images and matlab files from
the Image Processing module on vision:
http://www.see.ed.ac.uk/~sml/Image_Processing/
• Image of Lena
• Landsat image
We can also use other images – for example circuit image. (circuit.tif). Note that a full
list of all the images supplied as part of matlab can be accessed through help
images/imdemos.
You are of course encouraged to try these programs out on images of your choice.
3. Histogram manipulation:
As already seen in class, the histogram of an image is a simple way to see how the gray
level (i.e. the information) is spread over the available range of grey levels. This in turn
can be used to highlight problems during the acquisition (clipping due to over exposure,
low contrast). The histogram can also be modified to make better use of the available
range for further processing and display.
There is another way to see the histogram in the light of probability theory. An image can
be seen as a random field (array) of pixels, each pixel being a random variable. Assuming
these pixels follow the same statistical law, then the histogram can be interpreted as an
estimator of the probability density function (pdf) of this law. This fact has been used for
texture classification using local histograms for example (here, the texture are modelled
locally by the histogram).
To Try:-
Comments?
Use the histeq function to specify histograms for various images and compare the
result with the desired histogram.
Now have a look at the imadjust function and see how it is related to histeq.
4. Matlab Demos
Matlab has a number of demos for Image Enhancement. Investigate the use of Histogram
Equalisation and Simple transformation functions using the demo “Intensity
Adjustment and Histogram Equalisation”. (At the prompt type demos and select the
Image Processing Toolbox) Also investigate the demo “Contrast Enhancement
Techniques”. Work through the steps provided to see the use of the imadjust,
histeq and adapthisteq functions.
Session 4: Filtering
1. Objectives:
We will demonstrate various aspects of filtering, including spatial and frequency domains
and we will analyse the frequency response of various filters and their interpretation.
2. Resources required:
In order to carry out this session, you will need to download images and matlab files from
http://www.see.ed.ac.uk/~sml/Image_Processing/
• Image of Lena
• Landsat image
We will also use the circuit and flowers images (circuit.tif, flowers.tif).
You are of course encouraged to try these programs out on images of your choice.
Please download the following functions and script:
• SimpleFiltering.m
• gene_filters.m
• Analyse_Filter.m
• Deconvolution.m
3. Simple Filtering:
The simplest form of filtering are high and low pass filters applied directly in the
frequency domain. This is illustrated in the SimpleFiltering program as you might
have time to see in tutorial 2.
It is interesting to note that a finite frequency response (as it is the case for ideal low pass
and band-pass filtering) leads to an infinite space signal (image) and that these filters are
in general not realizable as they are not causal.
The SimpleFiltering() function applies either a low pass of high pass filter to the
image with the specified cut-off frequency. The syntax for the function is
ima_out = SimpleFiltering(ima,type,cutoff)
where
ima = imread(‘lena.tif’);
ima_out= SimpleFiltering(ima, 0, 0.5);
ima = imread(‘lena.tif’);
ima_out= SimpleFiltering(ima, 1, 0.5);
repeat the above altering the cut-off frequency, and noting the effect.
This type of filtering is more of the type feature extraction as we define the response of
the filter in function of the type of characteristics of the image we want to extract. More
efficient filters (both in terms of response and computation time) have been proposed
over the years to perform these type of operations.
Spatial domain filters are generally defined as small windows (or images) that are
convolved with the original image (see your notes on filtering). In the spatial domain, the
action of a filter can be seen by looking at the structure of the filter. For instance a mean
filter will just replace the value of the current pixel by the mean of its neighbour. This is
typically a low pass filtering operation as we will show shortly. Conversely, a Prewitt or
Sobel filter acts as a derivative and tends to locate edges. This is typically a high-pass /
band-pass filtering operation.
To examine the use of such spatial domain filters two functions are required. First run
gene_filters this will create a set of masks for a range of filters as small matrices
labelled as given below. The second function Analyse_Filter() will then apply the
specified filter to the image.
Using the gene_filters function, you will have generated the following filters as
matrices as labelled below (type whos to see the list of active matrices):
fmean3 [3x3] mean filter
These filters are all low pass filters. The interest of the Gaussian filter lies in the fact that
the Fourier transform of a Gaussian is a Gaussian and therefore the frequency response of
the filter is very smooth with no virtually no sidelobes.
>> gene_filters
>> ima = imread(‘circuit.tif);
>> ima2 = Analyse_Filter(ima, fmean3);
Analyse_Filter() will display the original image, the frequency response of the
image and the resulting filtered image. It will also plot the frequency response of the filter
as a 3D plot in a separate window.
You should now repeat the procedure above the other filters. (Note: you don’t have to
repeat the gene_filters and imread – only the Analyse_Filter stage)
Using the gene_filters function, you will also have generated the following filters:
These filters are all band-pass or high-pass filters used to perform edge detection
Use the function Analyse_Filter(image,filter) again, to see the effect of
each filter on the image and their associated frequency response. This will only work on
grey level images but could be easily extended to colour images. You can see that these
filters are not always ideal and have a fairly smooth frequency drop-off while you would
prefer a quick one for better performance. The last filter (ftop) is designed using an
optimal design technique (Remez) mainly used in 1D signal processing using FIR
filtering theory. The cut-off frequencies can be accurately chosen and the response of the
filter is more controllable.
Try it on various images.
All these filters do not take into account noise in the images and are not adapted to the
spectral content of the specific image we are dealing with. This is a major drawback in
some applications and can be dealt with using adaptive filtering (Wiener for instance).
All the filters considered here are linear filters. Some more advanced filters, usually
nonlinear, have also been developed to perform feature extraction. It is useful to note
filters such as Canny for edge detection.
So far we have used filters merely as feature extraction tools (edges, low frequencies).
We have also only considered linear filters where the resulting image is obtained by
convolution of the input image with the filter, making the analysis simple.
But filters are also used to perform noise removal on images corrupted by various sources
of noise. The noise can be additive (Gaussian noise, salt and pepper noise) or
multiplicative (Speckle noise). In each case a filter adapted to the problem must be
selected and we will see a few examples of well adapted filters for each case.
Using the imnoise function, it is possible to add a specific type of noise to an image.
ima = imread('circuit.tif');
ima1 = imnoise(ima,'gaussian',0.2,0.01);
Note that the mean and variance are in the range [0,1] as a percentage of the max gray
level. The imnoise() function is a matlab function and for Gaussian noise it requires
the mean and variance of the noise to be specified.
It can be shown that Gaussian or mean filters are best used to remove this type of noise.
Try the Analyse_Filter function again to see how you can denoise the image. For
example, you can try
You should also try the other filters (fmean5, fmean11, fgaussian5_1 etc).
However, there is no theoretical backing to this assertion and adaptive filtering is a better
solution in most case.
Using the imnoise function, it is possible to add a specific type of noise to an image.
For example you could do:
ima = imread('circuit.tif');
ima1 = imnoise(ima,'salt & pepper',0.02);
It can be shown that median filters are best used to remove this type of noise.
Try the medfilt2 function (help medfilt2) and see how you can denoise the
image.
ima2= medfilt2(ima1);
This is a multiplicative noise, very frequent in Radar and Satellite imagery. It is very
difficult to remove as it is multiplicative and very often correlated to the signal. Try:
ima = imread('circuit.tif');
ima1 = imnoise(ima,'speckle',0.02);
And try various filters on it to remove noise.
The issues of filtering for noise reduction can also be examined using the Matlab Demo –
“Noise Reduction Filtering”.
7. Adaptive Filters
As noted above an adaptive filter can often provide better noise reduction. One such filter
is the Wiener filter. The matlab function wiener2() performs 2-D adaptive noise-
removal filtering. It lowpass filters an intensity image that has been degraded by constant
power additive noise. It uses a pixel-wise adaptive Wiener method based on statistics
estimated from a local neighbourhood of each pixel.
filters the image using pixel-wise adaptive Wiener filtering, using neighbourhoods of size
M-by-N to estimate the local image mean and standard deviation. If you omit the [M N]
argument, M and N default to 3. The additive noise
(Gaussian white noise) power is assumed to be NOISE.
To illustrate the difference between the mean and Wiener filters, try the following:-
ima = imread('circuit.tif');
ima1 = imnoise(ima,'gaussian',0.2,0.01);
ima2 = wiener2(ima, [3,3]);
ima3 = Analyse_Filter(ima1, fmean3);
figure(1);
subplot(2,2,1);
imshow(ima);
title('Original Image');
subplot(2,2,2);
imshow(ima1);
title('Noise Added');
subplot(2,2,3);
imshow(ima2);
title('Wiener Filtered Image');
subplot(2,2,4);
imshow(ima3);
title('Mean Filtered Image');
8. Edge Detection
A range of different edge detectors including Roberts Cross, Prewtitt, Sobel, Laplacian of
Gaussian and Canny were introduced in class. Use the matlab demo “Edge Detection” to
investigate the difference between them.
Imagine that the real scene that you are after has been distorted by the imaging system
that you are using (out of focus lens, atmospheric perturbation,…). What you see is the
result of the convolution of the real scene by a known (or unknown) transfer function.
When the transfer function is known, you can, in the frequency domain, divided the
spectrum of the observed image by the spectrum of the transfer function and recover the
original image. To illustrate this, try the deconvolution program. Edit the program
and change the level of noise to see the effect of the instabilities in the filter.
However, direct deconvolution is very sensitive to noise as you might not have access to
the exact transfer function and also because you are dividing by the spectrum of the
transfer function (inverse problem). For frequencies where the signal in the image is
small and the noise signal is important, you are going to amplify the noise…
1. Objectives:
2.
We will demonstrate various aspects of developing Hough transform code in MATLAB.
In particular we will discuss:
1. How to discretize r and θ.
2. How to calculate the Calculate for each foreground pixel (x,y) in the image the
values of r=x cos θ + y sin θ for the selcted values of θ.
3. How to create an accumulator array whose sizes are the number of angles θ
and values r in the chosen discretizations.
4. How to step through the values of r and update the acculator array as you
proceed.
2. Resources required:
In order to carry out this session, you will need to download images and matlab files from
http://www.see.ed.ac.uk/~sml/Image_Processing/
You are of course encouraged to try these programs out on images of your choice.
Observe that even though a particular pair (r, θ) corresponds to only one line,a given
line can be parameterised in a variety of ways. For example, the line x+y=-3 can be
parameterised as θ=5π/4 and r=√(3/2). Alternatively if we use θ=π/4 and r=-√(3/2).
So there is a choice of where the values of θ can be restricted to -π/2≤ θ≤π/2and let r
have both positive and negative values, or we may let θ take any value chosen from
the range 0≤ θ≤2π and restrict r to non-negative values.
Question: Bearing in mind what we will wish to do with the values what would be
the most sensible option?
We can choose any discrete set of values we like, but let us take all the integer degree
values in the given range and convert them to radians for the purposes of calculation.
>>angles=[-90:180]*pi/180;
3.2 Calculating the r values
If the image is binary then we can find the positions of all the foreground pixels with
a simple application of the find function.
>>[x,y]=find(im);
If the image is not binary, we can create a binary edge image by a variety of
techniques discussed in the lecture course.
>>z=floor(x*cos(angles)+y*sin(angles));
If an image is m x n pixels the if the origin is to be at the top left corner then the
maximum value of r will be √(m2+n2 ). The size of the accumulator array can then be
set at √(m2+n2 )*270. However, we are only interested in positive values of r given
the choice of the range of θ. By finding the largest positive value of r and use that as
the dimension of the array.
>>rmax=max(r(find(r>0)));
>>acc=seros(rmax+1,270);
We must now step through the array of r values, and for each value (r, θ) increase the
corresponding accumulator value by one.
4. Exercises
Use the image files square.tif and circuits.tif in the following and use the methods
discussed above to develop a hough transform..
Only one line must be found in the image. The line should correspond to the maximum of
the accumulator. The input image is square, that is, it has an equal number of rows and
columns. The size of the accumulator should be the same as the image.
Do not try to build the complete functionality at once. Build small parts which you can
test with simple data.
Step 1:
You can use the matlab function find. Try help find in Matlab. Notice that in Matlab
the first index refers to the row (y-coordinate) of a matrix, and the second one
corresponds to the column number (x-coordinate).
Step 2:
Use the normal-form parameterisation of the line.