Matlab Programs For Image Processing
Matlab Programs For Image Processing
- to load image called filename having the dimension xdim and ydim, and which is stored as datatype precision
rawread(filename, xdim, ydim, precision)
filename with datatype precision . For internal storage purposes Matlab treats an image as a matrix. For example to load an image into a matrix try
image=rawread(ctscan.raw, 256, 256);
You can choose to treat rawread, rawwrite as blac 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 uint! ). % img " R#$R%#&(fname,ncols,nrows,precision); % function raw"rawread(fname, cols, rows, prec) if nargin""', precision" uint! ; elseif nargin""(, precision"prec; else, error( $rong number of arguments) *yntax+ ,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""/, precision" uint! ; elseif nargin""', precision"prec;
else, error( $rong number of arguments) *yntax+ ,rawwrite(fname,array), ); return; end fid " fopen(fname, w ); count " fwrite(fid,array,precision) ; fclose(fid);
;-----------------------------------------------------------1) Exercise 1 Rea an is!lay an image Rea " is!.m ;---------------------------------------------filename# $ctscan.raw%; image#rawrea &filename' ()*' ()*); Imagesc&image);
50
100
150
200
Result+ ;-------------------------------The image a!!ear coloure an image+ Ty!e in the comman win ow+ axis(square); colormap(gray); The result now+ istorte . In or er to is!lay a gray un istorte
50
100
150
200
50
100
150
200
250
200
150
100
50
50
100
150
200
250
300
;------------------------------------------------------------exercise ,. Transform a gray image into a binary image To ma e any graylevel image into a a binary image !",# image$ at say a threshold of half the maximum brightness value try :
maximum"max(max(img))5/.6; binimg"(img7maximum); The max operator returns a vector
if applied to a matrix and a number if applied to a vector - so to get the maximum graylevel value in a matrix it has to be applied twice. The same is true for the median, std and min functions. - o"binary.m
filename" ctscan.raw ; % the name of the file to be read image"rawread(filename, /01, /01); figure(2); 3magesc(image); % display original image axis( s4uare ); colormap( gray ); % binari8e maximum"max(max(image)); binimg"(image7maximum5/.6); figure(/); 3magesc(binimg); % display the binary image axis( s4uare ); colormap( gray ); % save filenameout" ctscanbin.raw ;
rawwrite(filenameout, binimg); % it writes not as bit but with the same precision as the original image
50
100
150
200
Find the way to store the image as bits% in order to show the smaller si&e of the file'' ( -------------------.on/olutions + Matlab has facilities for both #) and *) convolutions as implemented in the functions conv and conv/. For example to get a simple edge detection mas convolved with img try :
result"conv/(img,--2 -2 -2;-2 ! -2;-2 -2 -2., same );
(------------------------------------------------------------------0.1ourier Transform 1ourier Transforms+ There are also facilities for #) and *) forward and inverse Fourier Transforms - try the commands fft, fft/, ifft and ifft/. The command fftshift centers the Fourier spectrum origin to the center of the image. +ote 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. ,s an example to display the centered Fourier transform for the image in img type :
fouimg"fft/(img); fouimg"fftshift(fouimg); imagesc(abs(fouimg));
50
50
100
100
150
150
200
200
50
100
150
200
50
100
150
200
The phase
and the real and imaginary parts using the real and imag operators.
50
50
100
100
150
150
200
200
-eal part
imaginary part
.rogram fft"image.m
filename" ctscan.raw ; % the filename to be read img"rawread(filename, /01, /01); figure(2); 3magesc(img); % display axis( s4uare ); colormap( gray ); fouimg"fft/(img); fouimg"fftshift(fouimg);
fouimg2"/9 log26 (2: abs(fouimg));% compresion of the dinamic range figure(/); 3magesc(abs(fouimg2)); axis( s4uare ); colormap( gray ); figure('); 3magesc((fouimg2)); axis( s4uare ); colormap( gray );
/t is worth remembering that multiplication in the Fourier domain in the case of an e0uation of the form 1!u,v$2F!u,v$3!u,v$ would actually be implemented by elementwise multiplication of F and 3 using the .9 operator and not normal matrix multiplication. ( (------------------------------------------------------------------------------------------------Exercise ) The low pass filter: do4lowpass.m
filename" ctscan.raw ; % the filename to be read image"rawread(filename, /01, /01); 3magesc(image); % display axis( s4uare ); colormap( gray ); s8e"si8e(image); rows " s8e(2); cols " s8e(/); %cutoff is the cutoff fre4uency of the filter 6 - 6.0. % n is the order of the filter, the higher n is the sharper % the transition is. (n must be an integer 7" 2). cutoff"6.0; n"2.6; % ; and < matrices with ranges normalised to :5- 6.0 x " (ones(rows,2) 9 -2+cols. - (fix(cols5/):2))5cols; y " (-2+rows. 9 ones(2,cols) - (fix(rows5/):2))5rows;
filt"2 .5 (2.6 : (radius .5 cutoff).=(/9n)) ; f " fftshift(filt); % >he filter figure(/); surf(filt); im " normalise(image); % Rescale values 6-2 (and cast % to ?double if needed). @@>3m " fft/(im); % >aAe @@> of image him " real(ifft/(@@>3m.9f)); % #pply the filter, invert figure('); imagesc(him); axis( s4uare ); colormap( gray );
cutoff2".5
50
100
150
200
50
100
150
200
50
100
150
200
difference between the highpass ( red) and highboost filter for boost"26;
50
50
100
100
150
150
200
200
6oost2*
6oost2#"
him " exp(real(ifft/(@@>log3m.9h))); % #pply the high boost filter, invert % fft, and invert the log.
50
100
150
200
6ray le/el transformations Exercise 7+ 8inear .ontrast Enhancement: do4lcontrast.m Typically, high contrast images are visually more appealing.
0.45 0.4 0.35 0.3 0.25 0.2 0.15 0.1 0.05 0 0 50 100 150 200 250 300
CT Scan 50 100 150 200 250 50 100 150 200 250 2* alfa 50 100 150 200 250 50 100 150 200 250 50 100 150 200 250 50 100 150 200 250
alfa
Exercise 11+ 6ama .orrection : do4adgamma.m The drawbac of the linear contrast enhancemnt is that it leads to saturation. This may be avoided by employing a non-linear contrast ad7ustment scheme such as gamma correction
newim " newim.=(25g);
0.6 0.5 0.5 0.4 0.4 0.3 0.3 0.2 0.1 0 50 100 150 200 250 300 0.2 0.1 0 0 50 100 150 200 250 300
g25
12".;
50
50
100
100
150
150
200
200
g25
12".;
%Ealues of g in the range 6-2 enhance contrast of bright % regions, values 7 2 enhance contrast in darA % regions.
Exercise 1(+ 9 :ust .ontrast : do4ad7constrast.m <ne drawbac of the gamma correction is that the gray values are mapped in an asymmetric fashion with respect to the middle level gray.This may be alleviated by employing a sigmoidal non-linearity .
newim " 2.5(2 : exp(gain9(cutoff-newim))); % #pply *igmoid function
0.6 0.6 0.5 0.5 0.4 0.4 0.3 0.2 0.1 0 0 50 100 150 200 250 300 0.3 0.2 0.1 0 50 100 150 200 250 300
cutoff"6.0;
cutoff"6./;
50
50
100
100
150
150
200
200
cutoff"6.0;
cutoff"6./;
/. Gaussian_m.m
=oise .ancelling
noisy lena filtere with sigma # , filtere with sigma #1
,. .anny E ge etector
display of these images is to grab these images and show them by BxvB or BM,T?,6B. 8hile, you are encouraged to use the given M,T?,6 codes and get these images in M,T?,6 by yourself. Try to change the parameters to get more sense about how these parameters affect the edge detection.
e/iation sigma of
hori>ontal e ges
after thinning
e/iation sigma of
hori>ontal e ges
after thinning
Original Image 50 100 150 200 250 50 100 Iy 50 100 150 200 250 50 100 150 200 After Thresholding 50 100 150 200 250 50 100 150 200 250 250 50 100 150 200 250 50 150 200 250 50 100 150 200 250 50
Ix
250
250
d*dgauss can be used as )erivative of a 1aussian ?in s of materials for image processing and Matlab #.http:==amath.colorado.edu=courses=CD*"=*"""Epr=?abs=8or sheets=Matlab4tutorial=mat labimpr.html /ntroduction to image processing in Matlab # by Kristian Sandberg, Department of Applied Mathematics, University of Colorado at Boulder
50
100
150
200
50
100
150
200
50
100
150
200
50
100
150
200
50
100
150
200
5000
4000
3000
2000
1000
Fxercise #J. Fixed thresholding Aode: do4thresholding.m 68 2 /M*68!/,?FKF?$ converts the intensity image / to blac and white. 6ased on the histogram Thresh 2J". The output binary image 68 has values of " !blac $ for all pixels in the input image with luminance less than ?FKF? and # !white$ for all other pixels.
50
100
150
200
50
100
150
200
50
100
150
200
thresh2#*"
50
100
150
200
dilated with N5
50
100
150
200
50
100
150
200
Froded with N5
50
100
150
200
eroded with N; Fxercise *": do4opening.m Morphology could be also applied with bwmorph function that will use a N5 ernel(
50
100
150
200
op"bwmorph(B$, open );
50
100
150
200
50
100
150
200
50
100
150
200