PageRenderTime 151ms CodeModel.GetById 67ms RepoModel.GetById 0ms app.codeStats 0ms

/matlab_tools/Converted/kvlabel.m

http://github.com/aludnam/MATLAB
Objective C | 357 lines | 357 code | 0 blank | 0 comment | 9 complexity | 0a4088161ff23ebb8f8a91ac099e9cda MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %kvlabel 'Perform Labeling on Multiband or Cluster Image (K1)'
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros vlabel.pane file
  3. %
  4. % Parameters:
  5. % Integer: w 'Border Width ', default: 0: 'Specify the border width in pixels'
  6. % MultiChoice: m 'Type of Distance Metric (Choose One): ', default: 1: 'Distance Metric: 1 = Euclidean distance, 2 = city block'
  7. % Choices are:
  8. % 1: 'Euclidean '
  9. % 2: 'City Block'
  10. % MultiChoice: c 'Type of Connectivity (Choose One): ', default: 1: 'Connectivity: 1 = 4-connectivity, 2 = 8-connectivity'
  11. % Choices are:
  12. % 1: '4 Connectivity'
  13. % 2: '8 Connectivity'
  14. % Double: s 'Minimum Region Size in % ', default: 1: 'Minimum number of pixel to keep in label region'
  15. % OutputFile: o 'Output Labeled Image', required: 'output image after the labeling process'
  16. % OutputFile: asc 'Output ASCII File', optional: 'Output ASCII file for vlabel result display'
  17. % InputFile: i1 'Initial Image ', optional: 'input image'
  18. % InputFile: i2 'Cluster Center Image', optional: 'input image for cluster centers'
  19. % InputFile: i3 'Cluster Number Image', optional: 'input image for cluster numbers'
  20. % Integer: n 'Approximate Number of Regions Expected ', default: 10: 'Number of region to get from labeling process'
  21. % Double: f 'Split and Merge Factor', default: 0.07: 'Split & Merge Factor: higher values result in finer regions'
  22. %
  23. % Example: [o, asc] = kvlabel({i1, i2, i3}, {'w',0;'m',1;'c',1;'s',1;'o','';'asc','';'i1','';'i2','';'i3','';'n',10;'f',0.07})
  24. %
  25. % Khoros helpfile follows below:
  26. %
  27. % PROGRAM
  28. % vlabel - Perform Labeling on Multiband or Cluster Image (K1)
  29. %
  30. % DESCRIPTION
  31. % .I vlabel
  32. % performs a labeling on a multiband image or a cluster image by
  33. % attempting to merge connected pixels.
  34. %
  35. % The principal of the algorithm is as follows: A pixel receives the same
  36. % label as its neighbor if the likelihood distance between the two pixels
  37. % is acceptable. The label process is propagated for a given region number
  38. % until it is no longer possible to find a candidate.
  39. %
  40. % \fBThree different types of labeling choices exist":
  41. %
  42. % 1
  43. % .I First choice:
  44. % uses a single or multi band image, where the data storage can be
  45. % any of the following types: VFF_TYP_1_BYTE, VFF_TYP_2_BYTE,
  46. % VFF_TYP_4_BYTE, or VFF_TYP_FLOAT.
  47. % The input image corresponds to the -i1 argument.
  48. % The distance is computed using all the bands of the image.
  49. %
  50. % 2.
  51. % .I Second choice:
  52. % uses a cluster number and cluster center
  53. % image obtained from an algorithm like vkmeans or vquant.
  54. % For this case, the cluster center represents the value of a
  55. % class of pixels that have been grouped together.
  56. % Therefore, the distance between two neighbors will be the
  57. % distance between their clusters.
  58. % This case will require less computation time because the algorithm
  59. % will only compute the inter-class distance, instead of computing
  60. % the distance of two neighbors for the entire image.
  61. % An additional advantage with this choice, is that the output from
  62. % algorithms such as vkmeans or vquant may be utilized, which may lead
  63. % to better results.
  64. % The cluster center image corresponds to -i2 argument
  65. % The cluster number image corresponds to -i3 argument in the
  66. % command line.
  67. %
  68. % 3.
  69. % .I The final possibility
  70. % is to use a single or multiband input image
  71. % (argument -i1) associated with a cluster number image (argument -i3).
  72. % The advantage of this choice is that the results of a clustering
  73. % algorithm are used to keep the neighbor pixels that have the same cluster
  74. % number in the same class, and to rely on the distance in the single or
  75. % multi band image to group two neighbors that do not belong to the same
  76. % cluster.
  77. %
  78. % In summary, the three possible choices are:
  79. %
  80. %
  81. % -i1 image.xv
  82. %
  83. %
  84. % -i2 cluster_center.xv -i3 cluster_number.xv
  85. %
  86. %
  87. % -i1 image.xv -i3 cluster_number.xv
  88. %
  89. % .I The algorithm also requires
  90. % the following parameters:
  91. % \fBMetric distance:"
  92. % There are 2 different metric distances that can be used.
  93. %
  94. %
  95. % -d 1 uses Euclidean distance: sqrt[(x-s)^2 + (y-t)^2].
  96. %
  97. %
  98. % -d 2 uses City Block distance: |x-s| + |y-t|.
  99. %
  100. % \fBConnectivity:"
  101. % There are two possible neighborhoods:
  102. %
  103. %
  104. % -c 1 uses the 4 connectivity to link pixels together.
  105. %
  106. %
  107. % -c 2 uses the 8 connectivity.
  108. %
  109. % \fBMinimum size of a region:"
  110. %
  111. %
  112. % -s (float_value) determines the number of pixels required for a region
  113. % to be retained.
  114. % The minimum number of pixels is equal to:
  115. % Total number of pixels in image * float_value / 100.0 (-s corresponds to
  116. % a percentage of the total number of pixels in the image).
  117. %
  118. % \fBBorder Size:"
  119. % Each pixel in the image is updated except those outside of the border.
  120. % The size of the border is specified by the -w argument.
  121. %
  122. % \fBMerging Process:"
  123. % When the labeling process is computed, the user can expect that the small
  124. % rejected region will be merged together in a bigger acceptable region or
  125. % will be included inside another connected region.
  126. % This choice is selected by setting the logical argument -merge to TRUE.
  127. % If -merge is set to FALSE, the small regions will be ignored and labeled as
  128. % an UNDEFINED REGION (label number 0), the same as the border.
  129. %
  130. % "The AUTOMATIC or MANUAL OPTION:\fP
  131. % This option allows the user either to fix a threshold, or to give
  132. % an approximate number of regions.
  133. % If the AUTOMATIC option is used, the algorithm will iterate on the threshold
  134. % until the number of regions labeled by the process is comparable to the number
  135. % of expected regions.
  136. % In fact, if the expected number is not reached after 30 iterations,
  137. % the threshold that gives the closest number of regions is used for
  138. % the final labeling.
  139. %
  140. % Although this option is easy to use, the function:
  141. %
  142. %
  143. % number of regions = F(Threshold) is not a monotonically increasing function,
  144. % and the convergence toward a solution may not exist.
  145. %
  146. %
  147. % -n int_value (AUTOMATIC OPTION) determines the approximate final number of
  148. % regions expected.
  149. % -f float_value (MANUAL OPTION) determines the threshold used by the labeling
  150. % process.
  151. % THESE TWO OPTIONS ARE MUTUALLY EXCLUSIVE
  152. % The default threshold value is 0.07. This value generally gives good results
  153. % on noiseless images with large uniform regions.
  154. % Decreasing this value will increase the number of regions found during the
  155. % labeling process, but these regions will get smaller and could be rejected
  156. % by the minimum size threshold.
  157. % Increasing this value will decrease the number of regions found during the
  158. % labeling process. At the same time, the number of small regions will decrease
  159. % which means that this area of the curve, \fBnumber of regions = F(Threshold)",
  160. % is more stable than the other one.
  161. % Once the user becomes accustomed to this routine, good results are generally
  162. % obtained. One way to become familiar with the routine, is to use the
  163. % automatic option and analyze the output ASCII file (Statistics on the
  164. % iteration process). This file contains the number of regions labeled for
  165. % each iteration, allowing the user to see how the number of regions changes
  166. % as the THRESHOLD is changed.
  167. %
  168. % \fBOutput Files"
  169. %
  170. % 1
  171. % \fBOutput image:"
  172. % The resulting image, which corresponds to the -o1 argument, contains the
  173. % labeled image in which every pixel has a region number as its value. This
  174. % image is of data storage type VFF_TYP_4_BYTE.
  175. % The region label numbers are 1 to N. The region number 0 is reserved
  176. % as an UNDEFINED label or for the border.
  177. %
  178. % 2
  179. % \fBOutput Statistic ASCII File:"
  180. % This file contains all the information relative to the labeling process.
  181. %
  182. %
  183. %
  184. %
  185. % EXAMPLES
  186. %
  187. % vlabel -i1 image.xv -d 1 c 1 -merge 0 -f 0.07 -s 0.7 -w 2 -o image1.xv -asc stats
  188. % This command will label image.xv using the Euclidean distance, the
  189. % 4 connectivity, a split and merge factor equal to 0.07, a minimum
  190. % size for the regions equal to 0.7 percent of the total number of pixels
  191. % in image.xv, and a border size of 2. The merge option is not used which
  192. % means that the small regions will not be labeled.
  193. % The labeled image will be stored in image1.xv and the statistics will be
  194. % written in the ASCII file, stats.
  195. %
  196. % "SEE ALSO"
  197. % lvkmeans(3), lvquant(3), vkmeans(1), vquant(1).
  198. %
  199. % RESTRICTIONS
  200. % vlabel works only with cluster number and cluster center images conforming
  201. % to the convention established by the set of clustering algorithms.
  202. %
  203. % REFERENCES
  204. %
  205. % COPYRIGHT
  206. % Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI") All rights reserved.
  207. %
  208. function varargout = kvlabel(varargin)
  209. if nargin ==0
  210. Inputs={};arglist={'',''};
  211. elseif nargin ==1
  212. Inputs=varargin{1};arglist={'',''};
  213. elseif nargin ==2
  214. Inputs=varargin{1}; arglist=varargin{2};
  215. else error('Usage: [out1,..] = kvlabel(Inputs,arglist).');
  216. end
  217. if size(arglist,2)~=2
  218. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  219. end
  220. narglist={'w', 0;'m', 1;'c', 1;'s', 1;'o', '__output';'asc', '__output';'i1', '__input';'i2', '__input';'i3', '__input';'n', 10;'f', 0.07};
  221. maxval={16,0,0,100,0,1,1,1,1,2500,1};
  222. minval={0,0,0,0,0,1,1,1,1,2,0};
  223. istoggle=[1,0,0,1,0,1,1,1,1,1,1];
  224. was_set=istoggle * 0;
  225. paramtype={'Integer','MultiChoice','MultiChoice','Double','OutputFile','OutputFile','InputFile','InputFile','InputFile','Integer','Double'};
  226. % identify the input arrays and assign them to the arguments as stated by the user
  227. if ~iscell(Inputs)
  228. Inputs = {Inputs};
  229. end
  230. NumReqOutputs=1; nextinput=1; nextoutput=1;
  231. for ii=1:size(arglist,1)
  232. wasmatched=0;
  233. for jj=1:size(narglist,1)
  234. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  235. wasmatched = 1;
  236. was_set(jj) = 1;
  237. if strcmp(narglist{jj,2}, '__input')
  238. if (nextinput > length(Inputs))
  239. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  240. end
  241. narglist{jj,2} = 'OK_in';
  242. nextinput = nextinput + 1;
  243. elseif strcmp(narglist{jj,2}, '__output')
  244. if (nextoutput > nargout)
  245. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  246. end
  247. if (isempty(arglist{ii,2}))
  248. narglist{jj,2} = 'OK_out';
  249. else
  250. narglist{jj,2} = arglist{ii,2};
  251. end
  252. nextoutput = nextoutput + 1;
  253. if (minval{jj} == 0)
  254. NumReqOutputs = NumReqOutputs - 1;
  255. end
  256. elseif isstr(arglist{ii,2})
  257. narglist{jj,2} = arglist{ii,2};
  258. else
  259. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  260. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  261. end
  262. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  263. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  264. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  265. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  266. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  267. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  268. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  269. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  270. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  271. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  272. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  273. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  274. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  275. end
  276. end
  277. end
  278. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  279. narglist{jj,2} = arglist{ii,2};
  280. end
  281. end
  282. end
  283. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  284. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  285. end
  286. end
  287. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  288. for jj=1:size(narglist,1)
  289. if strcmp(paramtype{jj}, 'Toggle')
  290. if (narglist{jj,2} ==0)
  291. narglist{jj,1} = '';
  292. end;
  293. narglist{jj,2} = '';
  294. end;
  295. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  296. narglist{jj,1} = '';
  297. narglist{jj,2} = '';
  298. end;
  299. if strcmp(narglist{jj,2}, '__input')
  300. if (minval{jj} == 0) % meaning this input is required
  301. if (nextinput > size(Inputs))
  302. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  303. else
  304. narglist{jj,2} = 'OK_in';
  305. nextinput = nextinput + 1;
  306. end
  307. else % this is an optional input
  308. if (nextinput <= length(Inputs))
  309. narglist{jj,2} = 'OK_in';
  310. nextinput = nextinput + 1;
  311. else
  312. narglist{jj,1} = '';
  313. narglist{jj,2} = '';
  314. end;
  315. end;
  316. else
  317. if strcmp(narglist{jj,2}, '__output')
  318. if (minval{jj} == 0) % this is a required output
  319. if (nextoutput > nargout & nargout > 1)
  320. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  321. else
  322. narglist{jj,2} = 'OK_out';
  323. nextoutput = nextoutput + 1;
  324. NumReqOutputs = NumReqOutputs-1;
  325. end
  326. else % this is an optional output
  327. if (nargout - nextoutput >= NumReqOutputs)
  328. narglist{jj,2} = 'OK_out';
  329. nextoutput = nextoutput + 1;
  330. else
  331. narglist{jj,1} = '';
  332. narglist{jj,2} = '';
  333. end;
  334. end
  335. end
  336. end
  337. end
  338. if nargout
  339. varargout = cell(1,nargout);
  340. else
  341. varargout = cell(1,1);
  342. end
  343. global KhorosRoot
  344. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  345. w=['"' KhorosRoot];
  346. else
  347. if ispc
  348. w='"C:\Program Files\dip\khorosBin\';
  349. else
  350. [s,w] = system('which cantata');
  351. w=['"' w(1:end-8)];
  352. end
  353. end
  354. [varargout{:}]=callKhoros([w 'vlabel" '],Inputs,narglist);