%%完整的SOM程序(x,y,r,g,b)
clc,clear
load('monkey500.mat')
data=AA';
%% 创建网络
% 3*3 自组织映射网络
net = selforgmap([1,5]);
%% 网络训练
%data = mapminmax(data);
net = train(net, data);
%% 测试
y=net(data);
% 将向量表示的类别转为标量
result = vec2ind(y);
%% 输出结果
% 将分类标签排序
M=5;
score = zeros(1,M);
for i=1:M
t=data(:, result==i);
score(i) = mean(t(:));
end
[~,ind] = sort(score);%按升序排列,~表示不输出排列后结果
%将平均值最小的一类定为第一类,平均值最大的一类定为第九类
N=166167; %N表示所有点的个数
result_ = zeros(1,N);
for i=1:M
result_(result == ind(i)) = i;
end
%输出图片
p=499;
q=333;
R1=AA(:,3);
G1=AA(:,4);
B1=AA(:,5);
R2=reshape(R1,p,q);
G2=reshape(G1,p,q);
B2=reshape(B1,p,q);
RGB_=reshape(result_,p,q);
for i=1:p
for j=1:q
if RGB_(i,j)==1
r1(i,j)=R2(i,j);
g1(i,j)=G2(i,j);
b1(i,j)=B2(i,j);
RGB1=cat(3,r1,g1,b1);
elseif RGB_(i,j)==2
r2(i,j)=R2(i,j);
g2(i,j)=G2(i,j);
b2(i,j)=B2(i,j);
RGB2=cat(3,r2,g2,b2);
elseif RGB_(i,j)==3
r3(i,j)=R2(i,j);
g3(i,j)=G2(i,j);
b3(i,j)=B2(i,j);
RGB3=cat(3,r3,g3,b3);
elseif RGB_(i,j)==4
r4(i,j)=R2(i,j);
g4(i,j)=G2(i,j);
b4(i,j)=B2(i,j);
RGB4=cat(3,r4,g4,b4);
else
r5(i,j)=R2(i,j);
g5(i,j)=G2(i,j);
b5(i,j)=B2(i,j);
RGB5=cat(3,r5,g5,b5);
%elseif RGB_(i,j)==6
%r6(i,j)=image_r(i,j);
%g6(i,j)=image_g(i,j);
%b6(i,j)=image_b(i,j);
%RGB6=cat(3,r6,g6,b6);
%elseif RGB_(i,j)==7
%r7(i,j)=image_r(i,j);
%g7(i,j)=image_g(i,j);
%b7(i,j)=image_b(i,j);
%RGB7=cat(3,r7,g7,b7);
%elseif RGB_(i,j)==8
%r8(i,j)=image_r(i,j);
%g8(i,j)=image_g(i,j);
%b8(i,j)=image_b(i,j);
%RGB8=cat(3,r8,g8,b8);
%else
%r9(i,j)=image_r(i,j);
%g9(i,j)=image_g(i,j);
%b9(i,j)=image_b(i,j);
%RGB9=cat(3,r9,g9,b9);
end
end
end
subplot(3,3,1),imshow(RGB1),title('第一类');
subplot(3,3,2),imshow(RGB2),title('第二类');
subplot(3,3,3),imshow(RGB3),title('第三类');
subplot(3,3,4),imshow(RGB4),title('第四类');
subplot(3,3,5),imshow(RGB5),title('第五类');
%subplot(3,3,6),imshow(RGB6),title('第六类');
%subplot(3,3,7),imshow(RGB7),title('第七类');
%subplot(3,3,8),imshow(RGB8),title('第八类');
%subplot(3,3,9),imshow(RGB9),title('第九类');