Uploaded by Gul Haider

Project Coding (MATLAB)

ECP-LAB
PROJECT:
MEMBERS
:
PERFORMED BY :
SEMESTER
:
SUBMITTED TO :
Process of Project:
1) Import Image:
First of all, open MATLAB and
go to application section. Here you’ll find
Image Processing Toolbox, download it if you
haven’t downloaded it. Then create a new
script file, and type the code as in the coding
below. It is important that both the image and
.m file lie in the same directory. The command
imread reads an image and convert it into “3
dimensional matrix” in the RGB color space.
I wrote the name of my image as “Hard
ball.jpg” which is a 241 * 250 pixel image.
The imread function converted it into a matrix
of 250*241*3 (Rows*column*RGB). The
RGB corresponds to red, green and blue
intensity level. Then use imshow to view the
produced image.
2) Segment Image:
Now we should segment the
image into a binary image to differentiate
backgrounds from the desired image. The first
step is to divide the image into three images
based on the intensities of red, blue and green
components of image. This is the color based
image segmentation. As we see that blue plane
is the best choice for Thresholding. Set the
increment value to 0.01 and choose the best
value at which to threshold. I set this value to
0.44.
3) Segmentation continued (Remove Noise):
We need to
clean the image up significantly to improve the
accuracy of our diameter measurement. So i
wrote a code with the help of internet to clean
up the image and provide the more uniform
blob to analyze. Blobs in this document are
any collection of white pixels that touch to
create a cohesive and distinct object.
4) Measuring Image:
To measure an image, we’ll
select a best blob from thresholded, which
represents the ball in the original image. So,
now we have the original image in the binary
image form and this will make it easy for other
functions in MATLAB to quickly analyze the
region and a host of different information. The
regionprops function is the tool that will
provide the MajorAxisLength of the blob in
the image. As you can see, by not suppressing
the line 45 with the semi-colon, the diameter is
displayed in the Command Window.
5) Result:
The result is displayed in the final figure
and MajorAxisLength is displayed in the
Command Window.
Coding:
%% Import Image
clear;
clc;
obj = imread('Hard ball.jpg');
imshow(obj)
%% Segment Image
%Divide image "obj" into its respective RGB intensities
red = obj(:,:,1);
green = obj(:,:,2);
blue = obj(:,:,3);
figure(1)
subplot(2,2,1);
subplot(2,2,2);
subplot(2,2,3);
subplot(2,2,4);
imshow(obj); title('Original Image');
imshow(red); title('Red Plane');
imshow(green); title('Green PLane');
imshow(blue); title('Blue Plane');
%Threshold the blue plane
figure(2)
level=0.44;
bw2=im2bw(blue, level);
subplot(2,2,1); imshow(bw2); title('Blue Plane
Thresholded');
%%Remove Noise
%Fill the holes
fill = imfill(bw2,'holes');
subplot(2,2,2); imshow(fill); title('Holes filled');
%Remove any blobs on the border of the image
clear = imclearborder(fill);
subplot(2,2,3); imshow(clear); title('Remove blobs on
border');
%Remove blobs that are smaller than 9 pixels
q = strel('disk',9);
open = imopen(fill,q);
subplot(2,2,4); imshow(open); title('Remove small
blobs');
%% Measuring Object Diameter
diameter = regionprops(open,'MajorAxisLength')
%Show result
figure(3)
imshow(obj)
d = imdistline; %Include a line to physically measure a
ball
NOTE:
Respected Ma’am, This is a plotting
project so, we took help from internet and some
books to derive its coding. I used some built-in
functions to draw image. But we know little
about their use. This is all about to determine
diameter within an image. Therefore, we used
some built-in functions. I performed this project
by my own. Other members have no MATLAB.
But they know about my coding. So, I’m giving
the pictures of my practice on MATLAB.