clc; clear all ;
clear all;
close all;
clc;
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'Select MRI');
inputimage=strcat(pathname, filename);
input=imread(inputimage);
[m,n,p] = size(input) ;
if p==1
disp('Select RGB image')
break
end
figure,
imshow(input,[]);
axis off;
title('Input Image','fontsize',12,'fontname','Times New Roman','color','Black');
[row,col,cha] = size(input);
input_img = input;
if cha ==3
input = rgb2gray(input);
end
preim = medfilt2(input);
figure,
imshow(preim,[]);
axis off;
title('Preprocessed Image','fontsize',12,'fontname','Times New Roman','color','Black');
I(:,:,1) = input_img(:,:,1);
I(:,:,2) = input_img(:,:,2);
I(:,:,3) = input_img(:,:,3);
text(size(I,2),size(I,1)+15,...
'Clustering process is done', ...
'FontSize',7,'HorizontalAlignment','right');
ab = double(I(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',2);
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure,
imshow(pixel_labels,[]), title('Kmeans clustering');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = I;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
Fig1 = segmented_images{1};
Fig2 = segmented_images{2};
Fig3 = segmented_images{3};
figure,
subplot(2,2,1),
imshow(Fig1);
subplot(2,2,2),
imshow(Fig2);
subplot(2,2,3),
imshow(Fig3);