

A=imread('or13.tif');;

imageSize = size(A);
numRows = imageSize(1);
numCols = imageSize(2);

wavelengthMin = 4/sqrt(2);
wavelengthMax = hypot(numRows,numCols);
n = floor(log2(wavelengthMax/wavelengthMin));
wavelength = 2.^(0:(n-2)) * wavelengthMin;

deltaTheta = 45;
orientation = 0:deltaTheta:(180-deltaTheta);

g = gabor(wavelength,orientation);
gabormag = imgaborfilt(A,g);

for i = 1:length(g)
    sigma = 0.5*g(i).Wavelength;
    K = 3;
    gabormag(:,:,i) = imgaussfilt(gabormag(:,:,i),K*sigma); 
end

X = 1:numCols;
Y = 1:numRows;
[X,Y] = meshgrid(X,Y);
featureSet = cat(3,gabormag,X);
featureSet = cat(3,featureSet,Y);

numPoints = numRows*numCols;
X = reshape(featureSet,numRows*numCols,[]);

X = bsxfun(@minus, X, mean(X));
X = bsxfun(@rdivide,X,std(X));

L = kmeans(X,3,'Replicates',5);

L = reshape(L,[numRows numCols]);
figure
imshow(label2rgb(L))


imll=L;



	imshow(label2rgb(imll(81:1055,81:1217)));
	
	
	Lc=L(81:1055,81:1217);
	
	o=A;
	imshow(o(81:1055,81:1217));
	imcontrast
	
	bw=Lc==3;
B = bwboundaries(bw);


hold on

for k = 1:length(B)    
boundary = B{k};   

 plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2) 


end

bw=Lc==2;
B2 = bwboundaries(bw);


hold on

for k = 1:length(B2)    
boundary = B2{k};   

 plot(boundary(:,2), boundary(:,1), 'k', 'LineWidth', 2) 


end
	
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	%k=4
	
	

A=imread('or13.tif');;

imageSize = size(A);
numRows = imageSize(1);
numCols = imageSize(2);

wavelengthMin = 4/sqrt(2);
wavelengthMax = hypot(numRows,numCols);
n = floor(log2(wavelengthMax/wavelengthMin));
wavelength = 2.^(0:(n-2)) * wavelengthMin;

deltaTheta = 45;
orientation = 0:deltaTheta:(180-deltaTheta);

g = gabor(wavelength,orientation);
gabormag = imgaborfilt(A,g);

for i = 1:length(g)
    sigma = 0.5*g(i).Wavelength;
    K = 3;
    gabormag(:,:,i) = imgaussfilt(gabormag(:,:,i),K*sigma); 
end

X = 1:numCols;
Y = 1:numRows;
[X,Y] = meshgrid(X,Y);
featureSet = cat(3,gabormag,X);
featureSet = cat(3,featureSet,Y);

numPoints = numRows*numCols;
X = reshape(featureSet,numRows*numCols,[]);

X = bsxfun(@minus, X, mean(X));
X = bsxfun(@rdivide,X,std(X));

L = kmeans(X,4,'Replicates',5);

L = reshape(L,[numRows numCols]);
figure
imshow(label2rgb(L))


imll=L;



	imshow(label2rgb(imll(81:1055,81:1217)));
	
	
	Lc=L(81:1055,81:1217);
	
	imshow(o(81:1055,81:1217));
	imcontrast
	
	bw=Lc==3;
B = bwboundaries(bw);


hold on

for k = 1:length(B)    
boundary = B{k};   

 plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2) 


end


bw=Lc==4;
B3 = bwboundaries(bw);


hold on

for k = 1:length(B3)    
boundary = B3{k};   

 plot(boundary(:,2), boundary(:,1), 'w--', 'LineWidth', 2) 


end

bw=Lc==2;
B2 = bwboundaries(bw);


hold on

for k = 1:length(B2)    
boundary = B2{k};   

 plot(boundary(:,2), boundary(:,1), 'k', 'LineWidth', 2) 


end
	
	
	%%%%%%%%%%%%%%%%%%
	
	
L = kmeans(X,2,'Replicates',5);

L = reshape(L,[numRows numCols]);

	Lc=L(81:1055,81:1217);
	
	imshow(o(81:1055,81:1217));
	imcontrast
	
	bw=Lc==2;
B = bwboundaries(bw);


hold on

for k = 1:length(B)    
boundary = B{k};   

 plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2) 


end

