clear; close all hidden;
load color_m; %load study result to matrix c
pic_ori=imread('1.jpg','jpeg');
[m,n,z]=size(pic_ori);
borderm=50;bordern=30; %cut the border to reduce the edge effect from the camera
pic=pic_ori(borderm:m-borderm,bordern:n-bordern,:);
[m,n,z]=size(pic);
% dmin=10000;dmax=0; %not used yet
pic_bi=ones(m,n); %to store binary image, 0 to be background
for i=1:m
    for j=1:n
        v_rgb=double([pic(i,j,1) pic(i,j,2) pic(i,j,3)]);
        d=v_rgb*c*v_rgb';
%         if d<dmin
%             dmin=d;
%         end
%         if d>dmax
%             dmax=d;
%         end
        if d>300 %it is definitely not a pixel of the plate
            pic(i,j,1)=255;
            pic(i,j,2)=255;
            pic(i,j,3)=255;
            pic_bi(i,j)=0;
        end
    end
end
pic_gray=rgb2gray(pic);
pic_gray1=pic_gray;
[pic_bi labeln]=bwlabel(pic_bi,8); %get connected areas
%throw away connected areas with pixels less than min_count
count=zeros(1,labeln);
for i=1:m
    for j=1:n
        pix=pic_bi(i,j);
        if pix ~= 0
            count(pix)=count(pix)+1;
        end
    end
end
min_count=150;
for i=1:labeln
    if count(i)<min_count
        count(i)=0;
    end
end
for i=1:m
    for j=1:n
        pix=pic_bi(i,j);
        if pix ~= 0
            if count(pix) == 0
                pic_gray(i,j)=255;
            end
        end
    end
end

%imview(pic);imview(pic_gray);
% se=strel('rectangle',[3 3]);
% pic_gray=imopen(pic_gray,se);
% pic_edge=edge(pic_gray,'sobel');
pic_edge=edge(pic_gray,'prewitt','vertical');
y=zeros(m,1);
for i=1:m
    for j=1:n
        y(i)=y(i)+pic_edge(i,j);
    end
end
y=smooth(y);
z=zeros(m,1);
for i=1:m
    for j=1:n
        z(i)=z(i)+double(pic_gray(i,j));
    end
end
maxz=max(z);
for i=1:m
    z(i)=maxz-z(i);
end
z=smooth(z,10);
sumy=0;
for i =1:m
    sumy=sumy+y(i);
end
for i=1:m
    y(i)=y(i)-sumy/m;
%     if y(i)<0
%         y(i)=0;
%     end
end
% plot(y);
% figure,plot(z);
% imview(pic_gray');
% imview(pic_edge');

%locate the plate in vertical direction
for i=1:m-1
    j=m-i+1;
    if z(j-1)-z(j) > 400
        bottom=j;
        break;
    end
end
for i=m-bottom+1:m-1
    j=m-i+1;
    if z(j-1)-z(j) < 400 && z(j) < 3000
        top=j;
        break;
    end
end
plot(z,1:m);
hold on;
plot(1:max(z),top*ones(max(z),1),'r-');
plot(1:max(z),bottom*ones(max(z),1),'r-');
hold off;

%locate the plate in horizontal direction
pic1_gray=pic_gray(top:bottom,:);
imview(pic1_gray);
[m n]=size(pic1_gray);
w=zeros(1,n);
for j=1:n
    for i=1:m
        w(j)=w(j)+double(pic1_gray(i,j));
    end
end
maxw=max(w);
for j=1:n
    w(j)=maxw-w(j);
end
w=smooth(w,10);
for j=1:n-1
    i=n-j+1;
    if w(i-1)-w(i) > 200
        right=i;
        break;
    end
end
for j=n-right+1:n-1
    i=n-j+1;
    if w(i-1)-w(i) < 200 && w(i) < 300
        left=i;
        break;
    end
end
figure,plot(w);
hold on;
plot(right*ones(max(w),1),1:max(w),'r-');
plot(left*ones(max(w),1),1:max(w),'r-');
hold off;
pic2_gray=pic1_gray(:,left:right);
imview(pic2_gray);
save plate_region pic2_gray;