LAB 2

BINARY IMAGES AND OBJECT MOMENTS

Source Code

bullet

    moments.m

|| Lab2 || Lab3 || Lab4 || Lab5 || Lab6 || Lab7 || Lab8 ||
 Back to Main Menu

________________________________________________________________________________________________________________________

Aim of this lab:


The aim of this laboratory session is to introduce us to MATLAB and some of the image processing tools available from its Image Processing Toolbox. By the end of this session, we are be able to perform some simple binary analysis on an image.
________________________________________________________________________________________________________________________

First of all, we need to choose a colour image and convert it into a grey scaled image. Figure1 below is the color image 'lego1.png' and Figure2 is
the grey scale version for 'lego.png.

MATLAB Command:
im = imread('lego1.png');   %load image from directory
g = rgb2gray(im);          
%convert image to grey scale 


*click on picture for larger image
               
Figure1: The lego image 'lego1.png'                   Figure2: The grey scale image of 'lego1.png'      Figure3: The histogram of 'lego1.png'

We then choose a threshold value and convert the grey scale image into a binary image. The binary image with threshold value of 120 is shown in Figure4 and Figure5  is the binary image with threshold value of 150. The thresholded image will have the objects in black on a white background. All pixel that is above the threshold value are white and those below are black.

MATLAB Command:
bw = g > 120;  %threshold value = 120
bw = g > 150;


             
Figure4: Binary image with threshold value of 120    Figure5: Binary image with threshold value of 150

We want to get image where the objects are in white on a black background. So we negate the threshold image and then label the negated image with the bwlabel function. The negated image is shown in Figure6 and Figure7 is the negated image with labeled objects.

MATLAB Command:
bw = ~bw;                                %negate the binary image
bwl = bwlabel(bw);                      
%label the image
imagesc(bwl),colormap(gray),axis image  
%show labeled image


      
Figure6: Negation of binary image                         Figure7: Labeled image

The function bwlabel assigns unique values to the pixels in each distinct object in the binary image. Each pixel in the first object found in the binary image will be assigned a value of one, then each pixel in the second object found is given a value two and so forth. We have extracted 3 objects from the above binary image, which is object 3 (Figure8) , object 11 (Figure9) and object 20 (Figure10).

MATLAB Command:
ob3 = bwl == 3; %assign object 3
imshow(ob3);   
%show object 3

               
Figure8: Object 3                                             Figure9: Object 11                                        Figure10: Object 20

Calculating moments of the objects in the image

Below are the results for calculating moments for object 3 and object 20. The centroid of the objects are marked with red colour 'x' and the

MATLAB Command:
[centroid, theta, roundness] = moments(ob3);    %calculate the moments of object 3
[centroid, theta, roundness] = moments(ob20):  
%calculate the moments of object 20

Result:
                                             
                

Figure11: Moments of object 3                          Figure12: Moments of object 11                         Figure13: Moments of object 20
Centroid coordinate = 
(136.7832, 352.5988)   Centroid coordinate = (338.6261, 226.6513)    Centroid coordinate = (469.5004, 91.4323)      
Angle of axis of minimum inertia =
0.1347     Angle of axis of minimum inertia = -0.8432     Angle of axis of minimum inertia = 0.6296  
Roundness
= 0.8642                                         Roundness = 0.0742                                          Roundness = 0.1191

In order to test the correctness of code, we were asked to create a few synthetic test image, where we know the ground truth of the image. Figure14 is a 100x100 image with top left (10,30) and bottom right (50,60) and Figure15 is the moments of the object.

MATLAB Command:
test1 = zeros(100,100);                           %size 100x100 image
test1(30:50,10:60) = 1;
imshow(test1);                                   
%display image
[centroid, theta, roundness] = moments(test1);   
%calculate moments of image

       

Figure14: Test 1                                                         Figure15: Moments of Test 1                  
                                                                                   Centroid coordinate =
(35,40)        
                                                                                   Angle of axis of minimum inertia = 0  
  
                                                                                Roundness = 0.1692                                     

The following is a 200x200 test image with top left (50,20) and bottom right (150,100).

          
Figure16: Test 2                                                  Figure17: Moments of Test 1                  
                                                                            Centroid coordinate =
(75, 85)        
                                                                            Angle of axis of minimum inertia = 1.5708   
  
                                                                         Roundness = 0.1515   
 

Written by Geoffrey Liau
Last updated: 14th April 2005
liauc01@student.uwa.edu.au