PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/matlab_tools/Converted/kkclip.m

http://github.com/aludnam/MATLAB
Objective C | 391 lines | 388 code | 3 blank | 0 comment | 55 complexity | a533c7bee02f6bb697473a554a01485a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %kkclip 'Clip or Threshold the Range of Values in Object'
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros kclip.pane file
  3. %
  4. % Parameters:
  5. % InputFile: i 'Input ', required: 'Input file'
  6. % OutputFile: o ' Output ', required: 'output file'
  7. % InputFile: ilc 'Lower Cutoff Values File', optional: 'File containing lower cutoff values'
  8. % InputFile: iuc 'Upper Cutoff Values File', optional: 'File containing upper cutoff values'
  9. %
  10. % Example: o = kkclip({i, ilc, iuc}, {'i','';'o','';'ilc','';'iuc',''})
  11. %
  12. % Khoros helpfile follows below:
  13. %
  14. % PROGRAM
  15. % kclip - Clip or Threshold the Range of Values in Object
  16. %
  17. % DESCRIPTION
  18. % .I kclip
  19. % performs clipping and thresholding operations on the input data
  20. % object (i). Clipping limits the range of values in the data object, and
  21. % thresholding generates a two-level output by thresholding the values in
  22. % the object. For both operations, upper and lower cutoff values can be
  23. % specified as either single constant values (lc, uc), or sets of values
  24. % passed in as data objects (ilc, iuc). Output levels can be set using
  25. % the true and false value options (tval, fval). The data type of the
  26. % output object will be the same as the input object data type.
  27. %
  28. % Clipping
  29. % When clipping is enabled (-thresh 0), the values in the object are
  30. % clipped within the range specified by the upper and/or lower cutoff limits.
  31. % In the default mode (where neither fval nor tval are selected) data values
  32. % above the upper cutoff are set equal to the upper cutoff, and values below
  33. % the lower cutoff are set equal to the lower cutoff value. If the fval
  34. % parameter is provided, then data values below the lower cutoff are set to
  35. % fval. If the tval parameter is provided, data values greater than the upper
  36. % cutoff are set to that value. Exception: any time that both the upper and
  37. % lower cutoff values are provided, and the upper cutoff is less than the
  38. % lower cutoff, then data values between the two cutoffs are always set
  39. % equal to fval.
  40. %
  41. % Thresholding
  42. % If thresholding is enabled (-thresh 1), thresholding is performed using
  43. % the specified lower and upper cutoff values, creating a two-level output
  44. % data object. It is not necessary to specify both of the upper and lower
  45. % cutoff values. If only the lower cutoff is specified, all values above it
  46. % are in the threshold range. If only the upper cutoff is
  47. % specified, all values below it are in the threshold range.
  48. % The true value (tval) is assigned to values that are within the threshold
  49. % range, and all values outside of the threshold range are set to fval.
  50. %
  51. % The following four diagrams illustrate how the given operation is evaluated
  52. % for different combinations of upper and lower cutoffs. The plots
  53. % are example data histograms. Clipping and threshold output assignment
  54. % rules are given following the diagrams.
  55. % .KS
  56. %
  57. % "\fBLower Cutoff Only:"" 5
  58. %
  59. %
  60. % \f(CW
  61. % lower cutoff result: |<-F->|<----T---->|
  62. % | | : . |
  63. % | .:.:. .::: |
  64. % |:. .::::::.::::: |
  65. % |::::::::::::::::.|
  66. % ------|------------
  67. % lower cutoff (lc)
  68. %
  69. % if value < lc cutoff result = False
  70. % if value >= lc cutoff result = True
  71. % "
  72. %
  73. % .KE
  74. % .KS
  75. %
  76. % "\fBUpper Cutoff Only:"" 5
  77. %
  78. %
  79. % \f(CW
  80. % upper cutoff result: |<----T---->|<-F->|
  81. % | : | . |
  82. % | .:.:. .::: |
  83. % |:. .::::::.::::: |
  84. % |::::::::::::::::.|
  85. % ------------|------
  86. % upper cutoff (uc)
  87. %
  88. % if value > uc cutoff result = False
  89. % if value <= uc cutoff result = True
  90. % "
  91. %
  92. % .KE
  93. % .KS
  94. %
  95. % "\fBUpper Cutoff is Greater than Lower Cutoff:"" 5
  96. % When both upper and lower cutoff values are specified, and the upper cutoff
  97. % is greater than the lower cutoff, the combined cutoff result is the logical
  98. % AND of the individual results.
  99. %
  100. %
  101. % \f(CW
  102. % lower cutoff result: |<-F->|<----T---->|
  103. % upper cutoff result: |<----T---->|<-F->|
  104. %
  105. % combined cutoff result: |<-F->|<-T->|<-F->|
  106. % | | : | . |
  107. % | .:.:. .::: |
  108. % |:. .::::::.::::: |
  109. % |::::::::::::::::.|
  110. % ------|-----|------
  111. % lc uc
  112. %
  113. % if value < lc OR value > uc cutoff result = False
  114. % if value >= lc AND value <= uc cutoff result = True
  115. % "
  116. %
  117. % .KE
  118. % .KS
  119. %
  120. % "\fBUpper Cutoff is Less than Lower Cutoff:"" 5
  121. % When both upper and lower cutoff values are specified, but the upper cutoff
  122. % is less than the lower cutoff, the combined cutoff result is the logical
  123. % OR of the individual results. (Specifying the lower cutoff to be greater
  124. % than the upper cutoff inverts the operation.)
  125. % In this case, the false value assigned to the output will always be fval
  126. % for both clipping and thresholding.
  127. %
  128. %
  129. % \f(CW
  130. % upper cutoff result: |<-T->|<----F---->|
  131. % lower cutoff result: |<----F---->|<-T->|
  132. %
  133. % combined cutoff result: |<-T->|<-F->|<-T->|
  134. % | | : | . |
  135. % | .:.:. .::: |
  136. % |:. .::::::.::::: |
  137. % |::::::::::::::::.|
  138. % ------|-----|------
  139. % uc lc
  140. %
  141. % if value < lc AND value > uc cutoff result = False
  142. % if value >= lc OR value <= uc cutoff result = True
  143. % "
  144. %
  145. % .KE
  146. %
  147. % Clipping results are assigned according to the following rules. ("Lower
  148. % result" and "upper result" are either the clipping values or fval and
  149. % tval - see discussion above.)
  150. %
  151. % "1. When lower cutoff < upper cutoff" 5
  152. % If the data value is less than the lower cutoff, set the
  153. % output value to "lower result".
  154. % If the data value is greater than the upper cutoff, set
  155. % the output value to "upper result".
  156. % Otherwise, the data value remains the same.
  157. %
  158. % "2. When upper cutoff < lower cutoff" 5
  159. % If the data value is less than the upper cutoff and
  160. % it is greater than the lower cutoff, set output
  161. % value to the "lower result".
  162. % Otherwise, the data value remains the same.
  163. %
  164. % Thresholding results are assigned according to the following rules.
  165. %
  166. % "1. When lower cutoff < upper cutoff" 5
  167. % If the data value is less than the lower cutoff, or
  168. % it is greater than the upper cutoff, set the output
  169. % value to fval.
  170. % Otherwise, set the output value to tval.
  171. %
  172. % "2. When upper cutoff < lower cutoff" 5
  173. % If the data value is less than the lower cutoff and
  174. % it is greater than the upper cutoff, set output
  175. % value to fval.
  176. % Otherwise, set the output value to tval.
  177. %
  178. % When upper and lower cutoff values are equal, the only data values that
  179. % are evaluated as TRUE are those that are equal to the cutoff values.
  180. %
  181. % "Data Type" 5
  182. % .cI $DATAMANIP/repos/shared/man/sections/value_type_1input
  183. %
  184. % "Data Objects of Different Sizes" 5
  185. % If, in addition to the input data object (i), upper or lower cutoff
  186. % objects (ilc, iuc) are supplied,
  187. % the source objects can be of different sizes. If the sizes are different,
  188. % the size of the destination object's data will be the the same as that of
  189. % the input data object. When resizing the cutoff objects, the data is
  190. % zero padded, or truncated to the size of the input object.
  191. % Alignment between the input files will always be the upper-left hand corner,
  192. % position (0,0,0,0,0).
  193. %
  194. % "Map Data - Single Input" 5
  195. % .cI $DATAMANIP/repos/shared/man/sections/map_1input
  196. %
  197. % "Map Data - Multiple Input Objects" 5
  198. % .cI $DATAMANIP/repos/shared/man/sections/map_2input
  199. %
  200. % "Validity Mask - Single Input" 5
  201. % .cI $DATAMANIP/repos/shared/man/sections/mask_1input
  202. %
  203. % "Validity Mask - Multiple Input Objects" 5
  204. % .cI $DATAMANIP/repos/shared/man/sections/mask_2input
  205. %
  206. % "Explicit Location and Time Data - Single Input" 5
  207. % .cI $DATAMANIP/repos/shared/man/sections/loc_and_time_1input
  208. %
  209. % "Explicit Location and Time Data - Two Input Objects" 5
  210. % .cI $DATAMANIP/repos/shared/man/sections/loc_and_time_2input
  211. %
  212. % "Failure Modes - Single Input"
  213. % .cI $DATAMANIP/repos/shared/man/sections/fail_1input
  214. %
  215. % "Failure Modes - Two Input Objects"
  216. % .cI $DATAMANIP/repos/shared/man/sections/fail_2input
  217. %
  218. %
  219. %
  220. % EXAMPLES
  221. % kclip -thresh 1 -i image:ball -lc 128 -uc 202 -tval 12 -fval 0 -o thresh.out
  222. %
  223. % causes all pixels between the values on 128 and 202 (inclusively)
  224. % to have a value of 12 in the output image. All other output image pixels
  225. % have the value of zero.
  226. % kclip -i full_range.viff -uc 123 -lc 17 -o restricted_range.viff
  227. % Limits the range of values in the input file full_range.viff to 17...123
  228. % and writes the result to restricted_range.viff.
  229. %
  230. % "SEE ALSO"
  231. % kbitwise
  232. %
  233. % RESTRICTIONS
  234. % This program has not yet been updated to completely support the
  235. % polymorphic data model.
  236. %
  237. % REFERENCES
  238. %
  239. % COPYRIGHT
  240. % Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI") All rights reserved.
  241. %
  242. function varargout = kkclip(varargin)
  243. if nargin ==0
  244. Inputs={};arglist={'',''};
  245. elseif nargin ==1
  246. Inputs=varargin{1};arglist={'',''};
  247. elseif nargin ==2
  248. Inputs=varargin{1}; arglist=varargin{2};
  249. else error('Usage: [out1,..] = kkclip(Inputs,arglist).');
  250. end
  251. if size(arglist,2)~=2
  252. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  253. end
  254. narglist={'i', '__input';'o', '__output';'ilc', '__input';'iuc', '__input'};
  255. maxval={0,0,1,1};
  256. minval={0,0,1,1};
  257. istoggle=[0,0,1,1];
  258. was_set=istoggle * 0;
  259. paramtype={'InputFile','OutputFile','InputFile','InputFile'};
  260. % identify the input arrays and assign them to the arguments as stated by the user
  261. if ~iscell(Inputs)
  262. Inputs = {Inputs};
  263. end
  264. NumReqOutputs=1; nextinput=1; nextoutput=1;
  265. for ii=1:size(arglist,1)
  266. wasmatched=0;
  267. for jj=1:size(narglist,1)
  268. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  269. wasmatched = 1;
  270. was_set(jj) = 1;
  271. if strcmp(narglist{jj,2}, '__input')
  272. if (nextinput > length(Inputs))
  273. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  274. end
  275. narglist{jj,2} = 'OK_in';
  276. nextinput = nextinput + 1;
  277. elseif strcmp(narglist{jj,2}, '__output')
  278. if (nextoutput > nargout)
  279. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  280. end
  281. if (isempty(arglist{ii,2}))
  282. narglist{jj,2} = 'OK_out';
  283. else
  284. narglist{jj,2} = arglist{ii,2};
  285. end
  286. nextoutput = nextoutput + 1;
  287. if (minval{jj} == 0)
  288. NumReqOutputs = NumReqOutputs - 1;
  289. end
  290. elseif isstr(arglist{ii,2})
  291. narglist{jj,2} = arglist{ii,2};
  292. else
  293. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  294. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  295. end
  296. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  297. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  298. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  299. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  300. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  301. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  302. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  303. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  304. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  305. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  306. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  307. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  308. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  309. end
  310. end
  311. end
  312. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  313. narglist{jj,2} = arglist{ii,2};
  314. end
  315. end
  316. end
  317. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  318. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  319. end
  320. end
  321. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  322. for jj=1:size(narglist,1)
  323. if strcmp(paramtype{jj}, 'Toggle')
  324. if (narglist{jj,2} ==0)
  325. narglist{jj,1} = '';
  326. end;
  327. narglist{jj,2} = '';
  328. end;
  329. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  330. narglist{jj,1} = '';
  331. narglist{jj,2} = '';
  332. end;
  333. if strcmp(narglist{jj,2}, '__input')
  334. if (minval{jj} == 0) % meaning this input is required
  335. if (nextinput > size(Inputs))
  336. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  337. else
  338. narglist{jj,2} = 'OK_in';
  339. nextinput = nextinput + 1;
  340. end
  341. else % this is an optional input
  342. if (nextinput <= length(Inputs))
  343. narglist{jj,2} = 'OK_in';
  344. nextinput = nextinput + 1;
  345. else
  346. narglist{jj,1} = '';
  347. narglist{jj,2} = '';
  348. end;
  349. end;
  350. else
  351. if strcmp(narglist{jj,2}, '__output')
  352. if (minval{jj} == 0) % this is a required output
  353. if (nextoutput > nargout & nargout > 1)
  354. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  355. else
  356. narglist{jj,2} = 'OK_out';
  357. nextoutput = nextoutput + 1;
  358. NumReqOutputs = NumReqOutputs-1;
  359. end
  360. else % this is an optional output
  361. if (nargout - nextoutput >= NumReqOutputs)
  362. narglist{jj,2} = 'OK_out';
  363. nextoutput = nextoutput + 1;
  364. else
  365. narglist{jj,1} = '';
  366. narglist{jj,2} = '';
  367. end;
  368. end
  369. end
  370. end
  371. end
  372. if nargout
  373. varargout = cell(1,nargout);
  374. else
  375. varargout = cell(1,1);
  376. end
  377. global KhorosRoot
  378. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  379. w=['"' KhorosRoot];
  380. else
  381. if ispc
  382. w='"C:\Program Files\dip\khorosBin\';
  383. else
  384. [s,w] = system('which cantata');
  385. w=['"' w(1:end-8)];
  386. end
  387. end
  388. [varargout{:}]=callKhoros([w 'kclip" '],Inputs,narglist);