PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/matlab_tools/Converted/kccompare.m

http://github.com/aludnam/MATLAB
Objective C | 206 lines | 206 code | 0 blank | 0 comment | 19 complexity | e0cd4799297157bdf7fe01879c03a656 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %kccompare 'Compare two classification results and create confusion matrix'
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros ccompare.pane file
  3. %
  4. % Parameters:
  5. % InputFile: iclass 'Input Classified Image', required: 'input classified image'
  6. % InputFile: itruth 'Input Truth Image', required: 'input truth (correctly classified) image'
  7. % InputFile: icsf 'Class Specification File', required: 'input class specification file'
  8. % MultiChoice: omode 'Output Mode', default: 1: 'output mode toggle'
  9. % Choices are:
  10. % 1: 'Text'
  11. % 2: 'LaTeX'
  12. % 3: 'HTML'
  13. % 4: 'CSV'
  14. % OutputFile: oreport 'Output Report', optional: 'output report file'
  15. %
  16. % Example: oreport = kccompare({iclass, itruth, icsf}, {'iclass','';'itruth','';'icsf','';'omode',1;'oreport',''})
  17. %
  18. % Khoros helpfile follows below:
  19. %
  20. % PROGRAM
  21. % ccompare - Compare two classification results and create confusion matrix
  22. %
  23. % DESCRIPTION
  24. % Compare a classification result (passed with the [-iclass] parameter) with a "ground truth" image (passed with the [-itruth] parameter) and generate a confusion matrix with omission, comission and correct classification. It also requires a Class Specification File to be passed with the [-icsf] parameter (the CSF file will be used to get the classes' names for the report). The format for the Class Specification File is listed in the "cclassreport" kman page.
  25. % The main purpose of this program is to evaluate the classification results by comparing it with an already classified image (classified in the sense of being processed either by another classification program or visually interpreted by the user).
  26. % It does not sound smart - if we have a classification result that can be trusted enough to be called "ground truth", why bother with another classification process ? The answer is that the comparison can be done with small representative regions of the images and if the results are good, the same classifier and parameters can be used for larger regions of the image. The comparison results can also show where there is too many misclassified pixels, hinting that some changes on the choice of classes, regions for signatures or classification method can improve the results.
  27. % The ccompare program uses four data structures to show its results: a confusion matrix, an omission vector, a commission vector and the correct classification percentage, described below:
  28. % The confusion matrix is calculated by how many pixels in the correct class X (from the ground truth image) were classified as classes 0 (rejected), 1, 2...N. The result is a matrix which diagonal shows where the classification result and the ground truth agreed. The final column of this matrix will be a total column, with each line corresponding to a class and showing the total pixels in the ground truth for that class. The final line of the matrix will also be a total line, with each column corresponding to a class and showing the total pixels classified as that class.
  29. % The omission vector is calculated by analyzing for each class its corresponding line on the confusion matrix, and adding all values that are outside the diagonal of the confusion matrix, the comparing it with the total for that line. It means the percentage of pixels of class X that were not assigned to its correct class.
  30. % The commission vector is calculated by analyzing for each class its corresponding column on the confusion matrix, and adding all values that are outside the diagonal of the confusion matrix, the comparing it with the total for that line (the total number of pixels for the ground truth). It means the percentage of pixels of class X that were assigned to other classes than X. Note that the commission error and the omission error represent distinct concepts and their results are not directly related.
  31. % The correct classification statistics are calculated as 100-the values of the omission vector.
  32. % The overall accuracy is calculated by summing all values on the diagonal of the confusion matrix (corresponding to the number of correctly classified pixels, where the classified result and the ground truth agreed) and extracting the percentage compared with the total of pixels in the image.
  33. % Result can be presented in different formats according to the [-omode] output mode parameter:
  34. % * 1: Text ([-omode 1]): an ASCII text table will be created, with the cells in the matrix and vectors evenly spaced.
  35. % * 2: LaTeX table ([-omode 2]): a LaTeX table, complete with caption and label. It can be included in any LaTeX file with the \\input command or read as a block of text using a text editor. The caption for the table will contain the file name of the classification result used for the generation of the report, and should be changed for reports - a reminder of this will appear when you run latex. For references about LaTeX, please refer to the Classify Toolbox Manual.
  36. % * 3: HTML Table ([-omode 3]): will create a table formatted in HTML, including caption, ready for inclusion in HTML documents. The caption should be changed by the user, originally the file name will be used as caption.
  37. % * 4: CSV comma-delimited text ([-omode 4]): create a comma-delimited file (where the fields are separated by commas) which can be read/imported by Excel and other spreadsheet applications. One caveat: if the class names in the Class Specification File has commas on it, the application reading the CSV report will get confused, some manual editing will be necessary.
  38. %
  39. %
  40. %
  41. % EXAMPLES
  42. % All examples for the toolbox Classify are on the file $CLASSIFY/Readme.html. For an example of this program, please see the example workspace Classify:workspaces:KNN.
  43. %
  44. % "SEE ALSO"
  45. % cclassreport, Class Specification File format, described in the Classify Toolbox Manual.
  46. %
  47. % RESTRICTIONS
  48. % The input ground truth must be an unmapped image, with the class indexes corresponding to the class indexes in the classified image. It means that a RGB image \fBcannot" be used as a ground truth without some pre-processing (see below).
  49. % One way to create a ground truth image is to use a graphics editor or any other package to "paint" the image to be classified with the same RGB colors that are specified in the Class Specification File, then unmapping it with the "cunmapdata" kroutine .
  50. %
  51. % REFERENCES
  52. % All references for the toolbox Classify are on the file $CLASSIFY/Readme.html
  53. %
  54. % COPYRIGHT
  55. % Copyright (C) 1997 Rafael Santos. Khoros (C) Khoral Research, Inc.
  56. %
  57. function varargout = kccompare(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,..] = kccompare(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={'iclass', '__input';'itruth', '__input';'icsf', '__input';'omode', 1;'oreport', '__output'};
  70. maxval={0,0,0,0,1};
  71. minval={0,0,0,0,1};
  72. istoggle=[0,0,0,0,1];
  73. was_set=istoggle * 0;
  74. paramtype={'InputFile','InputFile','InputFile','MultiChoice','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=0; 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 'ccompare" '],Inputs,narglist);