在我們的生活中那,如何操作上述標題的小問題,小編今天就在這裡給大家分享一點我的小經驗,來增加我們的體驗,希望可以給你們帶來幫助。1把解壓後的StarCraft資料夾放到手機的內建
2020-11-30 07:49:03
剛開始用matlab學C均值聚類演算法,在網上找了好久發現東西很散很離亂,說的不清楚,下面就這個問題做詳細的講解,力求看過大家都能明白,這個演算法比較簡單,但是很常用,FCM程式碼的全解,以及用在FCM進行影象分割,希望大家看過後給我投票,或是有什麼問題不懂的可以在下面給我留言
首先,你要知道什麼是C均值聚類演算法,就是那個公式,你最好要能推出來,其次,要明白matlab中自帶FCM 的程式碼含義 ,在命令窗中輸入 edit fcm; 會在M檔案中開啟,前面是註釋
function [center, U, obj_fcn] = fcm(data, cluster_n, options)
%FCM Data set clustering using fuzzy c-means clustering.
%% [CENTER, U, OBJ_FCN] = FCM(DATA, N_CLUSTER) finds
N_CLUSTER number of
% clusters in the data set DATA. DATA is size M-by-N, where M is
the number of
% data points and N is the number of coordinates for each data point. The
% coordinates for each cluster center are returned in the rows of the matrix
% CENTER. The membership function matrix U contains the grade of membership of
% each DATA point in each cluster. The values 0 and 1 indicate no membership
% and full membership respectively. Grades between 0 and 1 indicate that the
% data point has partial membership in a cluster. At each iteration, an
% objective function is minimized to find the best location for the clusters
% and its values are returned in OBJ_FCN.%
% [CENTER, ...] = FCM(DATA,N_CLUSTER,OPTIONS) specifies a vector of options
% for the clustering process:% OPTIONS(1): exponent for the matrix U (default: 2.0)% OPTIONS(2): maximum number of iterations (default: 100)% OPTIONS(3): minimum amount of improvement (default: 1e-5)
% OPTIONS(4): info display during iteration (default: 1)
% The clustering process stops when the maximum number of iterations
% is reached, or when the objective function improvement between two
% consecutive iterations is less than the minimum amount of improvement
% specified. Use NaN to select the default value.%
% Example
% data = rand(100,2);
% [center,U,obj_fcn] = fcm(data,2);
% plot(data(:,1), data(:,2),'o');
% hold on;
% maxU = max(U);
% % Find the data points with highest grade of membership in cluster 1
% index1 = find(U(1,:) == maxU);
% % Find the data points with highest grade of membership in cluster 2
% index2 = find(U(2,:) == maxU);
% line(data(index1,1),data(index1,2),'marker','*','color','g');
% line(data(index2,1),data(index2,2),'marker','*','color','r');
% % Plot the cluster centers
% plot([center([1 2],1)],[center([1 2],2)],'*','color','k')% hold off;%
% See also FCMDEMO, INITFCM, IRISFCM, DISTFCM, STEPFCM.
% Roger Jang, 12-13-94, N. Hickey 04-16-01
% Copyright 1994-2002 The MathWorks, Inc.
% $Revision: 1.13 $ $Date: 2002/04/14 22:20:38 $
% %後是說明部分,從此處開始是函數定義
if nargin ~= 2 & nargin ~= 3,
error('Too many or too few input arguments!');
end
data_n = size(data, 1);
in_n = size(data, 2);
% Change the following to set default options
default_options = [2; % exponent for the partition matrix U
100; % max. number of iteration
1e-5; % min. amount of improvement
1]; % info display during iteration
if nargin == 2,
options = default_options;
else
% If "options" is not fully specified, pad it with default values.
if length(options) < 4,
tmp = default_options;
tmp(1:length(options)) = options;
options = tmp;
end
% If some entries of "options" are nan's, replace them with defaults.
nan_index = find(isnan(options)==1);
options(nan_index) = default_options(nan_index);
if options(1) <= 1,
error('The exponent should be greater than 1!');
end
end
expo = options(1); % Exponent for U
max_iter = options(2); % Max. iteration
min_impro = options(3); % Min. improvement
display = options(4); % Display info or not
obj_fcn = zeros(max_iter, 1); % Array for objective function
U = initfcm(cluster_n, data_n); % Initial fuzzy partition
% Main loop
for i = 1:max_iter,
[U, center, obj_fcn(i)] = stepfcm(data, U, cluster_n, expo);
if display,
fprintf('Iteration count = %d, obj. fcn = %fn', i, obj_fcn(i));
end
% check termination condition
if i > 1,
if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end,
end
end
iter_n = i; % Actual number of iterations
obj_fcn(iter_n+1:max_iter) = [];
英文看起來比較鬱悶的看中文如下
關於初始化子函數 function U = initfcm(cluster_n, data_n), 程式碼全解如下
下一個疊代子函數 function [U_new, center, obj_fcn] = stepfcm(data, U, cluster_n, expo)
下一個計算距離子函數 function out = distfcm(center, data)
所以這些函數都是用matlab 自帶的函數,包括子函數,你可以把所有的函數放在一個M檔案中 下面將貼出我自己的關於FCM的全碼,都是在自帶函數基礎上改的,
接著進行影象分割,呼叫程式碼如下,可以直接輸入在命令視窗中,這段程式碼大家要好好研究
下面展示下效果圖,有疊代次數,聚類中心,還有分割後的影象。大家研究下吧
相關文章
在我們的生活中那,如何操作上述標題的小問題,小編今天就在這裡給大家分享一點我的小經驗,來增加我們的體驗,希望可以給你們帶來幫助。1把解壓後的StarCraft資料夾放到手機的內建
2020-11-30 07:49:03
星海爭霸2,發現成就裡有個揭露黑幕是黑的,該怎麼辦呢?1首先觸發隱藏關卡「揭露黑幕」是要在「媒體轟炸」這一任務裡用A強制攻擊摧毀右下角的平民建築。如果你已經打過媒體轟炸,
2020-11-30 06:46:19
此製作流程為詳細版,適合所有玩家或者作者。1v1地圖在星海爭霸2乃至所有遊戲中是控制平衡性最重要的因素之一,所以想做地圖,就要先會玩遊戲。在此我將為各位展示目前版本的地圖
2020-11-30 06:06:43
使用者有遇到過在玩星海爭霸2遊戲時會出現閃退現象,這是驅動問題或系統設定不當導致的,下面一起來看看有什麼解決方法吧。1首先,換一個顯示卡驅動,可通過官方網站下載軟體更新驅
2020-11-30 05:46:43
教你從青銅打到大師~1第1課.青銅組青銅組目標:把錢花光,瞭解基本的建築順序所謂學會4BG,白銀2800青銅組的P民們第一課是學會4BG的基本建築順序,然後把錢用來刷兵[專業詞彙解釋
2020-11-30 05:29:21
星海爭霸1是一款90年代末流行的即時戰略遊戲,居然到現在還有很多人玩,一是說明這個遊戲質量非常好,二是證明此類遊戲後繼無人,下面介紹一下星海爭霸1蟲族任務第七關攻略以供參考
2020-11-30 02:29:46