%[img,map] = imread('project/l2.gif','gif');
%img = ind2gray(img,map);
%colormap(gray);
%[edgeimg,t] = edge(img,'canny',0.5);
%imview(img);
%size(img)
function[numbers,res] =  findNumbers(img)
numbers =[];
res = {};
global tetamap;
global edgemap;
[edgemap,tetamap] = prewitt(img,30);
[height,width] = size(img)
%edgemap=edgeimg;

edgemap = padarray(edgemap,[3,3]);
tetamap = padarray(tetamap,[3,3]);
tracemap = zeros(size(edgemap));
colormap(gray);
%imview(edgemap);
%imview(tetamap);
rowindex = 0;
invimg = edgemap';
traced=false;
rowindex = 3
rowend = size(edgemap,1)-2;
startindex =3;
indexend = size(edgemap,2)-2;
while (rowindex <rowend)    
    %row = invimg(:,rowindex);    
    row = edgemap(rowindex,:);    
    row=row';
    index=startindex;
    while (index<indexend)         
        if(row(index)==1)                 
                %fprintf('tracing: line %d row %d\n',rowindex,index);
                   %if(index>65)
                    [xend,yend,xmin,ymin,shapesize,allpoints,trace] = mtrace(index,rowindex);               
                   %end
                   %resample row from im
                   tracemap = tracemap|trace;
                  row = edgemap(rowindex,:);    
                  row=row';
                if(shapesize>2*width)
                    
                    %indexend = xend;
                    %rowend = yend;
                    %fprintf('big');
                    %imview(edgemap);
                elseif(shapesize>80)
                     shapeSpline = spline(linspace(0,1,size(allpoints,1)),allpoints(:,2));
                    features = round(ppval(shapeSpline,linspace(0,1,100)));
                    [score,num1,score2,num2] = bestMatch(features');
                    if(score<700)
                        %imview(trace);
                        res(size(res)+1)={num1};
                        numbers(size(numbers,1)+1,:) = [xmin,size(numbers,1)+1];
                    end
                    edgemap(ymin:yend,xmin:xend)=0;
                    %imview(edgemap);
                    %startindex = xend;
                    %index=xend;
                else
                    edgemap(ymin:yend,xmin:xend)=0;
                    %imview(edgemap);
                end
        end            
        index=index+1;          
    end
        rowindex=rowindex+1;
end



imview(tracemap);
end

        
        

        
