%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    Hw7: group textures by PCA                     %
%    Student: Bo Han                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
% read in each image and compute the moment invarients
img=[];
imgvec=[];

region_size=100;
sample_size=50;
figure;
for i=1:6
    tem=sprintf('%1d',i);
    filename=['hw7_' tem '.jpg'];
    img=imread(filename);
    [h,w]=size(img);
    %extract the sample regions
    imgt1=img(h-region_size+1:h, 1:region_size);
    imgt2=img(1:region_size, w-region_size+1:w);

    %show images
    subplot(6,2, (i-1)*2+1);
    imshow(imgt1);
    tem1=sprintf('%2d',(i-1)*2+1);
    title(tem1);
    
    subplot(6,2, (i-1)*2+2);    
    imshow(imgt2);
    tem2=sprintf('%2d',(i-1)*2+2);
    title(tem2);
    
    %resize the sample regions
    imgt1=imresize(imgt1,sample_size/region_size,'bilinear');
    imgt2=imresize(imgt2,sample_size/region_size,'bilinear');
    %reshape the sample region into vector
    imgvec((i-1)*2+1,:)=reshape(imgt1, 1, sample_size*sample_size);
    imgvec((i-1)*2+2,:)=reshape(imgt2, 1, sample_size*sample_size);
end
% 
%standarize the vectors
for j=1:sample_size*sample_size
    tem=imgvec(:,j);
    stdt=std(tem);
    meant=mean(tem);
    stdvec(:,j)=(tem-meant)./stdt;
end

retvar=[];
for dim=1:12
    [pcavec,proj,retvar(dim)] = pca_an(stdvec,dim,0);
    dim
    %compare the Euclidean Distance
    edist=[];
    for i=1:12
        for j=1:12
          edist(i,j) = vectorComp(pcavec(i,:),pcavec(j,:), 2);
      end
    end
    edist 
    %compare the Cosine Distance
    cosdist=[];
    for i=1:12
        for j=1:12
          cosdist(i,j) = vectorComp(pcavec(i,:),pcavec(j,:), 1);
        end
    end
    cosdist
end
