# IMAGE PROCESSING

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

### TABLE TENNIS BALL DETECTION-MATLAB CODE:

Using the concept of roundness to detect a circular object, the table tennis ball in mid air or in palm is detected.

FLOW CHART:

·        Read a RGB image with ball in mid air or ball in palm.
MATLAB CODE:
axes('Position',[0 .1 .74 .8],'xtick',[],'ytick',[]);
imshow(Img);title('Original Image');

2.     Image Pre-processing
·        Convert the RGB image to grayscale image.
·        Apply median filter
·        Adjust the brightness and contrast of the image using ‘imadjust’ function.
MATLAB CODE:
I=rgb2gray(Img);     % Converting RGB Image to
% Gray Scale Image
I=im2double(I);      % Converting Gray scale Image
% to Double type

J = medfilt2(I,[3 3]); % Median Filter ,
% 3x3 Convolution
% on Image
I2 = imadjust(J);     % Improve to quality of Image
% contrast and brightness values

3.     Threshold the image
·        The pixel value greater than the threshold value is converted to one else zero.
MATLAB CODE:
Ib = I2> 0.9627;

4.     Image Labeling
·        Label the connected components using ‘bwlabel’ function
·        Remove components that are smaller in size.
MATLAB CODE:
%Labelling
[Label,total] = bwlabel(Ib,4); % Indexing segments by
% binary label function

%Remove components that is small and tiny
for i=1:total
if(sum(sum(Label==i)) < 500 )

Label(Label==i)=0;

end
end

5.     Find the image properties: Area, Perimeter and Centroid
·        Using ‘regionprops’ function, find the Area, Perimeter, Bounding Box and Centroid.
MATLAB CODE:
%Find the properties of the image
Sdata = regionprops(Label,'all');

6.     Calculate the Roundness
·        Roundness = 4*PI*A/P^2
MATLAB CODE:
%Find the components number
Un=unique(Label);
my_max=0.0;

%Check the Roundness metrics
%Roundness=4*PI*Area/Perimeter.^2
for i=2:numel(Un)
Roundness=(4*pi*Sdata(Un(i)).Area)/Sdata(Un(i)).Perimeter.^2;
my_max=max(my_max,Roundness);
if(Roundness==my_max)
ele=Un(i);
end
end

7.     Find the component with the maximum roundness value
·        Find the max of the Roundness value for all the labeled components
8.     Show the detected table tennis ball
·        Use the ‘BoundingBox’ values to plot rectangle around the ball
·        Mark the centroid of the ball
MATLAB CODE:
%Draw the box around the ball
box=Sdata(ele).BoundingBox;
box(1,1:2)=box(1,1:2)-15;
box(1,3:4)=box(1,3)+25;

%Crop the image
C=imcrop(Img,box);

%Find the centroid
cen=Sdata(ele).Centroid;

%Display the image

axes('Position',[0 .1 .74 .8],'xtick',[],'ytick',[])
imshow(Img);
hold on
plot(cen(1,1),cen(1,2),'rx');%Mark the centroid

9.     Generate report
·        Find the radius using the Equidiameter obtained using ‘regionprops’ function.
·        Display the radius,Area,Perimeter and Centroid of the ball.
·        Show the Binary and Original image of the cropped ball.
MATLAB CODE:

Area=sdata(ele).Area;
Area=strcat('Area of the ball:',num2str(Area));

Pmt=sdata(ele).Perimeter;
Pmt=strcat('Perimeter of the ball:',num2str(Pmt));

Cen=sdata(ele).Centroid;
Cent=strcat('Centroid:',num2str(Cen(1,1)),',',num2str(Cen(1,2)));

BALL IN MID AIR:

Like "IMAGE PROCESSING" page

Arjun Nichal said...

I really like your blog.....its awesome....
Keep blogging.....

Subhankar Paul said...

Really very useful.. thanks for sharing..

Subhankar Paul said...

Really very useful & thanks for sharing...

saila said...

I just saw this blog yesterday, it is really very informative for beginner. If there is also some examples for recognition and classification. It would be very useful for learners.

saila said...

It is very informative for beginner. If there is also some examples for recognition and classification then it would also very useful. Thank for sharing.

Galaxy Winners said...

It is very useful because we are doing a sky observation project based on image processing using MATLAB.... I agree with saila it may be very helpful to us that if you present some examples on recognition

Ameya Kulkarni said...

Hey..very informative code..but when I try it the mark for the ball isn't at the proper place..it's outside the image..Can it be because I don't have the original image?

mayur kagathara said...

WOW dude.. thank u very much