Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

EGH315 Medical Imaging

LABORATORY PRACTICAL Week 7:


What you will learn:

A. Creating your own script m-file


B. Image Deblurring

A. Creating your own script m-file to run batch commands


The MATLAB Editor/Debugger provides a graphical user interface(GUI) for basic text
editing features for any file type, as well as for M-file debugging. The Editor/ Debugger is a
single tool that you can use for editing, debugging, or both.
To create a new file in the Editor/Debugger, click the New Script under the Home tab.

i. m-file script
M-file Scripts contains a collection of commands(as in the Command window).
They are useful for automating series of MATLAB commands, such as repeated
computations.
Simple Script Example
The commands below obtain information from the cell.tif image file, read it into
MATLAB, store into array I and displays it in a figure.

ii. Running & saving script m-file


1st method: Select Run Button under the Editor tab. Enter a name for the file eg. Myscript

2nd method: Save file first. Go to Command Window, & type the file name(without the ‘.m’)
Eg. if your filename is abc.m, type abc in Command window

Page 1 Effective Date: 20 Apr 2020


EGH315 Medical Imaging

iii. Debugging errors & variables


All errors(in red) from script run, will be found in the Command Window. Variable will
be stored in the Workspace

iv. Publishing script files as a report


Publishing m-file to HTML(web), XML, LaTeX, MS Word, PowerPoint formats After
you have completed writing and debugging an M-file script, you can publish the M-file
and its results in any of several presentation formats: HTML(web), MS Word,
PowerPoint, XML, LaTeX. This allows you to present not only the code, but also
commentary on the code and results from running the file.

How to publish an m-file as PDF or HTML(Web format)?

In PC MATLAB:
 Under Publish tab, click Publish. This will publish as default HTML(Web format)

 To publish as Word or ppt:


To modify output format to MS Word or PowerPoint, click the arrow below the Publish
button & click Edit Publishing Options. Select an Output File Format from those listed
in the menu: c HTML, XML, LaTeX, Word, or PowerPoint and click Close.

Page 2 Effective Date: 20 Apr 2020


EGH315 Medical Imaging

B. Image Deblurring

A blurred or degraded image can be approximately described by this equation


g = Hf + n, where:

g  The blurred image


H  The distortion operator is also called the point spread function (PSF). The distortion operator,
when convolved with the image, creates the distortion. Distortion caused by a point spread function
is just one type of distortion. (fspecial() in matlab creates the PSF)
f  The original true image.NB: The image f does not exist. It represents what you would have if
you had a perfect image acquisition conditions.
n  Additive noise(noise signal is added to the original signal to produce a corrupted noisy signal
eg. Gaussian), introduced during image acquisition, that corrupts the image

i. Creating blurring effect in images.


Commands below takes a clear image and deliberately blurs it by convolving it with a
PSF(Point Spread Function) *.
* NB: Quality of the deblurred image is mainly determined by the knowledge in PSF
The example uses the FSPECIAL() function to create a PSF that simulates a motion blur.
Then the example uses the IMFILTER() function to convolve the PSF with the original
image, I, to create the blurred image, Blurred.

NB: PSF is the 3D diffraction pattern of light from source object to the lens

>> I = imread('cell.tif');
>> figure, imshow(I), title('Original Image');

%CREATE parameters to generate PSF


>> LEN = 31; LEN= linear motion of camera by LEN pixels
>> THETA = 11; THETA= angle of blur
>> PSF = fspecial('motion',LEN,THETA); -> creates a 2D filter/matrix
-> performs multi-dim filtering
%CREATE a blurred image using imfilter
>> Blurred = imfilter(I,PSF,'conv','circular');
>> figure, imshow(Blurred), title('Blurred Image');

Q1 :
Change the THETA value to 45, 90. Describe the effects.

Answer: ________________________________________________________

Page 3 Effective Date: 20 Apr 2020


EGH315 Medical Imaging

ii. Applying deblurring function to blurred image


Use the DECONVWNR(): It restores image that was degraded by convolution with a point-
spread function, PSF , and possibly by additive noise. Wiener deconvolution can be used
effectively when the frequency characteristics of the image and additive noise are known, to
at least some degree. In the absence of noise, the Wiener filter reduces to the ideal inverse
filter.

>> est_nsr = 0; % NSR of additive noise


% deconvolves image Blurred using the Wiener filter algorithm.
% PSF point-spread function with which I was convolved
>> wnr1 = deconvwnr(Blurred,PSF, est_nsr); %use deconvwnr
>> figure, imshow(wnr1);
>> title('Restored, True PSF');

Change the est_nsr to 0.01, 0.0001, 0.008 & describe the effect.

Answer: ____________________________________________________________

What you have achieved:

A. Image Deblurring
B. Creating your own script m-file

Page 4 Effective Date: 20 Apr 2020

You might also like