%this is the matlab image processing program 
% written Xiaofan, Ningning and Yuanyuan
% input any picture with gray pixel 
% output detect different gestures
function gesture = imageprocess(imageA)

%change gray to black white
thresh=graythresh(imageA);
image2c=im2bw(imageA,thresh);

%medfilt
image_process =medfilt2(image2c,[3,3]);
%figure,imshow(image_process);

%change white and balck
[row,column]=size(image_process);
for i=1:row
    for j=1:column
        image_process((i-1)*column+j)=not(image_process((i-1)*column+j));
    end
end

%figure,imshow(image_process)
%delete all the unimportant 
[L,m]=bwlabel(image_process,8);
stats=regionprops(L,'Area');
%figure,imshow(image_process);title('binary image');
%hold on;
max_area=0;
for i=1:m
    if(stats(i).Area(:,1)>max_area)
          max_area=stats(i).Area(:,1);
    end
end
image2c2=bwareaopen(image_process,max_area-20);

%edge algorthm
BW=edge(image2c2,'canny');
figure,imshow(BW)
[m n] =size(BW);
%hough exchange
%see http://en.wikipedia.org/wiki/Hough_transform to know the detail
[H,T,R]=hough(BW);
% figure,imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axis on, axis normal,hold on;
%find the top 5 hops of hough and decide the lines
P=houghpeaks(H,5);
x=T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
lines=houghlines(BW,T,R,P,'FillGap',50,'MinLength',7);
max_len = 0;
hold on;

%calculate the slope 
slope=zeros(length(lines),1);
for k=1:length(lines)
    xy=[lines(k).point1;lines(k).point2];
    plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
    plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
    plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
    len=norm(lines(k).point1-lines(k).point2);
    Len(k)=len;
    if (len>max_len)
        max_len=len;
        xy_long=xy;
    end
    slope(k)=-(lines(k).point1(2)-lines(k).point2(2))/(lines(k).point1(1)-lines(k).point2(1));
end
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');
Index1=0;
L1=0;
for i=1:length(Len)
    if L1<Len(i)
        L1=Len(i);
        Index1=i;
    end    
end    
x1=[lines(Index1).point1(1) lines(Index1).point2(1)];
y1=[lines(Index1).point1(2) lines(Index1).point2(2)];
K1=-(lines(Index1).point1(2)-lines(Index1).point2(2))/(lines(Index1).point1(1)-lines(Index1).point2(1));
%calculate angle and rotate the picture
angle=atan(K1)*180/pi;
image2c3 = imrotate(image2c2,-angle,'bilinear','crop');

%imagerotate=imrotate(imagecolor,-angle,'bilinear','crop');
%figure,imshow(image2c3);
% find the exact position of hand
%calculate how many black white edge do we have
guess_change=0;
count=0;
max_change=0;
max_line=0;
cur_change=0;

iterator=zeros(4,1);
[row,column]=size(image2c3);
for k=1:column
    cur_change=0;
    for i=6:row
        iterator(1)=image2c3(i-5,k);
        iterator(2)=image2c3(i-4,k);
        iterator(3)=image2c3(i-3,k);
        iterator(4)=image2c3(i-2,k);
        iterator(5)=image2c3(i-1,k);
        iterator(6)=image2c3(i,k);
        if((iterator(6)==1)&&(iterator(5)==1)&&(iterator(4)==1)&&(iterator(3)==0)&&(iterator(2)==0)&&(iterator(1)==0))
               cur_change=cur_change+1;
        end
        if((iterator(6)==0)&&(iterator(5)==0)&&(iterator(4)==0)&&(iterator(3)==1)&&(iterator(2)==1)&&(iterator(1)==1))
               cur_change=cur_change+1;
        end
    end
   if(cur_change>max_change)
        if(cur_change==guess_change)
              count=count+1;
        else
            guess_change=cur_change;
            count=1;
        end
        if(count==3)
        max_change=guess_change;
        max_line=k;
        end
   end
end

%record the longest max change situation
%suppose it should be our fingers
longest_maxchange=0;
longest_start=0;
longest_finish=0;
last_longest=0;
cur_start=0;
cur_finish=0;
IB=zeros(row,column);
for k=1:column
    cur_change=0;
    for i=4:row
        iterator(1)=image2c3(i-3,k);
        iterator(2)=image2c3(i-2,k);
        iterator(3)=image2c3(i-1,k);
        iterator(4)=image2c3(i,k);
        if((iterator(4)==1)&&(iterator(3)==1)&&(iterator(2)==0)&&(iterator(1)==0))
               cur_change=cur_change+1;
        end
        if((iterator(4)==0)&&(iterator(3)==0)&&(iterator(2)==1)&&(iterator(1)==1))
               cur_change=cur_change+1;
        end
    end
    if(cur_change==max_change)
        if(last_longest==0)
            cur_start=k;
            cur_finish=k;
        else
            cur_finish=k;
        end
    end
    if((last_longest==1)&&(cur_change~=max_change)||(k==column))
        if(cur_finish-cur_start>=longest_maxchange)
            longest_maxchange=cur_finish-cur_start+1;
            longest_start=cur_start;
            longest_finish=cur_finish;
        end
    end
    last_longest=(cur_change==max_change);
end

middle_line=ceil((longest_start+longest_finish)/2);
IG=zeros(row,column);
IB=zeros(row,column);
for i=1:row
    IB(fix(i),middle_line)=255;
end
A=cat(3,image2c3,IG,IB);
figure,imshow(A);
all_white_pixel=0;
%first_white_pixel;
%second_white_pixel;
[row,column]=size(image2c3);
max_change;
if(max_change>=6)
       gesture='PAPER';
else
    if(max_change<3)
       gesture='ROCK';
    else  
        first_finger_up=0;
        first_finger_down=0;
        second_finger_up=0;
        second_finger_down=0;
        finger_width_1=0;
        finger_width_2=0;
        %width seems not help
        for i=4:row
            iterator(1)=image2c3(i-3,middle_line);
            iterator(2)=image2c3(i-2,middle_line);
            iterator(3)=image2c3(i-1,middle_line);
            iterator(4)=image2c3(i,middle_line);
            if((iterator(4)==1)&&(iterator(3)==1)&&(iterator(2)==0)&&(iterator(1)==0))
                if(first_finger_up==0)
                    first_finger_up=i-1;
                else
                    second_finger_up=i-1;
                end 
            end
            if((iterator(4)==0)&&(iterator(3)==0)&&(iterator(2)==1)&&(iterator(1)==1))
                if(first_finger_down==0)
                    first_finger_down=i-1;
                else
                    second_finger_down=i-1;
                end 
            end
        end
        
        %slope near middle line
        first_finger_up_1=0;
        first_finger_down_1=0;
        second_finger_up_1=0;
        second_finger_down_1=0;
        first_finger_up_2=0;
        first_finger_down_2=0;
        second_finger_up_2=0;
        second_finger_down_2=0;
        %width seems not help
        for i=4:row
            iterator(1)=image2c3(i-3,middle_line-3);
            iterator(2)=image2c3(i-2,middle_line-3);
            iterator(3)=image2c3(i-1,middle_line-3);
            iterator(4)=image2c3(i,middle_line-3);
            if((iterator(4)==1)&&(iterator(3)==1)&&(iterator(2)==0)&&(iterator(1)==0))
                if(first_finger_up_1==0)
                    first_finger_up_1=i-1;
                else
                    second_finger_up_1=i-1;
                end 
            end
            if((iterator(4)==0)&&(iterator(3)==0)&&(iterator(2)==1)&&(iterator(1)==1))
                if(first_finger_down_1==0)
                    first_finger_down_1=i-1;
                else
                    second_finger_down_1=i-1;
                end 
            end
        end
         for i=4:row
            iterator(1)=image2c3(i-3,middle_line+3);
            iterator(2)=image2c3(i-2,middle_line+3);
            iterator(3)=image2c3(i-1,middle_line+3);
            iterator(4)=image2c3(i,middle_line+3);
            if((iterator(4)==1)&&(iterator(3)==1)&&(iterator(2)==0)&&(iterator(1)==0))
                if(first_finger_up_2==0)
                    first_finger_up_2=i-1;
                else
                    second_finger_up_2=i-1;
                end 
            end
            if((iterator(4)==0)&&(iterator(3)==0)&&(iterator(2)==1)&&(iterator(1)==1))
                if(first_finger_down_2==0)
                    first_finger_down_2=i-1;
                else
                    second_finger_down_2=i-1;
                end 
            end
         end

        slope_finger1=(first_finger_down_1-first_finger_down_2)/6;
        slope_finger2=(second_finger_up_1-second_finger_up_2)/6;
        
        %distinguish lizard using the slope of two figures
        if(abs(slope_finger1-slope_finger2)<=0.3)
            gesture='LIZARD';
        
        else
            %detect what is the largest value of vertical line
            [row,column]=size(image2c3);
            M_00=0;
            M_01=0;
            M_10=0;
            for i=1:row
                for k=1:column
                    if(image2c3(i,k))
                    M_00=M_00+1;
                    M_01=M_01+k;
                    M_10=M_10+i;
                    end
                end
            end
            %calculate centroid

            CenterX=M_10/M_00;
            CenterY=M_01/M_00;
            
            Center_length=0;        
            image2c3_last=0;
            cur_length=0;
            for i=1:CenterX
                if(image2c3(i,fix(CenterY))==1)
                    if(image2c3_last==0)
                        cur_length=1;
                    else
                        cur_length=cur_length+1;
                    end
                else
                    if(image2c3_last==1)
                        if(Center_length<cur_length)
                            Center_length=cur_length;
                        end
                    end    
                end
                image2c3_last=image2c3(i,fix(CenterY));
            end
            Center_length=cur_length;

            
            Center_length1=0;
            image2c3_last=0;
            cur_length=0;
            for i=fix(CenterX):row
                if(image2c3(i,fix(CenterY))==1)
                    if(image2c3_last==0)
                        cur_length=1;
                    else
                        cur_length=cur_length+1;
                    end
                else
                    if(image2c3_last==1)
                        if(Center_length1<cur_length)
                            Center_length1=cur_length;
                        end
                    end    
                end
                image2c3_last=image2c3(i,fix(CenterY));
            end
            
            if(Center_length>Center_length1)
                Center_length=Center_length1;
            end
            
            %calculate maxim width of your gesture
            count=0;
            first_appear=0;
            last_appear=0;
            for i=1:row
               for j=1:column
                    if(image2c3(i,j))
                        count=count+1;
                    end
                    if(count>2)
                        if(first_appear==0)
                            first_appear=i;
                        end
                        last_appear=i;
                    end
               end
               count=0;
            end
            maxy_num=last_appear-first_appear;

            %using the ratio between maximum width 
			%and center width to distinguish spock and scissors
            if((maxy_num/Center_length)>3)
                gesture='SPOCK';
            else
                gesture='SCISSORS';
            end
        end
    end
end
