PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/matlab_tools/Converted/kcfuzzycmeans.m

http://github.com/aludnam/MATLAB
Objective C | 206 lines | 206 code | 0 blank | 0 comment | 1 complexity | 0eabdde233dbb53174389b45e9ab91ee MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %kcfuzzycmeans 'Performs Fuzzy C-Means Clustering '
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros cfuzzycmeans.pane file
  3. %
  4. % Parameters:
  5. % InputFile: i1 'Input data object', required: 'input data object'
  6. % InputFile: i2 'Input initial cluster centers', optional: 'input initial cluster centers'
  7. % InputFile: i3 'Input initial membership functions', optional: 'input initial membership functions'
  8. % InputFile: i4 'Input membership functions biases', optional: 'input membership functions biases'
  9. % Integer: c 'Number of clusters', default: 2: 'number of clusters'
  10. % Integer: n 'Max number of iterations', default: 50000: 'max number of iterations'
  11. % Toggle: map 'Generate output map', default: 0: 'generate output map'
  12. % Toggle: spectrum 'SPECTRUM compatable map segment', default: 0: 'SPECTRUM compatable map segment'
  13. % OutputFile: o1 'Cluster number output object', required: 'Cluster number output object'
  14. % OutputFile: o2 'Cluster center output object', optional: 'Cluster center output object'
  15. % OutputFile: o3 'Cluster variance output object', optional: 'Cluster variance output object'
  16. % OutputFile: o4 'Cluster membership count output', optional: 'Cluster membership count output'
  17. % OutputFile: o5 'Final membership functions', optional: 'Data final membership functions'
  18. % OutputFile: o6 'FCM statistics output (ASCII)', optional: 'FCM statistics output (ASCII)'
  19. %
  20. % Example: [o1, o2, o3, o4, o5, o6] = kcfuzzycmeans({i1, i2, i3, i4}, {'i1','';'i2','';'i3','';'i4','';'c',2;'n',50000;'map',0;'spectrum',0;'o1','';'o2','';'o3','';'o4','';'o5','';'o6',''})
  21. %
  22. % Khoros helpfile follows below:
  23. %
  24. % PROGRAM
  25. % cfuzzycmeans - Performs Fuzzy C-Means Clustering
  26. %
  27. % DESCRIPTION
  28. % The "cfuzzycmeans" kroutine will perform Fuzzy C-Means Clustering in all non-masked vectors in the input image passed by the parameter [-i1]. The Fuzzy C-Means clustering algorithm considers that one vector can belong to more than one cluster and belong to clusters to different extents. As an example, the vector X can belong 0.5 to cluster A, 0.3 to cluster B, 0.4 to cluster C and 0.1 to cluster D.
  29. % Fuzzy values are \fBnot" probabilities, so they don't need to sum up to one. To be used as a classifier, the fuzzy membership values must be defuzzified later (the program can also do this), with an alternative minimum membership threshold. This allows possible rejection of pixels which membership functions are too low to be considered really belonging to a cluster.
  30. % The number of clusters must be specified with the parameter [-c]. Initial c cluster centers can be passed to the routine, if cluster centers are not passed, they will be selected randomically unless the parameter [-i4] is used (see below).
  31. % The exponential weight value passed by the parameter [-m] controls the fuzzyness of the clustering: values close to 1 will assign strong membership function values to the points, while values higher will assign smaller membership function values.
  32. % Clustering is iterated until a maximum number of iterations specified by the parameter [-n] is achieved or until the objective function for the clustering has a difference smaller than the specified by the parameter [-epsilon] from its last iteration.
  33. % At the end of clustering, the membership functions will be scanned for the largest value for defuzzification. Only membership functions over the parameter [-alpha] will be considered, meaning that points which membership functions are all under alpha will be masked since they cannot be assigned to any cluster.
  34. % The user can pass initial cluster centers with the parameter [-i2], as with the "kkmeans" program. If passed, this data must have dimensions Cx1x1x1xE, where C is the number of classes and E is the number of elements in the [-i1] input data.
  35. % Other optional input data can be passed: initial membership function values with [-i3], with dimensions WxHxDxTxC, where W,H,D,T are the width, height, depth and time dimensions of the input data. If this parameter is not used the initial membership values will be random. This routine also can use biases for the membership functions passed with the [-i4] parameter, with a data object with dimensions WxHxDxTxC. If data is passed by the [-i4] parameter the initial cluster centers and membership functions will be calculated using these biases.
  36. % Most output files for this function are compatible with "kkmeans". The first output [-o1] will contain the clustered output which was defuzzified as described above. If the parameter [-map] is passed, a map will be created using the values from the center of the clusters. If the parameter [-spectrum] is passed, a map compatible with the info spectrum expects will be created.
  37. % If the parameter [-o2] is passed, it will create a file with the cluster centers stored row by row in the value segment. If the parameter [-o3] is passed, it will create a file containing contains cluster variances stored row by row in the value segment. If the parameter [-o4] is passed, it will create a file with the cluster vector counts stored row by row in the value segment. If the parameter [-o5] is passed, it will create a file with the final membership functions in the value segment. The outputs for the parameters [-o2], [-o3] and [-o4] will be created considering the defuzzified output of the routine.
  38. % Please refer to the Classify toolbox manual for more information on the Fuzzy C-Means algorithm.
  39. %
  40. %
  41. %
  42. % EXAMPLES
  43. % All examples for the Classify toolbox are listed on the Classify Toolbox Manual. For an example of this routine, please see the Classify:workspaces:FuzzyCMeans example workspace.
  44. %
  45. % "SEE ALSO"
  46. % kmeans, ckmeans2, the Classify Toolbox Manual.
  47. %
  48. % RESTRICTIONS
  49. % Restrictions on the input data dimensions as shown in the Description section above.
  50. %
  51. % REFERENCES
  52. % All references for the Classify toolbox are listed on the Classify Toolbox Manual.
  53. %
  54. % COPYRIGHT
  55. % Copyright (C) 1997 Rafael Santos. Khoros (C) Khoral Research, Inc.
  56. %
  57. function varargout = kcfuzzycmeans(varargin)
  58. if nargin ==0
  59. Inputs={};arglist={'',''};
  60. elseif nargin ==1
  61. Inputs=varargin{1};arglist={'',''};
  62. elseif nargin ==2
  63. Inputs=varargin{1}; arglist=varargin{2};
  64. else error('Usage: [out1,..] = kcfuzzycmeans(Inputs,arglist).');
  65. end
  66. if size(arglist,2)~=2
  67. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  68. end
  69. narglist={'i1', '__input';'i2', '__input';'i3', '__input';'i4', '__input';'c', 2;'n', 50000;'map', 0;'spectrum', 0;'o1', '__output';'o2', '__output';'o3', '__output';'o4', '__output';'o5', '__output';'o6', '__output'};
  70. maxval={0,1,1,1,2,100000,0,0,0,1,1,1,1,1};
  71. minval={0,1,1,1,2,0,0,0,0,1,1,1,1,1};
  72. istoggle=[0,1,1,1,0,1,1,1,0,1,1,1,1,1];
  73. was_set=istoggle * 0;
  74. paramtype={'InputFile','InputFile','InputFile','InputFile','Integer','Integer','Toggle','Toggle','OutputFile','OutputFile','OutputFile','OutputFile','OutputFile','OutputFile'};
  75. % identify the input arrays and assign them to the arguments as stated by the user
  76. if ~iscell(Inputs)
  77. Inputs = {Inputs};
  78. end
  79. NumReqOutputs=1; nextinput=1; nextoutput=1;
  80. for ii=1:size(arglist,1)
  81. wasmatched=0;
  82. for jj=1:size(narglist,1)
  83. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  84. wasmatched = 1;
  85. was_set(jj) = 1;
  86. if strcmp(narglist{jj,2}, '__input')
  87. if (nextinput > length(Inputs))
  88. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  89. end
  90. narglist{jj,2} = 'OK_in';
  91. nextinput = nextinput + 1;
  92. elseif strcmp(narglist{jj,2}, '__output')
  93. if (nextoutput > nargout)
  94. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  95. end
  96. if (isempty(arglist{ii,2}))
  97. narglist{jj,2} = 'OK_out';
  98. else
  99. narglist{jj,2} = arglist{ii,2};
  100. end
  101. nextoutput = nextoutput + 1;
  102. if (minval{jj} == 0)
  103. NumReqOutputs = NumReqOutputs - 1;
  104. end
  105. elseif isstr(arglist{ii,2})
  106. narglist{jj,2} = arglist{ii,2};
  107. else
  108. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  109. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  110. end
  111. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  112. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  113. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  114. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  115. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  116. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  117. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  118. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  119. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  120. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  121. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  122. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  123. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  124. end
  125. end
  126. end
  127. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  128. narglist{jj,2} = arglist{ii,2};
  129. end
  130. end
  131. end
  132. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  133. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  134. end
  135. end
  136. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  137. for jj=1:size(narglist,1)
  138. if strcmp(paramtype{jj}, 'Toggle')
  139. if (narglist{jj,2} ==0)
  140. narglist{jj,1} = '';
  141. end;
  142. narglist{jj,2} = '';
  143. end;
  144. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  145. narglist{jj,1} = '';
  146. narglist{jj,2} = '';
  147. end;
  148. if strcmp(narglist{jj,2}, '__input')
  149. if (minval{jj} == 0) % meaning this input is required
  150. if (nextinput > size(Inputs))
  151. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  152. else
  153. narglist{jj,2} = 'OK_in';
  154. nextinput = nextinput + 1;
  155. end
  156. else % this is an optional input
  157. if (nextinput <= length(Inputs))
  158. narglist{jj,2} = 'OK_in';
  159. nextinput = nextinput + 1;
  160. else
  161. narglist{jj,1} = '';
  162. narglist{jj,2} = '';
  163. end;
  164. end;
  165. else
  166. if strcmp(narglist{jj,2}, '__output')
  167. if (minval{jj} == 0) % this is a required output
  168. if (nextoutput > nargout & nargout > 1)
  169. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  170. else
  171. narglist{jj,2} = 'OK_out';
  172. nextoutput = nextoutput + 1;
  173. NumReqOutputs = NumReqOutputs-1;
  174. end
  175. else % this is an optional output
  176. if (nargout - nextoutput >= NumReqOutputs)
  177. narglist{jj,2} = 'OK_out';
  178. nextoutput = nextoutput + 1;
  179. else
  180. narglist{jj,1} = '';
  181. narglist{jj,2} = '';
  182. end;
  183. end
  184. end
  185. end
  186. end
  187. if nargout
  188. varargout = cell(1,nargout);
  189. else
  190. varargout = cell(1,1);
  191. end
  192. global KhorosRoot
  193. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  194. w=['"' KhorosRoot];
  195. else
  196. if ispc
  197. w='"C:\Program Files\dip\khorosBin\';
  198. else
  199. [s,w] = system('which cantata');
  200. w=['"' w(1:end-8)];
  201. end
  202. end
  203. [varargout{:}]=callKhoros([w 'cfuzzycmeans" '],Inputs,narglist);