clear;
mark1=imread('1_mark.bmp','bmp');
mark2=imread('2_mark.bmp','bmp');
mark3=imread('3_mark.bmp','bmp');
pic1=double(imread('1.jpg','jpg'));
pic2=double(imread('2.jpg','jpg'));
pic3=double(imread('3.jpg','jpg'));
[m,n]=size(mark1);
k=1;
for i=1:m
    for j=1:n
        if mark1(i,j)==255 %it is plate background
            r(k)=pic1(i,j,1); %get the R of RGB color
            g(k)=pic1(i,j,2); %get the G of RGB color
            b(k)=pic1(i,j,3); %get the B of RGB color
            k=k+1;
        end
%         if mark2(i,j)==255 %it is plate background
%             r(k)=pic2(i,j,1); %get the R of RGB color
%             g(k)=pic2(i,j,2); %get the G of RGB color
%             b(k)=pic2(i,j,3); %get the B of RGB color
%             k=k+1;
%         end
        if mark3(i,j)==255 %it is plate background
            r(k)=pic3(i,j,1); %get the R of RGB color
            g(k)=pic3(i,j,2); %get the G of RGB color
            b(k)=pic3(i,j,3); %get the B of RGB color
            k=k+1;
        end
    end
end
num=length(r);
sumr=0; sumg=0; sumb=0;
for i=1:num
    sumr=sumr+r(i);
    sumg=sumg+g(i);
    sumb=sumb+b(i);
end
meanr=sumr/num;
meang=sumg/num;
meanb=sumb/num;
c=zeros(3,3);
for i=1:num
    c(1,1)=c(1,1)+(r(i)-meanr)^2;
    c(1,2)=c(1,2)+(r(i)-meanr)*(g(i)-meang);
    c(1,3)=c(1,3)+(r(i)-meanr)*(b(i)-meanb);
    c(2,2)=c(2,2)+(g(i)-meang)^2;
    c(2,3)=c(2,3)+(g(i)-meang)*(b(i)-meanb);
    c(3,3)=c(3,3)+(b(i)-meanb)^2;
end
c(2,1)=c(1,2);c(3,1)=c(1,3);c(3,2)=c(2,3);
c=c/num;
c=inv(c);
save color_m c;
% plot3(r,g,b,'b.');
% k=1;n=100;
% for rr=1:n
%     for gg=1:n
%         for bb=1:n
%             d=[rr gg bb]*c*[rr gg bb]';
%             if  d < 100
%                 rrr(k)=rr;ggg(k)=gg;bbb(k)=bb;
%                 k=k+1;
%             end
%         end
%     end
% end
% figure,plot3(rrr,ggg,bbb,'r.');