Laboratory 2

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

 

Separating and Labeling Binary Objects

Original lego image (im)
lego1.jpg was chosen, since there are no white pieces. total of 20 pieces

g = rgb2gray(im);

 

BW = g > 160
160 is the minimum value, that the number of connected objects found equaled 20.

bwlabel(~BW)
Actually there was 21 objects, due to a black spec.

 

Final output using moments on all objects,

 

Synthetic Test Image (clockwise from top right)
square: sides 100, starting at location 100, 70
centroid =150 120
thetamin = 292.7399
roundness =1

rectangle: 200 x 50, starting at location 320, 100
centroid = 420 125
thetamin = 0
roundness =0.0644

line: length 100, starting at location 420, 300
centroid =420 350
thetamin = 90
roundness =0

circle: radius 100, center at location 170, 350
centroid = 170 350
thetamin = 20.8409
roundness = 1

 

image rotated 45 degrees

square:
centroid = 191.5000 432.5000
thetamin = 97.9877
roundness = 1

rectangle:
centroid = 385.5000 245.0000
thetamin = -44.9977
roundness =0.0643

line:
centroid = 545 404
thetamin = 45
roundness =0

circle:
centroid =368.0116 580.8682
thetamin = 20.0264
roundness =0.9997

Note: the circle and square have arbitary values

image rotated 120 degrees

square:
centroid = 349.8913 606.9094
thetamin = -18.8918
roundness = 0.9999

rectangle:
centroid = 219.2282 370.5797
thetamin = 59.9985
roundness =0.0644

line:
centroid = 414.2300 258.0000
thetamin = -29.9952
roundness =9.8486e-005

circle:
centroid = 539.0779 474.5787
thetamin = 9.1088
roundness = 0.9993

Note: the circle and square have arbitary values

Lisiting of moments.m

% MOMENTS
%
% Function calculates the moments of a binary image and returns 
% the centroid, the angle of axis of minimum inertia, and a measure 
% of 'roundness'.  The function assumes that there is only one object
% in the binary image.
%
% function [centroid, thetamin, roundness] = moments(im)
%
% Argument:  im   - a binary image containing values of 0 or 1
%
% Returns:   centroid  - a 2 element vector
%            thetamin  - the angle of axis of minimum inertia (radians)
%            roundness - ratio of minimum inertia/maximum inertia.
%
% Note that positive x is to the right and positive y is downwards
% thus angles are positive clockwise.
%
% The function also displays the image and overlays the position of
% the centroid and the axis of minimum inertia.

 function [centroid, thetamin, roundness] = moments(im)
 
    [rows,cols] = size(im);
    x = ones(rows,1)*[1:cols];    % Matrix with each pixel set to its x coordinate
    y = [1:rows]'*ones(1,cols);   % Matrix with each pixel set to its y coordinate

    area = sum(sum(im));
    meanx = sum(sum(double(im).*x))/area;
    meany = sum(sum(double(im).*y))/area;
    centroid = [meanx, meany];
    
    % coordinates changed with respect to centre of mass
    x = x - meanx;
    y = y - meany;
    
    a = sum(sum(double(im).*x.^2));
    b = sum(sum(double(im).*x.*y))*2;
    c = sum(sum(double(im).*y.^2));
    
    denom = b^2 + (a-c)^2;
    
    if denom == 0
        % let thetas equal arbitrary angles
        thetamin = 2*pi*rand;
        thetamax = 2*pi*rand;
        roundness = 1;
    else
        sin2thetamin = b/sqrt(denom);       %positive solution
        sin2thetamax = -sin2thetamin;
        cos2thetamin = (a-c)/sqrt(denom);   %positive solution
        cos2thetamax = -cos2thetamin;
    
        thetamin = atan2(sin2thetamin, cos2thetamin)/2;
        thetamax = atan2(sin2thetamax, cos2thetamax)/2;
        Imin = 0.5*(c+a) - 0.5*(a-c)*cos2thetamin - 0.5*b*sin2thetamin;
        Imax = 0.5*(c+a) - 0.5*(a-c)*cos2thetamax - 0.5*b*sin2thetamax;
        roundness = Imin/Imax;
    end
    
    % draw an axis proportional to object size
    % 0.5 takes into acount lines with roundness = 0
    % 5   takes into acount small objects, so axis is still visible.
    rho = sqrt(area)/(roundness + 0.5) + 5 ; 
    [X1,Y1] = pol2cart(thetamin,      rho);
    [X2,Y2] = pol2cart(thetamin + pi, rho);

    imshow(im);
    hold;
    line([X1 + meanx, X2 + meanx],[Y1 + meany, Y2 + meany])
    plot(meanx, meany,'r+')
    hold;