%kextractor 'Interactive image Region of Interest (ROI) extraction'
% This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros extractor.pane file
%
% Parameters: 
% InputFile: i 'Input File', required: 'File containing image from which to extract ROI'
% OutputFile: o 'Output ROI ', optional: 'Output image in which to save ROI'
% MultiChoice: shape 'ROI Shape:', default: 1: 'Desired shape for ROI'
%    Choices are:
%   1: 'Rectangle'
%   2: 'Polyline'
%   3: 'Circle'
%   4: 'Ellipse'
%   5: 'Line'
%   6: 'Freehand'
% Integer: x 'X Placement', default: -1: 'X location for GUI autoplacement'
% Integer: y 'Y Placement', default: -1: 'Y location for GUI autoplacement'
% Double: update 'Update time', default: 1: 'How often to check input file for modification'
%
% Example: o = kextractor(i, {'i','';'o','';'shape',1;'x',-1;'y',-1;'update',1})
%
% Khoros helpfile follows below:
%
%  PROGRAM
% extractor - Interactive image Region of Interest (ROI) extraction
%
%  DESCRIPTION
% 
% The extractor program allows you to display an image, and interactively
% extract regions of interest (ROI's) from it.  The region of interest shapes 
% supported include rectangle, line, polyline, signal, circle, curve, ellipse, 
% and freehand.  The region of interest extracted may be defined by the region 
% inside the shape, by the region outside the shape, or by the outline of the 
% shape.  When a region of interest is extracted, it may be written to an output 
% file, displayed in a separate window, or both.
% 
% When running extractor, you must provide an input image.  If desired, you may
% specify the shape to be used in ROI extraction using the [-shape] argument.
% The [-disp] argument may be used to specify whether the extracted ROI is 
% automatically displayed in a separate window.  The [-o] argument may be used
% to specify an output file.  Note that the extracted ROI may be displayed in
% a separate window, written to an output file, or both.  The ROI Extraction
% Policy dictates whether the region of interest is to be specified by the
% area inside the shape, by the area outside the shape, or by the outline of the
% shape; this can be specified using the [-policy] argument. 
% 
% For all ROI extraction, you will first indicate the desired region of interest 
% by drawing the desired shape in the input image.  For all ROI shapes except 
% signal (for which it does not apply) a mask is employed to obscure the pixels 
% which are not inside the region of interest before the ROI is written to disk 
% or displayed.  How the mask is applied depends on the ROI Extraction Policy.  ROI's may be
% defined by the area inside the shape drawn, the area outside the shape drawn,
% or by the outline of the shape drawn.
% 
% Once the input image is displayed, the desired ROI shape is specified, the
% desired ROI Extraction Policy is given, and an optional output filename is 
% entered (all of which can be done using the arguments to extractor) you 
% may immediately begin the ROI extraction process. The procedure used to 
% specify the region of interest in the input image will differ depending on 
% the ROI shape being used. The following is a summary of the available ROI 
% shapes and the procedure used for the extraction of each.
% 
% 
%  "Rectangle"
% The region of interest will be defined by a simple rectangle. For an
% ROI Extraction policy of "Inside Shape", the subimage defined by the rectangle
% is extracted.  For an ROI Extraction policy of "Shape Outline", the bounding
% box of the rectangle is calculated, and all pixel values inside the bounding
% box but not on the outline of the rectangle are masked out.  For an ROI 
% Extraction policy of "Outside Shape", the pixel values within the rectangle
% are masked out, while the remainder of the image is preserved.
% 
% 
% To extract a rectangular region of interest, rubberband the rectangle 
% surrounding the region of interest in the input image.  To rubberband the 
% rectangle, click the mouse button on a corner of the desired region of interest.
% Holding the mouse button down, move the mouse to the opposite corner of the 
% desired rectangle.  The rectangle will refreshed as the mouse moves, or 
% "rubberbanded" after the mouse.
% 
% 
%  "Line"
% The region of interest will be defined by a line.  For ROI Extraction Policies 
% of "Inside Shape" and "Shape Outline", the bounding box is calculated from the
% endpoints of the line and all pixel values within the bounding box but not on 
% the line are masked out.  For an ROI Extraction Policy of "Outside Shape", 
% all pixels on the line itself are masked out, while the rest of the image is
% preserved.  
% 
% 
% To extract a line region of interest, rubberband the line defining 
% the region of interest in the input image.  To rubberband the line, click the 
% mouse button on one endpoint of the desired region of interest.  Holding the 
% mouse button down, move the mouse to the other endpoint of the line. 
% 
% 
%  "Signal"
% The region of interest will be defined by a line; however, rather than 
% computing the bounding box of the line and masking out all unwanted pixels,
% the pixel values on the line are written out "alone" to the output image.
% Thus, the extraction of signal ROI's is a useful way of creating VIFF images
% containing single lines of image data that can be used in a 2D plot.  The
% ROI Extraction Policy does not apply to the signal ROI.
% 
% 
% Extraction of a signal is identical to extraction of a line (see above).
% 
% 
%  "Polyline"
% The region of interest will be defined by a polyline. For ROI Extraction 
% Policy of "Inside Shape", the bounding box is calculated from the vertices 
% of the polyline and all pixel values within the bounding box but not inside
% the polygon are masked out. For an ROI Extraction policy of "Shape Outline",
% the bounding box is calculated from the vertices of the polyline and all pixel 
% values not on the polyline itself are masked out.  For ROI Extraction policy of 
% "Outside Shape", all pixels within the polyline are masked out, while the 
% rest of the image is preserved.  
% 
%  
% To extract a polyline region of interest, begin by clicking the mouse on 
% one vertex of the desired polyline. As you move the mouse, a line will be 
% rubberbanded after it.  Continue to button click on each successive vertex, 
% until the polyline appears as desired; double click to end the polyline.  
% The first and last vertices of the polyline will be joined in order to define 
% a closed region of interest.
% 
% 
%  "Circle"
% The region of interest will be defined by a circle. For ROI Extraction 
% Policy of "Inside Shape", the bounding box is calculated from the dimensions
% of the circle and all pixel values within the bounding box but not inside
% the circle are masked out. For an ROI Extraction policy of "Shape Outline",
% the bounding box is calculated from the vertices of the polyline and all pixel 
% values not on the circle itself are masked out.  For ROI Extraction policy of 
% "Outside Shape", all pixels within the circle are masked out, while the 
% rest of the image is preserved.
% 
% 
% To extract a circular region of interest, rubberband the circle defining 
% the region of interest in the input image.  To rubberband the circle, click 
% the mouse button on the center of the desired region of interest.  Holding 
% the mouse button down, move the mouse until the circle has the desired radius.
% 
% 
%  "Ellipse"
% The ellipse region of interest works similarly to that of a circle (see above).
% To extract an ellipsoid region of interest, rubberband the ellipse defining 
% the region of interest in the input image.  Rubberbanding an ellipse is similar 
% to rubberbanding a circle, except that the width and height of the ellipse will 
% be changed with horizontal and vertical mouse movement.
% 
% 
%
%  
%
%  EXAMPLES
% % extractor -i image:mandril
%
%  "SEE ALSO"
% editimage(1)
%
%  RESTRICTIONS 
%
%  REFERENCES 
% The extractor program is covered in detail in Chapter 5 of the Envision Manual.
%
%  COPYRIGHT
% Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI")  All rights reserved.
% 


function varargout = kextractor(varargin)
if nargin ==0
  Inputs={};arglist={'',''};
elseif nargin ==1
  Inputs=varargin{1};arglist={'',''};
elseif nargin ==2
  Inputs=varargin{1}; arglist=varargin{2};
else error('Usage: [out1,..] = kextractor(Inputs,arglist).');
end
if size(arglist,2)~=2
  error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
 end
narglist={'i', '__input';'o', '__output';'shape', 1;'x', -1;'y', -1;'update', 1};
maxval={0,1,0,1000,1000,1};
minval={0,1,0,-1,-1,1};
istoggle=[0,1,0,1,1,1];
was_set=istoggle * 0;
paramtype={'InputFile','OutputFile','MultiChoice','Integer','Integer','Double'};
% identify the input arrays and assign them to the arguments as stated by the user
if ~iscell(Inputs)
Inputs = {Inputs};
end
NumReqOutputs=0; nextinput=1; nextoutput=1;
  for ii=1:size(arglist,1)
  wasmatched=0;
  for jj=1:size(narglist,1)
   if strcmp(arglist{ii,1},narglist{jj,1})  % a given argument was matched to the possible arguments
     wasmatched = 1;
     was_set(jj) = 1;
     if strcmp(narglist{jj,2}, '__input')
      if (nextinput > length(Inputs)) 
        error(['Input ' narglist{jj,1} ' has no corresponding input!']); 
      end
      narglist{jj,2} = 'OK_in';
      nextinput = nextinput + 1;
     elseif strcmp(narglist{jj,2}, '__output')
      if (nextoutput > nargout) 
        error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']); 
      end
      if (isempty(arglist{ii,2}))
        narglist{jj,2} = 'OK_out';
      else
        narglist{jj,2} = arglist{ii,2};
      end

      nextoutput = nextoutput + 1;
      if (minval{jj} == 0)  
         NumReqOutputs = NumReqOutputs - 1;
      end
     elseif isstr(arglist{ii,2})
      narglist{jj,2} = arglist{ii,2};
     else
        if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
            error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
        end
        if (minval{jj} ~= 0 | maxval{jj} ~= 0)
          if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
            error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
          elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
            error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
          elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
            error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
          elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
            error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
            error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
            error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
          end
        end
     end
     if ~strcmp(narglist{jj,2},'OK_out') &  ~strcmp(narglist{jj,2},'OK_in') 
       narglist{jj,2} = arglist{ii,2};
     end
   end
   end
   if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
        error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
   end
end
% match the remaining inputs/outputs to the unused arguments and test for missing required inputs
 for jj=1:size(narglist,1)
     if  strcmp(paramtype{jj}, 'Toggle')
        if (narglist{jj,2} ==0)
          narglist{jj,1} = ''; 
        end;
        narglist{jj,2} = ''; 
     end;
     if  ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
          narglist{jj,1} = ''; 
          narglist{jj,2} = ''; 
     end;
     if strcmp(narglist{jj,2}, '__input')
      if (minval{jj} == 0)  % meaning this input is required
        if (nextinput > size(Inputs)) 
           error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']); 
        else
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        end
      else  % this is an optional input
        if (nextinput <= length(Inputs)) 
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        else 
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end;
     else 
     if strcmp(narglist{jj,2}, '__output')
      if (minval{jj} == 0) % this is a required output
        if (nextoutput > nargout & nargout > 1) 
           error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']); 
        else
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
          NumReqOutputs = NumReqOutputs-1;
        end
      else % this is an optional output
        if (nargout - nextoutput >= NumReqOutputs) 
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
        else 
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end
     end
  end
end
if nargout
   varargout = cell(1,nargout);
else
  varargout = cell(1,1);
end
global KhorosRoot
if exist('KhorosRoot') && ~isempty(KhorosRoot)
w=['"' KhorosRoot];
else
if ispc
  w='"C:\Program Files\dip\khorosBin\';
else
[s,w] = system('which cantata');
w=['"' w(1:end-8)];
end
end
[varargout{:}]=callKhoros([w 'extractor"  '],Inputs,narglist);
