% ==================================================================================================
% Module:   findObjects.m
%
% Usage:    Isolate and analyize objects in a binary image.
%
% Purpose: 
%     Isolates objects in a binary images and 'labels' them by color. It
%     also outputs a plot of the pecstrum of each object found.
%
%     
% Input Variables:
%     filename          Filename of the binary image to seek images.
%     numberOfObjects   Number of objects in the image
%     
% Returned Results:
%     A formatted image with all the objects 'labeled' and displays plots
%     of each objects pecstrum.
%     
% Restrictions/Notes:
%     This function is geared for binary images.
%     
% See Also:
% 
% References:
%	setdifference, intersection, area, dilation, erosion, erode
% 
% Author:       Isaac Gerg
% Date:         2/16/04
% Revisions:    none
% ==================================================================================================


function O = findObjects(filename, numberOfObjects);
%filename = 'images/TheThreeShapes2.gif'
%numberOfObjects = 9;
% Intialization
I = imread(filename);
B = ones(3,3);
[m,n] = size(I);
O = ones(size(I));
objectNumber = 0;

COLOR = 0;

% Start finding objects in image I.
for x=1:m
    for y = 1:n
        if I(x,y) == 1;
	    objectNumber = objectNumber + 1;
            disp('Extracting object....');
            L = zeros(size(I));
            COLOR = COLOR + (1/(numberOfObjects+1));
            P = [x y];
            L(P(1), P(2)) = 1;

            % Find an object and store it in V.
            test = 1;
            i = 0;
            oldV = zeros(size(I));
            while test,
                disp(i);
                L = dilation(L,B);
                L = L(2:(m+1), 2:(n+1));
                V = intersection(I,L);
                if setdifference(oldV,V) == zeros(m,n)
                    disp('Algorithm converged.');
                    test = 0;
                end
                L = V;
                oldV=V;
                i = i+1;
            end

            % Find all pixels (1) that are in V and put thier coordinates in P.
            % Find the bounding box.
            % Output the object in O.
            disp('Computing bounding box.');
            left = n;
            right = 0;
            top = n;
			bottom = 0;
			object = zeros(m,n);
			for x=1:m
                for y = 1:n
                    if V(x,y) == 1
                        O(x,y) = COLOR;
                        I(x,y) = 0;
                        %P = [P; [x y]];
                        if y < left
                            left = y;
                        end
                        if y > right
                            right = y;
                        end
                        if x < top 
                            top = x;
                        end
                        if x > bottom
                            bottom = x;                
                        end
                        object(x,y) = 1;
                    end
                end
			end 
            % Print bounding box on object
            for x=top:bottom
                O(x, left) = COLOR;
                O(x, right) = COLOR;
            end
            for y=left:right
                O(top, y) = COLOR;
                O(bottom, y) = COLOR;
            end
            object = object(top:bottom, left:right); 

            % Compute the pecstrcum of the image using a 3x3 square
            % structuring element, B.
            pecstrum = [];
            for g=1:20
                seA = B;    % g dilated strel
                seB = B;    % g+1 dilated strel
                % Create our structuring elements.
                for f=1:g
                    seA = dilation(seA, B);
                end
                for f=1:g+1
                    seB = dilation(seB,B);
                end
                term1 = dilation(erosion(object, seA), seA);
                term2 = dilation(erosion(object, seB), seB);
                pecstrum = [pecstrum (area(term1)-area(term2)) / area(object)]; 

            end
	    H = plot(pecstrum);
	    xlabel = 'n';
	    ylabel = 'f(n)';
	    title(sprintf('Pecstrum of Object Number: %d', objectNumber));      
	    imwrite(object, sprintf('object_%d.png', objectNumber), 'png');
	    saveas(H, sprintf('object_%d_pectsrum.png', objectNumber), 'png');

        end     % End if
    end % End for
end     % End for
disp('Done.');
O = O./max(max(O));