clc;
clear;
img=imread('34.jpg');
[sr sc sh]=size(img);
img_bw=imbinarize(rgb2gray(img));
imshow(img_bw);
reg=regionprops(img_bw,'BoundingBox');
reg=cat(1,reg.BoundingBox);
[r,c,h]=size(reg);

for i=1:r
    
    %check if region size is greater than 10% of total image size
    area=reg(i,3)*reg(i,4);
    if(area<0.001*sr*sc)
        continue;
    end
    
    imshow(img);
    hold on
    rectangle('Position',reg(i,:),'EdgeColor','r','LineWidth',2);
    sx=round(reg(i,1));
    sy=round(reg(i,2));
    cr=0;
    cg=0;
    cb=0;
    absol=abs(reg(i,3)-reg(i,4));
    mini=min([reg(i,3),reg(i,4)]);
    if(absol>0.35*mini)
        fprintf('Region %d does not satisfy the length width constraint\n',i);
        hold off;
       input('','s');
        continue;
    end
    
    for k=sx:(sx+reg(i,3))
        for j=sy:(sy+reg(i,4))
            if(k>sc || j>sr)
                break;
            end
            if(max(img(j,k,:))==img(j,k,1) && img(j,k,1)>=150 && img(j,k,2)<=150 && img(j,k,3)<=150)
                cr=cr+1;
            elseif(max(img(j,k,:))==img(j,k,2) && img(j,k,2)>=150 && img(j,k,1)<=150 && img(j,k,3)<=150)
                cg=cg+1;
            elseif(max(img(j,k,:))==img(j,k,3) && img(j,k,3)>=150 && img(j,k,1)<=150 && img(j,k,2)<=150)
                cb=cb+1;
            
            end
            
        end
    end
    m=max([cr cg cb]);
    if(m==cr && m>=(0.15*reg(i,3)*reg(i,4)))
        fprintf('Red is max in bounding box=%d and count=%d percent\n',i,uint8(m/(reg(i,3)*reg(i,4))*100));
    elseif(m==cg && m>=(0.15*reg(i,3)*reg(i,4)))
        fprintf('Green is max in bounding box=%d and count=%d percent\n',i,uint8(m/(reg(i,3)*reg(i,4))*100));
    elseif(m==cb && m>=(0.15*reg(i,3)*reg(i,4)))
        fprintf('Blue is max in bounding box=%d and count=%d percent\n',i,uint8(m/(reg(i,3)*reg(i,4))*100));
    else
        hold off
        continue;
    end
    input('','s');
    hold off
end
