function lab2new()

%im = imread('d:\comp vision\images\lego1.png');

%size(im)
a = rect(70, 100, 100, 100);

size(a)
% show(b);

% [centroid, thetamin, roundness] = moments([1])
% rad2deg(thetamin)

b = circ(350,170,100);
size(b)
c = rect(100, 320, 50, 200);

size(c)
d = rect(300, 420, 100, 0);

size(d)
g = a + b + c + d;
g = imrotate(g, 120);
%g = rgb2gray(im);

%imwritesc(g, 'greylego1.png')
%figure(1)
%imshow(g);

imshow(g)
pixval on;
% lego1 was chosen rather then lego2 becuase of non white parts, making it
% easier to threshhold

% 20 pieces

%BW = g > 160; 
%show(BW);

% the minimum threshold, with 20 = number of connected objects found

%BW = ~BW;   % ~ is MATLAB's logical negation operator

    hold;
[BWL, num] = bwlabel(g);   % BWL contains the labeled image

for i=1:num
    ob = BWL==i;
    [centroid, thetamin, roundness] = moments(ob);
    centroid
    rad2deg(thetamin)
    roundness
    area = sum(sum(ob));
    meanx = centroid(1);
    meany = centroid(2);
    
    % 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);

    line([X1 + meanx, X2 + meanx],[Y1 + meany, Y2 + meany], 'LineWidth',2)
    plot(meanx, meany,'r+')

end

%imwritesc(BWL, 'bwlabellego1.jpg')
%imagesc(BWL), colormap(gray), axis image  % You also typically need to
                                            % specify a colormap to use
                                            % and the axis display.

  %show(BWL)       % The much simpler alternative!
%     figure(1);                 % Set the current figure to 'Figure 2'
%     print -dpng momentstest2.png     % Saves current figure as a postscript file
hold
function deg = rad2deg(rad)
    deg = rad*180/pi;
end

function rectim = rect(xpos, ypos, length, width)
    rectim = zeros(480, 640);
    rectim(xpos:xpos + length, ypos:ypos + width) = 1;
end
    
function circim = circ(xpos, ypos, radii)
    circim = zeros(480, 640);
    circim(xpos - radii: xpos + radii, ypos - radii: ypos + radii) = circularstruct(radii);
end