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

/matlab_tools/Converted/kvwmdd.m

http://github.com/aludnam/MATLAB
Objective C | 394 lines | 394 code | 0 blank | 0 comment | 6 complexity | d421a36b9f21d1d2d6e08f89da7acbab MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %kvwmdd 'Weighted Minimum Distance Detector (K1)'
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros vwmdd.pane file
  3. %
  4. % Parameters:
  5. % InputFile: i1 'Input Image ', required: 'Input Image'
  6. % InputFile: i2 'Input Center/Class Image', required: 'Input Cluster Center/Class Image'
  7. % InputFile: i3 'Input Variance Image ', required: 'Input Variance Image'
  8. % Double: k 'Scaling Factor ', default: 1: ' Scaling factor'
  9. % Integer: b 'Border Width ', default: 0: ' Border width in pixels'
  10. % OutputFile: o 'Output Classified Image ', required: 'Output specifying which vector belongs to which cluster'
  11. %
  12. % Example: o = kvwmdd({i1, i2, i3}, {'i1','';'i2','';'i3','';'k',1;'b',0;'o',''})
  13. %
  14. % Khoros helpfile follows below:
  15. %
  16. % PROGRAM
  17. % vwmdd - Weighted Minimum Distance Detector (K1)
  18. %
  19. % DESCRIPTION
  20. % .I vwmdd
  21. % is an Weighted minimum distance detector or Classifier.
  22. % The main idea behind this detector is that it will distinguish
  23. % a single class form the rest of the data. However, it will
  24. % also work to distinguish multiple classes from each other.
  25. % .SH Theory
  26. %
  27. % To classify or detect spatial patterns in an image, we must
  28. % have a template to compare against. Creating a template can be thought
  29. % of as training on representative (ideal) data. This can be done
  30. % by using a clustering algorithm on the representative data.
  31. % Clustering routines such as vkmeans, vquant, isodata, etc can
  32. % be used to create a template. The idea here is to over
  33. % cluster the representative data, and quantize that into k classes.
  34. % Pseudo color in editimage, and kasc2val can be used to
  35. % create a class image. When the clustering is performed, two
  36. % items need to be computed; (1) the cluster center values, and
  37. % (2) the variances for each axis in the cluster. The variances
  38. % can be obtained by looking at the diagonal of the covariance
  39. % matrix.
  40. %
  41. % Explained below is a method of creating a the class assignments for
  42. % each cluster center. The class assignments may, for example, quantize
  43. % the space from 12 clusters to 2 classes. Example
  44. %
  45. % \f(CW
  46. %
  47. % Cluster Center Class
  48. % 1 --------------------- 2
  49. % 2 --------------------- 2
  50. % 3 --------------------- 1
  51. % 4 --------------------- 2
  52. % .
  53. % .
  54. % .
  55. % 12 --------------------- 1
  56. %
  57. %
  58. % "
  59. %
  60. %
  61. % This routine expects an image that contains the diagonal terms of the
  62. % covariance matrix, called the variance image, and an image that
  63. % contains the cluster center values and class assignment for
  64. % each cluster center. And of course the image to be classified.
  65. % This detector is to approximate the following equation:
  66. %
  67. % \f(CW
  68. %
  69. % (X - Mi)~ Inv(C) (X - Mi) (1)
  70. %
  71. % "
  72. %
  73. % where Mi is the mean, inv(C) is the inverse of the Covariance matrix,
  74. % and X is the data points, and ~ denotes transpose. This can be written as
  75. %
  76. % \f(CW
  77. %
  78. % (d1 d2 ... dn)~ - Q1 - (d1 d2 ... dn) (2)
  79. % | Q2 |
  80. % | . 0 |
  81. % | . |
  82. % | 0 . |
  83. % | Qn |
  84. % - -
  85. %
  86. % Which equals
  87. %
  88. % sq(d1) sq(d2) sq(dn)
  89. % ------ + ------ + ..... + ------ (3)
  90. % Q1 Q2 Qn
  91. %
  92. % Notation:
  93. % sq => square
  94. % Qi => the ith variance
  95. % di => the ith (X - Mi) value
  96. % the matrix is actually the inverse of C
  97. %
  98. % "
  99. %
  100. % Since the inverse of the Covariance matrix can not be easily determined,
  101. % we only consider the diagonal terms or simply the variance terms.
  102. %
  103. % There are two methods for detecting classes in the input image. Each data
  104. % point is put through a test based on the ideas above. The detector works
  105. % in two modes as follows:
  106. %
  107. % (1) uses the summed method -s option set to yes. This is an approximation
  108. % to method (2).
  109. %
  110. % \f(CW
  111. %
  112. % sq( || X - Mi || ) <
  113. % Vi = ------------------ > 1
  114. % K x sq(Si)
  115. %
  116. % where: sq( || X - Mi || ) = sq(di)
  117. % = Euclidean Distance Squared
  118. % sq(Si) = trace(C)
  119. % the trace of C is the sum of the
  120. % diagonal terms (variances).
  121. % K is a Constant
  122. % "
  123. %
  124. %
  125. % (2) non summed method (default).
  126. %
  127. % \f(CW
  128. %
  129. % sq(d1) sq(d2) sq(dn) <
  130. % Vi = ------ + ------ + ..... + ------ > 1
  131. % Q1 x K Q2 x K Qn x K
  132. %
  133. % where: sq(dj) = sq( || xj - Mi || )
  134. % = Euclidean Distance Squared of the
  135. % jth element of the vector X.
  136. % Qj = the jth variance element of the variance
  137. % vector for the ith cluster center.
  138. % K = Constant
  139. %
  140. % "
  141. %
  142. % In both cases the constant K is used. This is used to adjust the
  143. % variance value(s). If K is large, Qi can increase such that
  144. % Vi is always less than 1. There are no specific rules as to
  145. % the optimal value for K.
  146. %
  147. % The data point X is put through the test for each cluster, the
  148. % data point X is assigned to a Class based on the following criteria:
  149. %
  150. % 1.
  151. % choose the smallest Vi value, where Vi is the result for cluster i.
  152. %
  153. % 2.
  154. % If Vi is greater than 1, assign X to the NULL class (NULL class is always 0).
  155. % Otherwise assign Vi to class that corresponds to the ith cluster.
  156. %
  157. % This routine accepts three images as the
  158. % input. The image that corresponds to the -i1 argument is the image
  159. % that needs to be classified. This image can be have as many data
  160. % bands as necessary. The number of data bands depicts the dimensionality
  161. % of the vectors. Each pixel in the image is a vector with dimensionality
  162. % of the number of data bands.
  163. %
  164. % The second input image corresponds to the -i2 argument and is
  165. % the prototype image. This image contains two pieces of information.
  166. % (1) the cluster center values for each cluster center, and (2) the
  167. % class assignments for each cluster center.
  168. % The last data band of this image contains the class assignments for
  169. % each cluster center value. This image must contain the same
  170. % number of data bands as the other input image plus an extra data
  171. % band that represents the class mapping. This
  172. % image would most likely have been created by vkmeans or some other
  173. % routine that will give cluster centers. This image contains vectors that
  174. % correspond to the prototype of each class.
  175. %
  176. % As stated above the center image's last data band is a class data band. The
  177. % class data band simply maps each cluster center vector to a final class.
  178. %
  179. % At this point most class images must be created manually.
  180. % A class image can be created by using pseudo color in editimage
  181. % on the resulting clustered image from the
  182. % vkmeans routine. Vcustom can then be used to create the class image. Finally
  183. % vinset can be used to add the class image data to the center image. Normally
  184. % one would over cluster using vkmeans then reduce the space down. So vkmeans
  185. % might produce 100 clusters but, really only 3 classes are desired. Pseudo
  186. % color in editimage and kasc2val can be used to reduce the space down and
  187. % create a class image. The class data might map cluster center vectors 1-50
  188. % to class 1, vectors 51-60 to class 2 and vectors 61-100 to class 3.
  189. % When the vwmdd routine is run, the result would be a classified image of
  190. % three classes.
  191. %
  192. % The third input image corresponds to the -i3 argument. This image
  193. % contains the variance values for each cluster center. This image
  194. % contains the same number of data bands as the input image (-i1 image), and
  195. % contains the same number of vectors as the center image (-i2 image).
  196. % Each element in the variance vector is the variance value along an
  197. % axis in the n-dimensional space. The variance values can be
  198. % found by computing the diagonal of the covariance matrix for each
  199. % cluster. The cluster center values would be used as the mean values.
  200. % Note, vkmeans will compute this image for you.
  201. %
  202. % The scale factor (-k) must be chosen by trial and error. It has been
  203. % found that if the summed method is being used a small (1.0 - 3.0) value
  204. % for k is sufficient. If the average method is not being used a larger
  205. % k (8.0 - 10.0) value is sufficient. Note, this may change based on the
  206. % data, so these ranges are only a starting point.
  207. %
  208. % The border option (-b) allows the user to specify a border width,
  209. % in pixels, encompassing the image. The border region is skipped
  210. % by vwmdd when classification is performed.
  211. % This useful if neighborhood operators have been used
  212. % previously and have not updated edge pixels.
  213. %
  214. % All input images must be of data storage type FLOAT.
  215. %
  216. % This routine was written with the help of and ideas from
  217. % Dr. Don Hush, University of New Mexico, Dept. of EECE.
  218. %
  219. %
  220. %
  221. % EXAMPLES
  222. % .begin code
  223. % vwmdd -i1 aerial_image.xv -i2 centers_class -i3 variance -b 5 -k 8.0 -o test
  224. % .end code
  225. %
  226. % aerial_image is a 5 band image; thus, each vector has
  227. % dimensionality of 5, centers_class image is a 6 band image,
  228. % 5 bands that contain the cluster center values and 1
  229. % class band. The border that vwmdd will ignore is 5
  230. % pixels wide. The output image will be a single band image called
  231. % test. The scalar to multiply the denominator by is 8.0.
  232. % By default the non-summing method is used.
  233. %
  234. % "SEE ALSO"
  235. % vkmeans(1)
  236. %
  237. % RESTRICTIONS
  238. % All input images must be of data storage type FLOAT.
  239. %
  240. % REFERENCES
  241. %
  242. % COPYRIGHT
  243. % Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI") All rights reserved.
  244. %
  245. function varargout = kvwmdd(varargin)
  246. if nargin ==0
  247. Inputs={};arglist={'',''};
  248. elseif nargin ==1
  249. Inputs=varargin{1};arglist={'',''};
  250. elseif nargin ==2
  251. Inputs=varargin{1}; arglist=varargin{2};
  252. else error('Usage: [out1,..] = kvwmdd(Inputs,arglist).');
  253. end
  254. if size(arglist,2)~=2
  255. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  256. end
  257. narglist={'i1', '__input';'i2', '__input';'i3', '__input';'k', 1;'b', 0;'o', '__output'};
  258. maxval={0,0,0,2,100,0};
  259. minval={0,0,0,2,0,0};
  260. istoggle=[0,0,0,1,1,0];
  261. was_set=istoggle * 0;
  262. paramtype={'InputFile','InputFile','InputFile','Double','Integer','OutputFile'};
  263. % identify the input arrays and assign them to the arguments as stated by the user
  264. if ~iscell(Inputs)
  265. Inputs = {Inputs};
  266. end
  267. NumReqOutputs=1; nextinput=1; nextoutput=1;
  268. for ii=1:size(arglist,1)
  269. wasmatched=0;
  270. for jj=1:size(narglist,1)
  271. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  272. wasmatched = 1;
  273. was_set(jj) = 1;
  274. if strcmp(narglist{jj,2}, '__input')
  275. if (nextinput > length(Inputs))
  276. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  277. end
  278. narglist{jj,2} = 'OK_in';
  279. nextinput = nextinput + 1;
  280. elseif strcmp(narglist{jj,2}, '__output')
  281. if (nextoutput > nargout)
  282. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  283. end
  284. if (isempty(arglist{ii,2}))
  285. narglist{jj,2} = 'OK_out';
  286. else
  287. narglist{jj,2} = arglist{ii,2};
  288. end
  289. nextoutput = nextoutput + 1;
  290. if (minval{jj} == 0)
  291. NumReqOutputs = NumReqOutputs - 1;
  292. end
  293. elseif isstr(arglist{ii,2})
  294. narglist{jj,2} = arglist{ii,2};
  295. else
  296. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  297. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  298. end
  299. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  300. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  301. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  302. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  303. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  304. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  305. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  306. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  307. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  308. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  309. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  310. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  311. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  312. end
  313. end
  314. end
  315. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  316. narglist{jj,2} = arglist{ii,2};
  317. end
  318. end
  319. end
  320. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  321. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  322. end
  323. end
  324. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  325. for jj=1:size(narglist,1)
  326. if strcmp(paramtype{jj}, 'Toggle')
  327. if (narglist{jj,2} ==0)
  328. narglist{jj,1} = '';
  329. end;
  330. narglist{jj,2} = '';
  331. end;
  332. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  333. narglist{jj,1} = '';
  334. narglist{jj,2} = '';
  335. end;
  336. if strcmp(narglist{jj,2}, '__input')
  337. if (minval{jj} == 0) % meaning this input is required
  338. if (nextinput > size(Inputs))
  339. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  340. else
  341. narglist{jj,2} = 'OK_in';
  342. nextinput = nextinput + 1;
  343. end
  344. else % this is an optional input
  345. if (nextinput <= length(Inputs))
  346. narglist{jj,2} = 'OK_in';
  347. nextinput = nextinput + 1;
  348. else
  349. narglist{jj,1} = '';
  350. narglist{jj,2} = '';
  351. end;
  352. end;
  353. else
  354. if strcmp(narglist{jj,2}, '__output')
  355. if (minval{jj} == 0) % this is a required output
  356. if (nextoutput > nargout & nargout > 1)
  357. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  358. else
  359. narglist{jj,2} = 'OK_out';
  360. nextoutput = nextoutput + 1;
  361. NumReqOutputs = NumReqOutputs-1;
  362. end
  363. else % this is an optional output
  364. if (nargout - nextoutput >= NumReqOutputs)
  365. narglist{jj,2} = 'OK_out';
  366. nextoutput = nextoutput + 1;
  367. else
  368. narglist{jj,1} = '';
  369. narglist{jj,2} = '';
  370. end;
  371. end
  372. end
  373. end
  374. end
  375. if nargout
  376. varargout = cell(1,nargout);
  377. else
  378. varargout = cell(1,1);
  379. end
  380. global KhorosRoot
  381. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  382. w=['"' KhorosRoot];
  383. else
  384. if ispc
  385. w='"C:\Program Files\dip\khorosBin\';
  386. else
  387. [s,w] = system('which cantata');
  388. w=['"' w(1:end-8)];
  389. end
  390. end
  391. [varargout{:}]=callKhoros([w 'vwmdd" '],Inputs,narglist);