You are on page 1of 20

Lab-7

Image Processing in Matlab


7.1 Objective

 To understand image and its types


 To overview Digital image processing and Biomedical Imaging
 Use of Matlab in image processing
 Simple statistics related to images

7.2 Image

An image is defined as a two-dimensional function(x,y), where x and y are spatial coordinates, and
the amplitude of F at any pair of coordinates (x,y) is called the intensity of that image at that point.
When x,y, and amplitude values of F are finite, we call it a digital image. an image can be defined by
a two-dimensional array specifically arranged in rows. Digital Image is composed of a finite number
of elements, each of which elements have a particular value at a particular location. These elements
are referred to as picture elements, image elements, and pixels. A Pixel is most widely used to
denote the elements of a Digital Image.

7.2.1 Types of an image

 BINARY IMAGE– The binary image as its name suggests, contain only two-pixel elements i.e., 0 &
1, where 0 refers to black and 1 refers to white. This image is also known as Monochrome.
 BLACK AND WHITE IMAGE– The image which consist of only black and white color is called
BLACK AND WHITE IMAGE.
 8-bit COLOR FORMAT– It is the most famous image format. It has 256 different shades of colors
in it and commonly known as Grayscale Image. In this format, 0 stands for Black, and 255 stands
for white, and 127 stands for gray.
 16-bit COLOR FORMAT– It is a color image format. It has 65,536 different colors in it. It is also
known as High Color Format. In this format the distribution of color is not as same as Grayscale
image.

A 16-bit format is divided into three further formats which are Red, Green and Blue. That famous
RGB format.

7.3 Digital Image Processing

Digital Image Processing means processing digital image by means of a digital computer. We can also
say that it is a use of computer algorithms, to get enhanced image to extract some useful
information.

7.4 Biomedical Imaging

Biomedical imaging concentrates on the capture of images for both diagnostic and therapeutic
purposes. Snapshots of in vivo physiology and physiological processes can be garnered through
advanced sensors and computer technology. Biomedical imaging technologies utilize either x-rays
(CT scans), sound (ultrasound), magnetism (MRI), radioactive pharmaceuticals (nuclear medicine:
SPECT, PET) or light (endoscopy, OCT) to assess the current condition of an organ or tissue and can
monitor a patient over time over time for diagnostic and treatment evaluation

7.5 Matlab Usage in biomedical imaging

The use of images in biomedical engineering is increasingly common. Medical images can be
acquired using modalities such as magnetic resonance (MR), X-ray computed tomography (CT) and
ultrasound (US). In this section we will introduce how to read, write, and display general imaging
data using MATLAB

7.5.1 Example

The following example illustrates the use of several built-in image-related MATLAB commands.

% read image

im = imread('pout.tif');

% display image

imshow(im);

% modify image

im(100:120,100:120) = 0;

% save image (saves 'im' array)

imwrite(im, 'fluoro_imwrite.tif');

% save image (saves figure as displayed)

saveas(gcf, 'fluoro_saveas.tif');

The imread command reads imaging data from a file with a name specified by the single argument.
The value returned is a 2-D array of pixel intensities. Images are treated as normal 2D arrays in
MATLAB. The intensities can be of different types such as int8, single or double, but within a single
image all must have the same type, as with all MATLAB arrays. Imaging data can be manipulated just
like any other MATLAB array, as we can see from the third non-comment line in the above example
in which a rectangular block of pixels has its intensities set to 0.

The imshow command can be used to display a 2-D image in a figure window. The single argument is
the 2-D array of pixel intensities.

This example shows two ways in which the image can be written to an external file. The imwrite
command takes a 2-D array and a file name as its only arguments, so the image data written to the
file depends only on the array and not on any figure currently being displayed. The saveas command
is used in this example to save the data in the current figure (gcf means get current figure).
Therefore, any changes made to the displayed figure will also be saved, but any changes made to the
original array since it was displayed (as in the example above) will not be saved. In this example, save
as saves the figure as an image, but it can also be used to save in the native MATLAB figure format,
which has a “.fig” file extension.
Both the imread and imwrite commands can handle a range of different imaging formats, such as
bmp, png and tif. For a full list of supported formats, type imformats at the MATLAB command
window. Normally, just adding the extension to the file name is enough to tell MATLAB which format
we want to use.

7.6 Image tool and Image View App

 imtool opens the Image Viewer app in an empty state. Use the File menu options Open or
Import from Workspace to choose an image for display.
 imtool(I) displays the grayscale image I in the Image Viewer.
 imtool(I,[low high]) displays the grayscale image I in the Image Viewer, specifying the display
range for I in the vector [low high]. The value low (and any value less than low) is displayed as
black, the value high (and any value greater than high) is displayed as white. Values in between
are displayed as intermediate shades of gray. The Image Viewer uses the default number of gray
levels. If you use an empty matrix ([]) for [low high], the Image Viewer uses [min (I(:)) max(I(:))];
the minimum value in I is displayed as black, and the maximum value is displayed as white.
 imtool(RGB) displays the true color image RGB in the Image Viewer.
 imtool(BW) displays the binary image BW in the Image Viewer. Pixel values of 0 display as black;
pixel values of 1 display as white.
 imtool(X,map) displays the indexed image X with colormap map in the Image Viewer.

7.6.1 Example

Display an image from a file.

imtool('board.tif')

Display an indexed image.

[X,map] = imread('trees.tif');

imtool(X,map)

Display a grayscale image.

I = imread('cameraman.tif');

imtool(I)

Display a grayscale image, adjusting the display range.

h = imtool(I,[0 80]); close(h)

7.7 Image Statistics

7.7.1 Mean Square Error

The mean squared error (MSE) tells you how close a regression line is to a set of points. It does this
by taking the distances from the points to the regression line (these distances are the “errors”) and
squaring them. The squaring is necessary to remove any negative signs. It also gives more weight to
larger differences. It’s called the mean squared error as you’re finding the average of a set of errors.
The lower the MSE, the better the forecast
Figure 7.1 Mean Square Error Formula

7.7.1.1 Example

%Read Image

ref = imread('pout.tif');

%Show Image

imshow(ref)

% Applying Filter for adding noise

A = imnoise(ref,'salt & pepper', 0.02);

%Show Image

imshow(A)

%Applying Mean Squared Error

err = immse(A, ref);

%Show Image

fprintf('\n The mean-squared error is %0.4f\n', err);

7.7.2 Structural Similarity Index (SSIM)

The Structural Similarity (SSIM) index is a method for measuring the similarity between two images.
The SSIM index can be viewed as a quality measure of one of the images being compared, provided
the other image is regarded as of perfect quality.

Figure 7.2 Structural Similarity Formula


7.7.2.1 Example

%Read Image

ref = imread('pout.tif');

% Making Filter

H = fspecial('Gaussian',[11 11],1.5);

%Applying filter

A = imfilter(ref,H,'replicate');

% Ploting the Image before and after applying filter

subplot(1,2,1); imshow(ref);

title('Reference Image');

subplot(1,2,2);

imshow(A);

title('Blurred Image');

% Making array and applying Structural Similariity Index

[ssimval, ssimmap] = ssim(A,ref);

% Print value of SSIM

fprintf('The SSIM value is %0.4f.\n',ssimval);

% Showing Figure of After Applying SSIM

figure, imshow(ssimmap,[]);

%Giving Title with Sprintf command

title(sprintf('ssim Index Map - Mean ssim Value is %0.4f',ssimval));

7.8 Tasks

7.8.1 Under Image Processing Toolbox of Matlab Use example codes of:
Apply these codes on your own facial image and one brain image obtained from CT scan or
Xray. In the end explain the working of code and its outputs in your words.

Deblurring Images the Blind Deconvolution Algorithm

a) Facial Image

Code

For facial image

%Importing image
I = imread('2.jpg');

figure;imshow(I);title('Original Image');

%Converting rgb image into greyscale

I=rgb2gray(I);

%Applying blur

PSF = fspecial('gaussian',7,10);

Blurred = imfilter(I,PSF,'symmetric','conv');

figure;imshow(Blurred)

title('Blurred Image')

%Recovering image

UNDERPSF = ones(size(PSF)-4);

[J1,P1] = deconvblind(Blurred,UNDERPSF);

%imshow(J1)

%title('Deblurring with Undersized PSF')

OVERPSF = padarray(UNDERPSF,[4 4],'replicate','both');

[J2,P2] = deconvblind(Blurred,OVERPSF);

%imshow(J2)

%title('Deblurring with Oversized PSF')

INITPSF = padarray(UNDERPSF,[2 2],'replicate','both');

[J3,P3] = deconvblind(Blurred,INITPSF);

figure;

imshow(J3)

title('Deblurring with INITPSF')

figure;

subplot(2,2,1)

imshow(PSF,[],'InitialMagnification','fit')

title('True PSF')

subplot(222)
imshow(P1,[],'InitialMagnification','fit')

title('Reconstructed Undersized PSF')

subplot(2,2,3)

imshow(P2,[],'InitialMagnification','fit')

title('Reconstructed Oversized PSF')

subplot(2,2,4)

imshow(P3,[],'InitialMagnification','fit')

title('Reconstructed true PSF')

WEIGHT = edge(Blurred,'sobel',.08);

se = strel('disk',2);

WEIGHT = 1-double(imdilate(WEIGHT,se));

WEIGHT([1:3 end-(0:2)],:) = 0;

WEIGHT(:,[1:3 end-(0:2)]) = 0;

figure

imshow(WEIGHT)

title('Weight Array')

[J,P] = deconvblind(Blurred,INITPSF,30,[],WEIGHT);

imshow(J)

title('Deblurred Image')

P1 = 2;

P2 = 2;

FUN = @(PSF) padarray(PSF(P1+1:end-P1,P2+1:end-P2),[P1 P2]);

[JF,PF] = deconvblind(Blurred,OVERPSF,30,[],WEIGHT,FUN);

imshow(JF)

title('Deblurred Image')

Output
Figure 7.3

Figure 7.4
Figure 7.5

Figure 7.6
Figure 7.7

b) CT Scan Brain image

Code

Code will be the same as above, we just need to change the file name in “imread” to whatever our
CT scan brain image name is.

Output

Figure 7.8
Figure 7.9

Figure 7.10
MRI Brain Image

Figure 7.11

Figure 7.12

Figure 7.13
Working

Read a grayscale image into the workspace. The deconvblind function can handle arrays of any
dimension. Simulate a real-life image that could be blurred (e.g., due to camera motion or lack of
focus). The example simulates the blur by convolving a Gaussian filter with the true image (using
imfilter). The Gaussian filter then represents a point-spread function, PSF. To illustrate the
importance of knowing the size of the true PSF, this example performs three restorations. Each time
the PSF reconstruction starts from a uniform array (an array of ones). The first restoration, J1 and P1,
uses an undersized array, UNDERPSF, for an initial guess of the PSF. The size of the UNDERPSF array
is 4 pixels shorter in each dimension than the true PSF. The second restoration, J2 and P2, uses an
array of ones, OVERPSF, for an initial PSF that is 4 pixels longer in each dimension than the true PSF.
The third restoration, J3 and P3, uses an array of ones, INITPSF, for an initial PSF that is exactly of the
same size as the true PSF. All three restorations also produce a PSF. The following pictures show how
the analysis of the reconstructed PSF might help in guessing the right size for the initial PSF. In the
true PSF, a Gaussian filter, the maximum values are at the center (white) and diminish at the borders
(black). The PSF reconstructed in the first restoration, P1, obviously does not fit into the constrained
size. It has a strong signal variation at the borders. The corresponding image, J1, does not show any
improved clarity vs. the blurred image, Blurred. The PSF reconstructed in the second restoration, P2,
becomes very smooth at the edges. This implies that the restoration can handle a PSF of a smaller
size. The corresponding image, J2, shows some deblurring but it is strongly corrupted by the ringing.
Finally, the PSF reconstructed in the third restoration, P3, is somewhat intermediate between P1 and
P2. The array, P3, resembles the true PSF very well. The corresponding image, J3, shows significant
improvement; however it is still corrupted by the ringing.

The ringing in the restored image, J3, occurs along the areas of sharp intensity contrast in the image
and along the image borders. This example shows how to reduce the ringing effect by specifying a
weighting function. The algorithm weights each pixel according to the WEIGHT array while restoring
the image and the PSF. In our example, we start by finding the "sharp" pixels using the edge
function. By trial and error, we determine that a desirable threshold level is 0.08. To widen the area,
we use imdilate and pass in a structuring element, se. The pixels close to the borders are also
assigned the value 0. The image is restored by calling deconvblind with the WEIGHT array and an
increased number of iterations (30). Almost all the ringing is suppressed. The example shows how
you can specify additional constraints on the PSF. The function, FUN, below returns a modified PSF
array which deconvblind uses for the next iteration.

In this example, FUN modifies the PSF by cropping it by P1 and P2 number of pixels in each
dimension, and then padding the array back to its original size with zeros. This operation does not
change the values in the center of the PSF, but effectively reduces the PSF size by 2*P1 and 2*P2
pixels. The anonymous function, FUN, is passed into deconvblind last. See the section Parameterizing
Functions, in the MATLAB Mathematics documentation, for information about providing additional
parameters to the function FUN.

In this example, the size of the initial PSF, OVERPSF, is 4 pixels larger than the true PSF. Setting P1 = 2
and P2 = 2 as parameters in FUN effectively makes the valuable space in OVERPSF the same size as
the true PSF. Therefore, the outcome, JF and PF, is similar to the result of deconvolution with the
right sized PSF and no FUN call, J and P, from step 4. If we had used the oversized initial PSF,
OVERPSF, without the constraining function, FUN, the resulting image would be similar to the
unsatisfactory result, J2, achieved in Step 3.
Image Rotation and Scale

a) Facial Image

Code

original = imread('2.jpg');

figure;

imshow(original)

title('Original Image')

scale = 0.5;

distorted = imresize(original,scale); % Try varying the scale factor.

theta = 90;

distorted = imrotate(distorted,theta); % Try varying the angle, theta.

figure;

imshow(distorted)

title('Manipulated Image')

movingPoints = [151.52 164.79; 131.40 79.04];

fixedPoints = [135.26 200.15; 170.30 79.30];

tform = fitgeotrans(movingPoints,fixedPoints,'nonreflectivesimilarity');

tformInv = invert(tform);

Tinv = tformInv.T;

ss = Tinv(2,1);

sc = Tinv(1,1);

scale_recovered = sqrt(ss*ss + sc*sc)

theta_recovered = atan2(ss,sc)*180/pi

Roriginal = imref2d(size(original));

recovered = imwarp(distorted,tform,'OutputView',Roriginal);

figure;

montage({original,recovered})

title('Image comparison')

Output
Figure 7.14

Figure 7.15
Figure 7.16

b) CT scan brain image

Code

Code will be the same as above, we just need to change the file name in “imread” to whatever our
CT scan brain image name is.

Output
Figure 7.17

Figure 7.18
Figure 7.19

MRI brain image

Figure 7.20
Figure 7.21

Figure 7.22

Working

Read an image into the workspace. Then Resize and Rotate the Image. Use the Control Point
Selection Tool to pick at least two pairs of control points. You can run the rest of the example with
these pre-picked points, but try picking your own points to see how the results vary.

cpselect(distorted,original,movingPoints,fixedPoints);

Save control points by choosing the File menu, then the Save Points to Workspace option. Save the
points, overwriting variables movingPoints and fixedPoints. Fit a nonreflective similarity
transformation to your control points. The geometric transformation, tform, contains a
transformation matrix in tform.T. Since you know that the transformation includes only rotation and
scaling, the math is relatively simple to recover the scale and angle.
Let sc=scale∗cos(theta)

Let ss = scale∗sin(theta)

Recover the original image by transforming distorted, the rotated-and-scaled image, using the
geometric transformation tform and what you know about the spatial referencing of original. The
'OutputView' name-value pair is used to specify the resolution and grid size of the resampled output
image. The recovered (right) image quality does not match the original (left) image because of the
distortion and recovery process. In particular, the image shrinking causes loss of information. The
artifacts around the edges are due to the limited accuracy of the transformation. If you were to pick
more points in Step 3: Select Control Points, the transformation would be more accurate.

You might also like