Professional Documents
Culture Documents
Matlab Programs For Image Processing
Matlab Programs For Image Processing
You can choose to treat rawread, rawwrite as black boxes. The source code is given at
the end of this document.
Rawread.m
;--------------------------------------%Reads a raw intesity image file, written with a given precision (by
default 'uint8').
%
img = RAWREAD(fname,ncols,nrows,precision);
%
function raw=rawread(fname, cols, rows, prec)
if nargin==3, precision='uint8';
elseif nargin==4, precision=prec;
else, error('Wrong number of arguments! Syntax:
"rawread(fname,cols,rows)"'); return; end
fid = fopen(fname,'r');
raw = fread(fid,[cols,rows],precision)';
fclose(fid);
;---------------------------;rawwrite
function count=rawwrite(fname, array, prec)
if nargin==2, precision='uint8';
elseif nargin==3, precision=prec;
;-----------------------------------------------------------1) Exercise 1
Read and display an image
Read_disp.m
;---------------------------------------------filename= ctscan.raw;
image=rawread(filename, 256, 256);
Imagesc(image);
50
100
150
200
250
Result:
50
100
150
200
250
;-------------------------------The image appear coloured and distorted. In order to display a gray undistorted
image:
Type in the command window:
axis(square);
colormap(gray);
The result now:
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
250
200
150
100
50
50
100
150
200
250
300
50
100
150
200
250
50
100
150
200
250
Find the way to store the image as bits in order to show the smaller size of the file!!
; -------------------Convolutions :
Matlab has facilities for both 1D and 2D convolutions as implemented in the functions
conv and conv2. For example to get a simple edge detection mask convolved with img
try :
result=conv2(img,[-1 -1 -1;-1 8 -1;-1 -1 -1],'same');
;------------------------------------------------------------------4.Fourier Transform
Fourier Transforms:
There are also facilities for 1D and 2D forward and inverse Fourier Transforms - try the
commands fft, fft2, ifft and ifft2. The command fftshift centers the Fourier
spectrum origin to the center of the image. Note that the results of these are complex and
to display them one needs to decide on a reasonable representation such as for example
the modulus. As an example to display the centered Fourier transform for the image in
img type :
fouimg=fft2(img);
fouimg=fftshift(fouimg);
imagesc(abs(fouimg));
50
50
100
100
150
150
200
200
250
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
The phase
and the real and imaginary parts using the real and imag operators.
50
50
100
100
150
150
200
200
250
250
50
100
150
Real part
200
250
50
imaginary part
Program fft_image.m
filename= 'ctscan.raw'; % the filename to be read
img=rawread(filename, 256, 256);
figure(1);
Imagesc(img); % display
axis('square');
colormap('gray');
fouimg=fft2(img);
fouimg=fftshift(fouimg);
100
150
200
250
figure(4);
imagesc(angle(fouimg)) ;
axis('square');
colormap('gray');
cutoff=0.3
cutoff=0.1
the filter
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
Exercise 6
High pass filter; do_highpass.m
It is obtained as:
filt= 1.0 - filtlow; where fillow is the lowpass filter described above
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
300
50
50
100
100
150
150
200
200
250
250
50
100
150
200
250
Boost=2
50
100
150
200
Boost=10
Exercise 8
Homomorphic filtering: do_homorphic.m
The homomorphic filtering
im = normalise(im);
cast
% to `double' if needed).
FFTlogIm = fft2(log(im+.01));
% Take FFT of log (with
offset
% to avoid log of 0).
250
him = exp(real(ifft2(FFTlogIm.*h)));
% Apply the high boost
filter, invert
% fft, and invert the log.
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
300
CT Scan
alfa
50
50
100
100
150
150
200
200
250
250
2* alfa
3* alfa
50
50
100
100
150
150
200
200
250
250
newim.^(1/g);
0.9
0.9
0.8
0.8
0.7
0.7
0.6
0.6
0.5
0.5
0.4
0.4
0.3
0.3
0.2
0.2
0.1
0.1
0
50
100
150
200
250
g=3
300
50
100
150
200
250
G=0.5
50
50
100
100
150
150
200
200
250
250
50
g=3
100
150
200
250
50
100
150
200
G=0.5
1./(1 + exp(gain*(cutoff-newim)));
250
300
0.9
0.9
0.8
0.8
0.7
0.7
0.6
0.6
0.5
0.5
0.4
0.4
0.3
0.3
0.2
0.2
0.1
0
50
100
150
200
250
cutoff=0.5;
300
0.1
50
150
200
250
cutoff=0.2;
50
50
100
100
150
150
200
200
250
250
50
cutoff=0.5;
100
100
150
200
250
50
100
150
cutoff=0.2;
200
250
300
2. Gaussian_m.m
Noise Cancelling
noisy lena
noisy lena
50
50
100
100
150
150
200
200
250
50
100
150
200
250
250
50
smooth
50
100
100
150
150
200
200
250
250
100
150
150
200
250
200
250
noise cancel
50
50
100
200
250
50
100
150
display of these images is to grab these images and show them by "xv" or "MATLAB".
While, you are encouraged to use the given MATLAB codes and get these images in
MATLAB by yourself. Try to change the parameters to get more sense about how these
parameters affect the edge detection.
vertical edges
horizontal edges
after thresholding
after thinning
vertical edges
horizontal edges
after thresholding
after thinning
Original Image
Ix
50
50
100
100
150
150
200
200
250
50
100
Iy
150
200
250
250
50
50
100
100
150
150
200
200
250
50
100 150 200
After Thresholding
250
250
50
50
100
100
150
150
200
200
250
50
100
150
200
250
250
50
250
50
250
50
100
150
200
250
exercise 13
Edge Detectors + morphology in Matlab
Prewit operators
Code: do_prewitt.m
Prw_im = edge(image,'prewitt');
50
100
150
200
250
50
100
150
200
250
Exercise 14
Sobel operators
Code: do_sobel.m
sob_im = edge(image,'sobel');
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
200
250
50
100
150
200
250
50
100
150
50
100
150
200
250
50
100
150
200
250
5000
4000
3000
2000
1000
0
0
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
50
100
150
200
250
Exercise 19 Morphology
Dilation: code: do_dilation.m
Different kernels
K3=ones(3); K5=ones(5); K7=ones(7); K9=ones(9);
50
100
150
200
250
50
100
thresh=120
150
200
250
50
100
150
200
250
50
100
150
200
250
100
150
200
250
100
150
200
250
dilated with K3
50
100
150
200
250
50
dilated with K5
etc..
Exercise 20 Erosion
50
100
150
200
250
50
Eroded with K3
50
100
150
200
250
50
100
150
200
250
eroded with K5
Exercise 20: do_opening.m
Morphology could be also applied with bwmorph function that will use a K3 kernel;
50
100
150
200
250
50
100
150
op=bwmorph(BW,'open');
Exercise 21 Closing
Code: do_closing.m
200
250
50
100
150
200
250
50
closing
100
150
200
Exercise 23 Thining
Do_thining.m
50
100
150
200
250
50
Ex
100
150
200
250
Exercise 23 Thickening
Do_thickening.m
50
100
150
200
250
50
100
150
200
250
250