%first read all the texture images
%texturekmean_rev('testimage.jpg,6,6,2,1);

function [] = texturekmean_rev(filename,num,dim,color,density);
close all;

%read textures
for i = 1:num
    texturefilename = strcat(int2str(i),'.jpg');
    origin = imread(texturefilename);
    YIQ = rgb2ntsc(origin);
    [row,col,k] = size(YIQ);
    if (color == 1)     %grey color
        temp = reshape(YIQ(:,:,1),row*col,1);
    else
        temp = reshape(YIQ,row*col*3,1);
    end
    sss = reshape(YIQ(:,:,1),row*col,1);
    texturemean(i) = mean(sss(:,1));

    x(:,i) = temp(:,1);
end

%[a,b] = size(x)

%compute PCA
pc = pcacov(cov(x'));

glbm = mean(x')';

%for each texture, find its coresponding vector
for i = 1:num 
    cvector(:,i) = pc(:, [1:dim])'*(x(:,i)-glbm);
end

%[a,b] = size(cvector)

%texture detection
Target = imread(filename);
figure, imshow(Target);
C = double(Target);
[sizem,sizen,k] = size(Target);
B = reshape(Target,sizem*sizen,3);
D = double(B);
YIQ = rgb2ntsc(Target);
BIGI = YIQ(:,:,1);
figure, imshow(BIGI);
[row1,col1] = size(BIGI);


if (density==1)
    stepr = 1;
    stepc = 1;
else
    stepr = row;
    stepc = col;
end
row2 = row1-row+1;
col2 = col1-col+1;

for m=1:row1
    for n=1:col1
        textures(m, n) = 1;
    end
end

for i=1:stepr:row2
    for j=1:stepc:col2
        %test each points wehther it belongs to the texture
        if (color == 1)
            imageblock = reshape(BIGI(i:i+row-1,j:j+col-1), row*col,1);
        else
            imageblock = reshape(YIQ(i:i+row-1,j:j+col-1,:), row*col*3,1);
        end
        
        ss = reshape(BIGI(i:i+row-1,j:j+col-1), row*col,1);
        blockmean = mean(ss(:,1));
        
        minDIF = 0;
        for m = 1:num
            %do something for the imageblock
            reviseblock = imageblock*(texturemean(m)/blockmean);
            %do the similarity test
            imagevector = pc(:, [1:dim])'*(imageblock(:,1)-glbm);

            %calculate euclidean distance
            dif = 0;
            for n = 1:dim
                dif = dif + (imagevector(n)-cvector(n,m))^2;
            end
            if ((m==1)||(minDIF>dif))
                minDIF = dif;
                imagetexture = m;
            end
        end
        %assign a good value for it
        if (density == 1)
            textures(i,j) = imagetexture;
        else
%             for m=i:i+row-1
%                 for n=j:j+col-1
%                     textures(m,n) = imagetexture;
%                 end
%             end
            textures(i:i+row-1, j:j+col-1) = imagetexture;
        end
    end
end

%show
[m, n]=size(textures);

for i = 1:num
    for k = 1:3
        cmean(i,k) = 0;
    end
    cluster(i) = 0;
end

%calculate the mean value for the color of each region
for i = 1:m
    for j = 1:n
        index = textures(i,j);
        for k = 1:3
	        cmean(index,k) = cmean(index,k)+C(i,j,k);
	    end
        cluster(index) = cluster(index) + 1;
    end
end

for i = 1:num
    for k = 1:3
       cmean(i,k) = cmean(i,k)/cluster(i);
    end
end


for i = 1:m
    for j = 1:n
        index = textures(i, j);
	    for k = 1:3
	        result(i, j, k) = cmean(index,k);
    	end
    end
end
figure; imshow(uint8(result));
% using texture value as fourth dimension and use k-mean
FDim = reshape(textures,sizem*sizen,1);
NewD(:,1:3) = D;
NewD(:,4) = FDim(:,1)*600;

[distance,label,tse] = kmeans1(D, dim);

E = reshape(label, sizem, sizen);

[m, n]=size(E);

for i = 1:num
    for k = 1:3
        cmeans(i,k) = 0;
    end
    clusters(i) = 0;
end

%calculate the mean value for the color of each region
for i = 1:m
    for j = 1:n
        index = E(i,j);
        for k = 1:3
	        cmeans(index,k) = cmeans(index,k)+C(i,j,k);
	    end
        clusters(index) = clusters(index) + 1;
    end
end

for i = 1:num
    for k = 1:3
       cmeans(i,k) = cmeans(i,k)/clusters(i);
    end
end


for i = 1:m
    for j = 1:n
        index = E(i, j);
	    for k = 1:3
	        results(i, j, k) = cmeans(index,k);
    	end
    end
end


figure; imshow(uint8(results));