-
Notifications
You must be signed in to change notification settings - Fork 1
/
SOURCECODE1.m
67 lines (59 loc) · 2.08 KB
/
SOURCECODE1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
clear all
% Gaussian Kernel is taken
%%
% VARIABLE PARAMETERS CHANGE TO GET DIFFERENT RESULTS
hs = 8; %spatial bandwidth
hr= 10; % range bandwidth
threshold_convergence_mean = 0.25;
bandwidth=[hs,hr];
%%
i=imread('sunset.bmp');
[height,width,frame] = size(i);
x=zeros(3,height*width);
disp(size(x));
%% Going from RGB space to Luv using the function RGB2LUV
for j=1:height
for l=1:width
%x(1,l+width*(j-1)) = j;
%x(2,l+width*(j-1)) = l;
[x(1,l+width*(j-1)),x(2,l+width*(j-1)),x(3,l+width*(j-1))] = RGB2LUV(i(j,l,1),i(j,l,2),i(j,l,3));
end
end
%%
%%
% finding the clusters and plotting the clusters with their data points
% centres_clusters = centres of all clusters obtained
tic
[centres_clusters,data2cluster,datapoints_cluster_no] = mean_shift_algorithm(x,bandwidth,threshold_convergence_mean);
no_clusters = length(datapoints_cluster_no);
figure(1);
hold on
color_vector = 'bgrcmykbgrcmykbgrcmykbgrcmykbgrcmykbgrmykbgrcmykbgrcmykbgrcmykbgrcmy';
for k = 1:no_clusters
clusters_dataset_members = datapoints_cluster_no{k};
cluster_centres = centres_clusters(:,k);
plot(x(1,clusters_dataset_members),x(2,clusters_dataset_members),[color_vector(k) '.'])
plot(cluster_centres(1),cluster_centres(2),'o','MarkerEdgeColor','k','MarkerFaceColor',color_vector(k), 'MarkerSize',13')
end
title(['data points with their cluster centres Gaussian Kernel (hs,hr)=',num2str(hs),',',num2str(hr)]);
hold off
%%
% creating a 3-D RGB matrix and then plotting the 3-D matrix to get the segmented image
[h2,w2] = size(centres_clusters);
zfilter=zeros(3,height*width);
for i12=1:w2
mem=datapoints_cluster_no{i12,1};
p1=size(mem);
for s1=1:p1(1,2)
zfilter(:,mem(s1))=centres_clusters(:,i12);
end
end
zluv(:,:,1)=(reshape(zfilter(1,:),width,height))';
zluv(:,:,2)=(reshape(zfilter(2,:),width,height))';
zluv(:,:,3)=(reshape(zfilter(3,:),width,height))';
zrgb = colorspace('Luv->RGB',zluv);
zrgb1=round(zrgb*255);
figure(2)
imshow(zrgb);
title(['Segmented Image (hs,hr)=',num2str(hs),',',num2str(hr)]);
%%