clear all 
close all

for nnn = 1
    disp(nnn)
    mkdir([num2str(nnn-1,'%02.0f'),'\rot\'])
    for i = 1:100
        disp(i)
%         temp = imread([num2str(nnn-1,'%02.0f');
         temp = imread([num2str(i-1,'%02.0f'),'.png']);
        origImage = temp;
        temp = rgb2gray(temp);
        temp = temp == 0;
        %subplot(1,3,1);
        %imagesc(temp)
        %title(i)
        stats = regionprops(temp,'Orientation');%find the orientation 
        %angleRegionProps(i) = 90-stats.Orientation();
        %subplot(1,3,2)
        %imagesc(imrotate(temp,-angle(i)))
        %title(num2str(angleRegionProps(i)))
        %subplot(1,3,3)
        count = 0;
        for j = 1:size(temp,1)
            temp2 = find(temp(j,:)==0,1);
            if ~isempty(temp2)
                count = count + 1;
                goodPixel(count,1) = j;
                goodPixel(count,2) = temp2;
            end
        end
        temp3 = [goodPixel(:,1) ones(size(goodPixel(:,1)))]\goodPixel(:,2);
        angleGuess(i) = atan(temp3(1))*180/pi;
        %imagesc(imrotate(origImage,-angleGuess(i),'bicubic'))
        rotatedImage = imrotate(origImage,-angleGuess(i),'bicubic','crop');
        cropped = rotatedImage(:,find(mean(rotatedImage(:,:,1))>60),:);
        %edged = edge(cropped,'Roberts');
        %imagesc(edged)
        finalImage(i).png = imresize(cropped,[3000,20]);
        % Display images to be stitched
        %imshow(finalImage(i))
        imwrite(finalImage(i).png,[num2str(nnn-1,'%02.0f'),'\rot\',num2str(i-1,'%02.0f'),'.png'])
        %plot(goodPixel(:,1),goodPixel(:,2))
        %drawnow()

    end
end

%matching part
clear all 
close all
clc;
ex=2;
%size as defined in a cropped portion
Ed=zeros(100,3000,3);
St=zeros(100,3000+ex+ex+1,3);
fld='00\rot\';
for i = 1:100
    temp = imread([fld,num2str(i-1,'%02.0f'),'.png']);
%     origImage = temp;
%     temp(:,:,1)=edge(temp(:,:,1),'canny');
%     temp(:,:,2)=edge(temp(:,:,2),'canny');
%     temp(:,:,3)=edge(temp(:,:,2),'canny');
%    temp(temp<70)=0;
%    temp(temp>=70)=255;
    
    ss=temp(:,:,1);    
    temp(:,:,1)=imbinarize(ss, 'adaptive');%im2bw(ss,.4);%median(ss(:)/255.0));
    ss=temp(:,:,2);    
    temp(:,:,2)=imbinarize(ss, 'adaptive');%median(ss(:)/255.0));
    ss=temp(:,:,3);    
    temp(:,:,3)=imbinarize(ss, 'adaptive');%median(ss(:)/255.0));
    St(i,ex+1:3000+ex,:)=temp(:,1,:); %first col of each image
    Ed(i,:,:)=temp(:,length(temp(1,:,1)),:);  %last col
end

MCf=zeros(100,2);
MCv=zeros(100,1);
rev=zeros(100,1);
rev2=zeros(100,1);
MCv2=zeros(100,1);
for i = 1:100
    tmp=ones(100,2)*30000000;
    tmpx=ones(100,1)*30000000;
    
    for j = 1:100
        tmp2=ones(ex+ex+1,1)*3000000000;
        tmp3=ones(ex+ex+1,1)*3000000000;
        for k=-ex:ex
            if i~=j
                tmp2(k+ex+1)=sum(sum(abs(St(i,k+ex+1:k+3000+ex,:)-Ed(j,:,:))));%difference of first col and last col
                tmp3(k+ex+1)=sum(sum(abs(St(i,k+ex+1:k+3000+ex,:)-Ed(j,3000:-1:1,:))));
                
            end
        end
        tmp(j,:)=[min(tmp2),min(tmp3)];%min of differences
        tmpx(j,:)=min([min(tmp2),min(tmp3)]);
    end
    tn=min(tmp(:,1));    
    tr=min(tmp(:,2));
    if tn<tr
        rev(i)=0;
    else
        rev(i)=1;
    end
    MCv(i)=min([tn,tr]);
    if(min(tmpx)<.000001*(max(tmpx)))
        
        MCf(i,1)=min(find(tmpx==min(tmpx)));
        MCf(i,2)=i;
    else
        MCf(i,:)=[-1,-1];
    end
    
end
MCf0=MCf;

MCf=MCf(MCf(:,1)>0,:)

lst=zeros(1,100);
lst(1)=MCf(1,1);
lst(2)=MCf(1,2);
MCf(1,1)=300;
MCf(1,2)=300;
i=3;
while i<=100
    t=find(MCf(:,1)==lst(i-1));
    
    %t=min(find(MCf==lst(i-1)));
    if length(t)<1
        t=min(find(MCf(:,1)==min(MCf(:,1))));
        lst(i)=MCf(t,1);
        rev2(i)=rev(t);
        i=i+1;
        lst(i)=MCf(t,2);
        MCf(t,2)=300;
        MCf(t,1)=300;
        i=i+1;
    elseif(length(t)>1)
        t=t(MCf(t,2)==min(MCf(t,2)));
        
        rev2(i)=rev(t);
        lst(i)=MCf(t,1);
        i=i+1;
        lst(i)=MCf(t,2);
        MCf(t,2)=300;
        MCf(t,1)=300;
        i=i+1;
    else
        lst(i)=MCf(t,1);
        
        rev2(i)=rev(t);
        i=i+1;
        lst(i)=MCf(t,2);
        MCf(t,2)=300;
        MCf(t,1)=300;
        i=i+1;
        
    end
        
    
    
    
    %MCf2=MCf(MCf>t)
end



for i = 0:99
    if(lst(i+1)<=100)
        if(rev2(i+1)==1)
            op(3000:-1:1,(i*20)+1:((i+1)*20),:)=imread([fld,num2str(lst(i+1)-1,'%02.0f'),'.png']);
        else
            op(:,(i*20)+1:((i+1)*20),:)=imread([fld,num2str(lst(i+1)-1,'%02.0f'),'.png']);
        end
    end
end
imshow(uint8(op))