Lets Learn together... Happy Reading

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

Recent Posts

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:


1.     Read input image
·        Read a RGB image with ball in mid air or ball in palm.
                   MATLAB CODE:
                       %Read the input image
         Img=imread(Filename);
         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
                                 % and adjusting
                                 % 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:

        Rad=(sdata(ele).EquivDiameter)/2;
        Rad=strcat('Radius of the Ball :',num2str(Rad));
     
        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 button Like "IMAGE PROCESSING" page

Text On Sphere - MATLAB CODE


Text on Sphere-MATLAB CODE

              We can see how to wrap a text around a sphere. First obtain the x,y and z co-ordinates of  the sphere.These co-ordinates can be used as an input for surface object.
Surface object generates sphere using the co-ordinates.  Example:[x,y,z]=sphere(25); surface(x,y,z);
Now read a text image and convert it into binary.  Label the components in the image. You can also check how to label components without BWLABELfunction in MATLAB.   Now use the labeled image as a CDATA value in surface object.
Example: surface(x,y,z, ‘CDATA’,matrix);

MATLAB CODE:


%Input a text image
A=imread('quotes.png');


         


A=~im2bw(A);
B=bwlabel(A,8);
F=double(B);
mytext=flipud(F);


% Create the surface.
[x,y,z] = sphere(100);


figure,surface(x,y,z,'CData',mytext,'FaceColor','texturemap','FaceLighting','phong','EdgeLighting','phong','EdgeColor','none');




%set the colormap. Some built-in colormaps are pink, copper, jet, hot...
%colormap pink

%Here I created my own colormap, the colors are white[1 1 1], black[0 0 0] and %blue [0 0 1]. Initialize the map with zeros(black) for 256x3 matrix. %Add blue 
%color to some components(words). Initialize the first row with [ 1 1 1](white).





map=zeros([253 3]);
map(1,:)=0;
map(50:80,3)=1;
colormap(map);



%Create light object
light('position',[1 2 0 ],'Style','infinite','color',[0.8 0.7 0.8]);
light('position',[-2 -3 0 ],'color',[0.8 0.7 0.8]);


%Specify the viewpoint
axis square off
view(12,0);










Another Example: To create a GLOBE from a WORLD MAP



INPUT IMAGE















To convert a RGB image into SPHERE:
MATLAB CODE:




A=imread('mountain.jpg');
[mytext,map]=rgb2ind(A,256);
mytext=flipud(mytext);

% Create the surface.
[x,y,z] = sphere(100);

figure,surface(x,y,z,'CData',mytext,'FaceColor','texturemap','FaceLighting','phong','EdgeLighting','phong','EdgeColor','none');
%set the colormap. 

colormap(map);


%Create light object
light('position',[1 2 0 ],'Style','infinite','color',[0.8 0.7 0.8]);
light('position',[-2 -3 0 ],'color',[0.8 0.7 0.8]);


%Specify the viewpoint
axis square off
%view(12,0);
view(180,0);










Hope you enjoyed this post. Check the link for more images:http://www.facebook.com/media/set/?set=a.352096201499476.75050.241768945865536&type=1

like button Like "IMAGE PROCESSING" page

Circle

      Did you come here by searching for ‘How to draw a circle in MATLAB’ then definitely at the end of the article you will draw a circle. 
 Let me first discuss how to draw a circle and then we will see about concentric circles.
The equation of the circle: (x-x1).^2 +(y-y1).^2=r^2.
where x and y are the centre of the circle and r is the radius.
Here I am not using any plot function, just the equation of the circle.

Initialize the image with 255 and find the centre of the image. The variable ‘sz’ changes the size of the image. The image is a square matrix. Then ‘rad’ contains the radius of the circle.
sz=300;
rad=100;
clear RGB
RGB(1:sz,1:sz,1:3)=255;


 I am storing the x co-ordinates in x and y co-ordinates in y.

[x y]= find(RGB==255);

‘xc’ and ‘yc’ contains the midpoint of the circle.


xc=ceil((sz+1)/2);
yc=ceil((sz+1)/2);

r=rad.^2;

 Here I am finding the points which satisfy the equation. To avoid for loop I am using the ‘find’ function.

d=find(((x-xc).^2+(y-yc).^2) <= r);

The points that are stored in d contains the index value for x and y that satisfies the equation.
        for i=1:size(d,1)
      
         RGB(x(d(i)),y(d(i)),:)=0;
      
        end
Before edge detection
           
       
   The rest of the procedure is same. I found the edge and I strengthen the edge by dilating.

B=rgb2gray(RGB);


ED=edge(B);
SE=strel('disk',1);
cir=~imdilate(ED,SE);
figure,imshow(cir);



Concentric circles:

The procedure is same but I use loop to produce ‘n’ no.of circles.
for i=1:8
    radius=(rad-10*i).^2;
    r=find(((x-xc).^2+(y-yc).^2)<=radius);
   
for j=1:size(r,1)

    if(mod(i,2)==0)
                RGB(x(r(j)),y(r(j)),:)=255;
    end
            if(mod(i,3)==1)
               RGB(x(r(j)),y(r(j)),:)=0;
            end
        
end
end

Before edge detection

final image


If you find any difficulty in the code, Mail me, I will send the code.
like button Like "IMAGE PROCESSING" page
Next Post Home
Related Posts Plugin for WordPress, Blogger...
Twitter Bird Gadget Google ping Hypersmash.com