[img,map] = imread('project/l1.gif','gif');
%[edgemap,tetamap] = prewitt('l2.gif',30);
%imview(edgemap);
colormap(gray);

%imview(tetamap);
size(map);

%for each map entry mark 1 if possible yellow.

yellow=zeros(size(map,1),1);
for i=1:size(yellow,1)
    r = map(i,1);
    g = map(i,2);
    b = map(i,3);
    if(abs(r-g)*255<70 && g*255>100 && map(i,3)<=max([r,g])*0.5)
        map(i,1:3)=1;
        yellow(i)=1;
    else
        map(i,1:3)=0;                
    end
end
img = ind2gray(img,map);

imwrite(img,'yplate.jpg','jpg');
colormap(gray);
%img = edge(img,'prewitt',0.3);
res = bwlabel(img);
regions = regionprops(res,'boundingbox','area');

for i=1:size(regions,1)
%for i=1:
    region = regions(i).BoundingBox;
    regionsize = regions(i).Area;
    topx=region(1);
    topy=region(2);
    wx=region(3);
    wy=region(4);
    %ignore small regions less than 7*20 by 20 (7 digits atleast 20pixels
    %each)
    if(regionsize>2800)
        plate ='';
        numbers = [];
        test = img(ceil(topy):ceil(topy)+floor(wy)-1,ceil(topx):ceil(topx)+floor(wx)-1);
        [edgemap,tetamap] = prewitt(test,30);
        [numbers,names] = findNumbers(test)
        numbers = sortrows(numbers,1);
        for j=1:size(numbers,1)
            plate = strcat(plate,names{numbers(j,2)});
        end
        fprintf('plate: %s',plate);
        %imview(test);    
    end
end
