PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/matlab_tools/Converted/klrfclass.m

http://github.com/aludnam/MATLAB
Objective C | 286 lines | 283 code | 3 blank | 0 comment | 54 complexity | 5fc3ce366326d77e467e5a44cc634240 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %klrfclass 'Classify Image Using the Localized Receptive Field Classifier (K1)'
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros lrfclass.pane file
  3. %
  4. % Parameters:
  5. % InputFile: i1 'Input image', required: 'input image'
  6. % InputFile: i2 'Cluster Center image ', required: 'cluster center image'
  7. % InputFile: i3 'Cluster Variance image', required: 'cluster variance image'
  8. % InputFile: i4 'Weight image', required: 'weight image'
  9. % Integer: b 'Input Image Border Width', default: 0: 'Border Width'
  10. % OutputFile: o 'Classified image', required: 'classified image'
  11. %
  12. % Example: o = klrfclass({i1, i2, i3, i4}, {'i1','';'i2','';'i3','';'i4','';'b',0;'o',''})
  13. %
  14. % Khoros helpfile follows below:
  15. %
  16. % PROGRAM
  17. % lrfclass - Classify Image Using the Localized Receptive Field Classifier (K1)
  18. %
  19. % DESCRIPTION
  20. % .I lrfclass
  21. % classifies an image using the Localized Receptive Field classifier (LRF).
  22. % The Localized Receptive Field (LRF) is based on a single layer of
  23. % self-organizing, "localized receptive field" units, followed by a
  24. % single layer perceptron. The single layer of perceptron units use the
  25. % LMS or Adaline learning rule to adjust the weights. The weights are
  26. % adjusted or "trained" using the companion program, "lrftrain". After
  27. % training the weights, using the "lrftrain" program, a number of similar
  28. % images may be quickly classified with this program based on the training
  29. % data set.
  30. % .SH "LRF network theory"
  31. %
  32. % The basic network model of the LRF consists of a two layer topology.
  33. % The first layer of "receptive field" nodes are trained using a clustering
  34. % algorithm, such as K-means, or some other algorithm which can determine
  35. % the receptive field centers. Each node in the first layer computes a
  36. % receptive field response function, which should approach zero as the
  37. % distance from the center of the receptive field is increased. The second
  38. % layer of the LRF model sums the weighted outputs of the first layer,
  39. % which produces the output or response of the network. A supervised
  40. % LMS rule is used to train the weights of the second layer nodes.
  41. %
  42. % The response function of the LRF network is formulated as follows:
  43. % .DS
  44. %
  45. % f(x) = SUM(Ti * Ri(x))
  46. %
  47. % where,
  48. %
  49. % Ri(x) = Q( ||x - xi|| / Wi )
  50. %
  51. % x - is a real valued vector in the input space,
  52. % Ri - is the ith receptive field response function,
  53. % Q - is a radially symmetric function with a single
  54. % maximum at the origin, decreasing to zero at
  55. % large radii,
  56. % xi - is the center of the ith receptive field,
  57. % Wi - is the width of the ith receptive field,
  58. % Ti - is the weight associated with each receptive field.
  59. %
  60. % .DE
  61. %
  62. % The receptive field response functions ( Ri(x) ), should be formulated
  63. % such that they decrease rapidly with increasing radii. This ensures that
  64. % the response functions provide highly localized representations of the
  65. % input space. The response function used in this algorithm is modeled after
  66. % the Gaussian, and uses the trace of the covariance matrix to set the widths
  67. % of the receptive field centers.
  68. %
  69. % Prior to using this algorithm, it is necessary to "train" the weights for
  70. % the output layer by running the companion program, "lrftrain", on a previously
  71. % clustered image. Thus the inputs to this program are the original input
  72. % image (-i1), the cluster center image (-i2), the cluster variance image (-i3),
  73. % and the weight image (-i4). The original input image contains all of the
  74. % features used in the original clustering (ie. vkmeans). The cluster center
  75. % image (-i2) contains the locations of the cluster centers in the input
  76. % feature space, which fixes the centers of the localized receptive fields.
  77. % The cluster variance image (-i3) contains the variances associated with
  78. % each cluster center. This establishes the widths of the localized receptive
  79. % field Gaussians. The weight image (-i4) contains all of the weights for
  80. % each node in the output layer.
  81. %
  82. % The number of receptive field response nodes in the first layer of the
  83. % LRF is determined by the number of cluster centers in the "cluster center"
  84. % image. The number of output classes, and hence the number of output
  85. % nodes in the second (ie. last) layer, is determined by the number of
  86. % desired classes that was specified in the "supervised" classification
  87. % phase of the clustering. This information is contained in the last
  88. % band of the cluster center image. The number of weights in the network
  89. % is determined by the number of receptive field response nodes and the
  90. % number of output nodes. That is,
  91. % .DS
  92. %
  93. % #Wts = (#rf_response_nodes * #output_nodes) + #output_nodes
  94. %
  95. % .DE
  96. %
  97. % The resulting output image is classified with the desired number of
  98. % classes specified in the last band of the "cluster center" (-i2) image.
  99. % The number of desired classes corresponds to the number of output nodes
  100. % in the last layer of the LRF network. This classified image is of
  101. % data storage type INTEGER.
  102. % .SH "Input Options"
  103. %
  104. %
  105. % "-b" 8
  106. % is an integer that specifies the border width, in pixels, encompassing
  107. % the desired region of the image to be classified. This region is ignored
  108. % during the classification process.
  109. %
  110. % This routine was written with the help of and ideas from
  111. % Dr. Don Hush, University of New Mexico, Dept. of EECE.
  112. %
  113. %
  114. %
  115. % EXAMPLES
  116. % lrfclass -i1 feature_image.xv -i2 cluster_centers -i3 variances -i4 weight_image -o classified_image -b 4
  117. %
  118. % This example uses feature_image.xv as the input feature image, and the
  119. % three other images from the companion program "lrftrain". These include
  120. % the cluster_centers image, the variances image, and the weight_image.
  121. % The resulting classified image is stored as "classified_image". A border
  122. % width of 4 pixels is specified, which will cause the outermost four pixels
  123. % of the image to be ignored.
  124. %
  125. % "SEE ALSO"
  126. % lrftrain(1)
  127. %
  128. % RESTRICTIONS
  129. % All input images MUST be of data storage type FLOAT. The resulting
  130. % classified image (-o) is of data storage type INTEGER.
  131. %
  132. % REFERENCES
  133. %
  134. % COPYRIGHT
  135. % Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI") All rights reserved.
  136. %
  137. function varargout = klrfclass(varargin)
  138. if nargin ==0
  139. Inputs={};arglist={'',''};
  140. elseif nargin ==1
  141. Inputs=varargin{1};arglist={'',''};
  142. elseif nargin ==2
  143. Inputs=varargin{1}; arglist=varargin{2};
  144. else error('Usage: [out1,..] = klrfclass(Inputs,arglist).');
  145. end
  146. if size(arglist,2)~=2
  147. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  148. end
  149. narglist={'i1', '__input';'i2', '__input';'i3', '__input';'i4', '__input';'b', 0;'o', '__output'};
  150. maxval={0,0,0,0,100,0};
  151. minval={0,0,0,0,0,0};
  152. istoggle=[0,0,0,0,1,0];
  153. was_set=istoggle * 0;
  154. paramtype={'InputFile','InputFile','InputFile','InputFile','Integer','OutputFile'};
  155. % identify the input arrays and assign them to the arguments as stated by the user
  156. if ~iscell(Inputs)
  157. Inputs = {Inputs};
  158. end
  159. NumReqOutputs=1; nextinput=1; nextoutput=1;
  160. for ii=1:size(arglist,1)
  161. wasmatched=0;
  162. for jj=1:size(narglist,1)
  163. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  164. wasmatched = 1;
  165. was_set(jj) = 1;
  166. if strcmp(narglist{jj,2}, '__input')
  167. if (nextinput > length(Inputs))
  168. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  169. end
  170. narglist{jj,2} = 'OK_in';
  171. nextinput = nextinput + 1;
  172. elseif strcmp(narglist{jj,2}, '__output')
  173. if (nextoutput > nargout)
  174. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  175. end
  176. if (isempty(arglist{ii,2}))
  177. narglist{jj,2} = 'OK_out';
  178. else
  179. narglist{jj,2} = arglist{ii,2};
  180. end
  181. nextoutput = nextoutput + 1;
  182. if (minval{jj} == 0)
  183. NumReqOutputs = NumReqOutputs - 1;
  184. end
  185. elseif isstr(arglist{ii,2})
  186. narglist{jj,2} = arglist{ii,2};
  187. else
  188. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  189. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  190. end
  191. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  192. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  193. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  194. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  195. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  196. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  197. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  198. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  199. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  200. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  201. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  202. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  203. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  204. end
  205. end
  206. end
  207. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  208. narglist{jj,2} = arglist{ii,2};
  209. end
  210. end
  211. end
  212. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  213. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  214. end
  215. end
  216. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  217. for jj=1:size(narglist,1)
  218. if strcmp(paramtype{jj}, 'Toggle')
  219. if (narglist{jj,2} ==0)
  220. narglist{jj,1} = '';
  221. end;
  222. narglist{jj,2} = '';
  223. end;
  224. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  225. narglist{jj,1} = '';
  226. narglist{jj,2} = '';
  227. end;
  228. if strcmp(narglist{jj,2}, '__input')
  229. if (minval{jj} == 0) % meaning this input is required
  230. if (nextinput > size(Inputs))
  231. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  232. else
  233. narglist{jj,2} = 'OK_in';
  234. nextinput = nextinput + 1;
  235. end
  236. else % this is an optional input
  237. if (nextinput <= length(Inputs))
  238. narglist{jj,2} = 'OK_in';
  239. nextinput = nextinput + 1;
  240. else
  241. narglist{jj,1} = '';
  242. narglist{jj,2} = '';
  243. end;
  244. end;
  245. else
  246. if strcmp(narglist{jj,2}, '__output')
  247. if (minval{jj} == 0) % this is a required output
  248. if (nextoutput > nargout & nargout > 1)
  249. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  250. else
  251. narglist{jj,2} = 'OK_out';
  252. nextoutput = nextoutput + 1;
  253. NumReqOutputs = NumReqOutputs-1;
  254. end
  255. else % this is an optional output
  256. if (nargout - nextoutput >= NumReqOutputs)
  257. narglist{jj,2} = 'OK_out';
  258. nextoutput = nextoutput + 1;
  259. else
  260. narglist{jj,1} = '';
  261. narglist{jj,2} = '';
  262. end;
  263. end
  264. end
  265. end
  266. end
  267. if nargout
  268. varargout = cell(1,nargout);
  269. else
  270. varargout = cell(1,1);
  271. end
  272. global KhorosRoot
  273. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  274. w=['"' KhorosRoot];
  275. else
  276. if ispc
  277. w='"C:\Program Files\dip\khorosBin\';
  278. else
  279. [s,w] = system('which cantata');
  280. w=['"' w(1:end-8)];
  281. end
  282. end
  283. [varargout{:}]=callKhoros([w 'lrfclass" '],Inputs,narglist);