Hello Mat

 找回密码
 立即注册
查看: 7491|回复: 0

BP神经网络工具箱+Kmeans的图像分割算法

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-6-8 21:32:51 | 显示全部楼层 |阅读模式
BP神经网络工具箱+Kmeans的图像分割算法:
百度网盘链接:
链接:http://pan.baidu.com/s/1kVBjVAB

具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1

BP_Kmeans代码如下
  1. %% BP网络的预测
  2. %% 清空环境变量
  3. clc,clear,close all
  4. warning off
  5. format shortG
  6. % 导入数据
  7. im = imread('lena.jpg');
  8. if size(im,3)>1
  9.     im = rgb2gray(im);
  10. end
  11. if size(im,1)>256
  12.     im = imresize(im,[256,ceil(size(im,2)/(size(im,1)/256))]);
  13. end
  14. [Row,Col] = size(im);
  15. %% Kmeans进行图像数据的分类---For getting train data
  16. Kc = 2;     % 分类数量
  17. [ClusterData, center, center_index] = get_train_data(im, Kc);
  18. %% 训练数据和预测数据
  19. len = length(ClusterData);      % 样本长度
  20. input_train = ClusterData(:)';  % 列方向排列样本
  21. % 测试样本
  22. im1 = imread('ysw.jpg');
  23. im1 = im2double( im1(:,:,1) );
  24. input_test = im1(:)';
  25. % 输出
  26. output_train = center_index'; % 训练
  27. output_test = [];             % 预测的数据

  28. %% 网络结构初始化
  29. % 节点个数
  30. inputnum=1;
  31. % hiddennum=10;
  32. outputnum=1;
  33. %初始化网络结构
  34. nntwarn off
  35. % 单隐藏层
  36. % net=newff(input_train,output_train,hiddennum,{'logsig','logsig'},'trainlm');
  37. % 多隐藏层
  38. net=newff(input_train,output_train,[3,7],{'logsig','logsig'},'trainlm');
  39. net.trainParam.epochs=100;
  40. net.trainParam.lr=0.01;
  41. net.trainParam.goal=1e-16;
  42. net.trainParam.min_grad = 1e-16;
  43. net.trainParam.show=100;
  44. net.trainParam.showWindow=1;
  45. net.trainParam.max_fail=100;
  46. %网络训练
  47. [net,per]=train(net,input_train,output_train);
  48. %% BP网络预测
  49. % 训练样本--预测输出
  50. yc_train = sim(net,input_train);
  51. yc_train = round( yc_train' );
  52. error_train = abs(yc_train-output_train');
  53. disp(['训练样本均方根误差 = ',num2str(mse(error_train))])

  54. % 测试样本--预测输出
  55. yc_test = sim(net,input_test);
  56. % yc_test = sim(net,input_train);
  57. disp('预测数据如下:')
  58. yc_test = round( yc_test' );

  59. %% 测试样本分割结果
  60. [Row,Col] = size(im1);
  61. A = zeros(Row*Col, 1);
  62. A(find(yc_test==1), :) = 1;
  63. A1 = reshape(A,Row,Col);  % 第1类
  64. A = zeros(Row*Col, 1);
  65. A(find(yc_test==2), :) = 1;
  66. A2 = reshape(A,Row,Col);  % 第2类
  67. figure,
  68. subplot(121),imshow(A1,[])
  69. subplot(122),imshow(A2,[])
复制代码
采用kmeans算法获取训练样本数据:
  1. function [ClusterData, center, center_index] = get_train_data(im, Kc)

  2. if size(im,3)>1
  3.     im = rgb2gray(im);
  4. end
  5. im = im2double(im);

  6. ClusterData = im(:);
  7. % 分类数
  8. [len, depth]= size(ClusterData);
  9. % Kc = 3;
  10. % 初始化中心
  11. index = ceil( unifrnd(1,len, 1, Kc) );   % 均匀分布
  12. center = ClusterData(index,:);
  13. center_new = center;   % 初始化新的中心
  14. % maim loop
  15. maxgen = 1e3;  % 最大迭代次数
  16. for i=1:maxgen
  17.     % 计算 每一个待分类的数据隶属于的中心编号
  18.     [center_minDist,center_index] = center_nearest(center,ClusterData);
  19.     % 更新center
  20.     for j=1:Kc
  21.         index = find(center_index==j);
  22.         center_new(j, :) = mean( ClusterData(index,:) );
  23.     end
  24.     error = norm( center_new-center, 2 );  % 相连两次中心点的距离值
  25.     center = center_new;   % 更新中心
  26.     if error<0.000001
  27.         break;
  28.     end
  29. end
复制代码
聚类中心计算:
  1. function [minDist,index] = center_nearest(center,ClusterData)
  2. % ClusterData:待聚类的数据
  3. % center:聚类的中心
  4. % 函数目的:求解每一个数据隶属于的中心
  5. minDist = zeros(size(ClusterData,1),1);
  6. index = zeros(size(ClusterData,1),1);
  7. dist = zeros(size(center,1),1);
  8. for i=1:length(ClusterData)
  9.     for j=1:size(center,1)
  10.         dist(j) = norm(ClusterData(i,:) - center(j,:), 2);
  11.     end
  12.     [ minDist(i), index(i) ]=  min(dist);
  13. end
复制代码












算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-24 18:54 , Processed in 0.247031 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表