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

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

Min Filter - MATLAB CODE

MIN FILTER
  •      To find the darkest points in an image.
  •       Finds the minimum value in the area encompassed by the filter.
  •       Reduces the salt noise as a result of the min operation.
  •       The 0th percentile filter is min filter.

MATLAB CODE:

%READ AN IMAGE
A = imread('board.tif');
A = rgb2gray(A(1:300,1:300,:));
figure,imshow(A),title('ORIGINAL IMAGE');

%PREALLOCATE THE OUTPUT MATRIX
B=zeros(size(A));

%PAD THE MATRIX A WITH ZEROS
modifyA=padarray(A,[1 1]);

        x=[1:3]';
        y=[1:3]';
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
      
       %VECTORIZED METHOD 
       window=reshape(modifyA(i+x-1,j+y-1),[],1);

       %FIND THE MINIMUM VALUE IN THE SELECTED WINDOW
        B(i,j)=min(window);

    end
end

%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MIN FILTERING');












like button Like "IMAGE PROCESSING" page

Max Filter - MATLAB CODE

  • To find the brightest points in an image.
  •  Finds the maximum value in the area encompassed by the filter.
  •  Reduces the pepper noise as a result of the max operation.
  •  The 100th percentile filter is max filter. Check the 50th percentile filter i.e the median filter.



MATLAB CODE:

%READ AN IMAGE
A = imread('board.tif');
A = rgb2gray(A(1:300,1:300,:));
figure,imshow(A),title('ORIGINAL IMAGE');

%PREALLOCATE THE OUTPUT MATRIX
B=zeros(size(A));

%PAD THE MATRIX A WITH ZEROS
modifyA=padarray(A,[1 1]);

        x=[1:3]';
        y=[1:3]';
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
      
       %VECTORIZED METHOD
       window=reshape(modifyA(i+x-1,j+y-1),[],1);

       %FIND THE MAXIMUM VALUE IN THE SELECTED WINDOW
        
       B(i,j)=max(window);
   
    end
end

%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
figure,imshow(B),title('IMAGE AFTER MAX FILTERING');














like button Like "IMAGE PROCESSING" page

2-D Inverse Discrete Cosine Transform


Consider the result obtained after DCT. (Check 2d-DCT )
Apply Inverse Discrete Cosine Transform to obtain the original Image.




MATLAB CODE:

%2-D INVERSE DISCRETE COSINE TRANSFORM
%PREALLOCATE THE MATRIX
A=zeros(size(B));
Temp=zeros(size(B));
[M N]=size(B);

x=1:M;
x=repmat(x',1,N);
y=repmat(1:N,M,1);

figure,
imshow(log(abs(B)),[]);colormap(jet);title('After DCT');







for i=1:M
    for j = 1: N
   
        if(i==1)
          AlphaP=sqrt(1/M);
        else
          AlphaP=sqrt(2/M);
        end
       
        if(j==1)
          AlphaQ=sqrt(1/N);
        else
          AlphaQ=sqrt(2/N);
        end
                 
        cs1=cos((pi*(2*x-1)*(i-1))/(2*M));
        cs2=cos((pi*(2*y-1)*(j-1))/(2*N));
        Temp=B.*cs1.*cs2*AlphaP*AlphaQ;
              
          A(i,j)=sum(sum(Temp));
    end
end
%OUTPUT
figure,
imshow(abs(A),[0 255]);title('Image after IDCT');












like button Like "IMAGE PROCESSING" page

2-D Discrete Cosine Transform


DCT is a technique for converting a signal into elementary frequency components.
It is widely used in image compression. Check Inverse discrete cosine transform for the reverse process.





MATLAB CODE:

%CONSIDER A MATRIX
A=[4 2 9 60; 7 10 5 77;88 66 44 3];
%PREALLOCATE THE MATRICES
B=zeros(size(A));
Temp=zeros(size(A));
display(A);






%FIND THE SIZE OF THE MATRIX
[M N]=size(A);
for i=1:M
    for j=1:N
       
        if(i==1)
          AlphaP=sqrt(1/M);
        else
          AlphaP=sqrt(2/M);
        end
       
        if(j==1)
          AlphaQ=sqrt(1/N);
        else
          AlphaQ=sqrt(2/N);
        end


For A(1,1),  m=1 and n=1. If m= 1 then AlphaP=sqrt(1/M)=0.5774
If n=1 then AlphaQ=sqrt(1/N)=0.5000


For A(2,3),m=1 and n=3. AlphaP=sqrt(2/M)=0.8165
 AlphaQ =  sqrt(2/N) = 0.7071









Temp=0;
       for x=1:M
            for y=1:N
                   cs1=cos((pi*(2*x-1)*(i-1))/(2*M));
                cs2=cos((pi*(2*y-1)*(j-1))/(2*N));
                Temp=Temp+(A(x,y)*cs1*cs2);
             end
        end
       B(i,j)=AlphaP*AlphaQ*Temp;
    end
end
%2-D discrete Cosine Transform
display(B);






EXAMPLE 2



%2-D DISCRETE COSINE TRANSFORM FOR AN IMAGE


%READ THE IMAGE
I=imread('coins.png');
I=I(90:150,140:210);



A=double(I);

%PREALLOCATE THE MATRIX
B=zeros(size(A));
Temp=zeros(size(A));
[M N]=size(A);


%
x=1:M;
x=repmat(x',1,N);
y=repmat(1:N,M,1);


for i=1:M
    for j = 1: N
   
        if(i==1)
          AlphaP=sqrt(1/M);
        else
          AlphaP=sqrt(2/M);
        end
       
        if(j==1)
          AlphaQ=sqrt(1/N);
        else
          AlphaQ=sqrt(2/N);
        end
       
             
               
        cs1=cos((pi*(2*x-1)*(i-1))/(2*M));
        cs2=cos((pi*(2*y-1)*(j-1))/(2*N));
        Temp=A.*cs1.*cs2;
              
      
        B(i,j)=AlphaP*AlphaQ*sum(sum(Temp));
    end
end

%OUTPUT
figure,
subplot(2,1,1);imshow(I);title('Original Image');
subplot(2,1,2),imshow(log(abs(B)),[]);
colormap(jet);title('After DCT');





















like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com