using System;
using System.Collections;
using HalconDotNet;
namespace HalconTools
{
///
/// This class contains the parameters that are used to
/// create and detect the shape-based model.
/// Besides auxiliary methods to set and get the parameters, there
/// is a set of methods to handle the parameters for 'automatic'
/// determination.
///
public class MatchingParam
{
// --------------- create model ---------------------
///
/// Defines the maximum number of pyramid levels
///
public int mNumLevel;
///
/// Measure for local gray value differences between
/// the object and the background and between
/// different parts of the object
///
public int mContrast;
///
/// Minimum scale of the model
///
public double mMinScale;
///
/// Maximum scale of the model
///
public double mMaxScale;
///
/// Step length within the selected range of scales
///
public double mScaleStep;
///
/// Smallest rotation of the model
///
public double mStartingAngle;
///
/// Extent of the rotation angles
///
public double mAngleExtent;
///
/// Step length within the selected range of angles
///
public double mAngleStep;
///
/// Used to separate the model from the noise in the image
///
public int mMinContrast;
///
/// Conditions determining how the model is supposed to be
/// recognized in the image
///
public string mMetric;
///
/// Defines the kind of optimization and optionally the
/// kind of method used for generating the model
///
public string mOptimization;
// -------------------- find model -----------------------
///
/// Defines the score a potential match must have at least
/// to be accepted as an instance of the model in the image
///
public double mMinScore;
///
/// Number of instances of the model to be found
///
public int mNumMatches;
///
/// 'Greediness' of the search heuristic (0 means safe but slow -
/// 1 means fast but matches may be missed).
///
public double mGreediness;
///
/// Defines fraction two instances may at most overlap in order
/// to consider them as different instances, and hence
/// to be returned separately
///
public double mMaxOverlap;
///
/// Determines whether the instances should be extracted with
/// subpixel accuracy
///
public string mSubpixel;
///
/// Determines the lowest pyramid level to which the
/// found matches are tracked. Mechanism is used to
/// speed up the matching.
///
public int mLastPyramidLevel;
//----------------- optimize recognition speed ----------------
///
/// Defines heuristic for evaluating the detection results
///
public int mRecogRateOpt; /* opt=0 => '=' und opt=1 => '>='*/
///
/// Defines rate for the model recognition
///
public int mRecogRate;
///
/// Defines mode to determine accuracy of recognition
///
public string mRecogSpeedMode;
///
/// Manual selection of number of matches to be found.
///
public int mRecogManualSel;
// ---------------------- inspect vals -----------------------
///
/// Is equal to the value of the parameter mNumMatches
///
public int mInspectMaxNoMatch;
///
/// List of parameters to be determined automatically
///
public ArrayList paramAuto;
///
/// Constant defining the auto-mode for the parameter NumLevels
///
public const string AUTO_NUM_LEVEL = "num_levels";
///
/// Constant defining the auto-mode for the parameter Contrast
///
public const string AUTO_CONTRAST = "contrast";
///
/// Constant defining the auto-mode for the parameter ScaleStep
///
public const string AUTO_SCALE_STEP = "scale_step";
///
/// Constant defining the auto-mode for the parameter AngleStep
///
public const string AUTO_ANGLE_STEP = "angle_step";
///
/// Constant defining the auto-mode for the parameter MinContrast
///
public const string AUTO_MIN_CONTRAST = "min_contrast";
///
/// Constant defining the auto-mode for the parameter Optimization
///
public const string AUTO_OPTIMIZATION = "optimization";
///
/// Constant indicating an update for the button representation of
/// AngleStart.
///
public const string BUTTON_ANGLE_START = "angle_start";
///
/// Constant indicating an update for the button representation of
/// AngleExtent.
///
public const string BUTTON_ANGLE_EXTENT = "angle_extent";
///
/// Constant indicating an update for the button representation of
/// ScaleMin.
///
public const string BUTTON_SCALE_MIN = "scale_min";
///
/// Constant indicating an update for the button representation of
/// ScaleMax.
///
public const string BUTTON_SCALE_MAX = "scale_max";
///
/// Constant indicating an update for the button representation of
/// Metric.
///
public const string BUTTON_METRIC = "metric";
///
/// Constant indicating an update for the button representation of
/// MinScore.
///
public const string BUTTON_MINSCORE = "min_score";
///
/// Constant indicating an update for the button representation of
/// Greediness.
///
public const string BUTTON_GREEDINESS = "greediness";
///
/// Constant defining the number of instances to be detected:
/// Find number of models specified by the user
///
public const string RECOGM_MANUALSELECT = "RecognFindSpecifiedNumber";
///
/// Constant defining the number of instances to be detected:
/// Find at least one model instance per image
///
public const string RECOGM_ATLEASTONE = "RecognAtLeast";
///
/// Constant defining the number of instances to be detected:
/// Find maximum number of model instances per image
///
public const string RECOGM_MAXNUMBER = "RecognFindMaximum";
///
/// Constant indicating a change of ScaleStep for
/// its GUI component representation
///
public const string RANGE_SCALE_STEP = "RangeScaleStep";
///
/// Constant indicating a change of AngleStep for
/// its GUI component representation
///
public const string RANGE_ANGLE_STEP = "RangeAngleStep";
///
/// Constant indicating an error regarding the parameter set.
/// It is forwarded for HALCON errors that occur during the
/// creation of the shape-based model or detection of instances
/// of the model
///
public const string H_ERR_MESSAGE = "Halcon Error";
/// Constructor
public MatchingParam()
{
paramAuto = new ArrayList(10);
}
/*******************************************************************/
/* Setter-methods for the set of values, that can be determined
* automatically. If a parameter gets assigned a new value
* it can be only caused by user interaction, which means, the
* auto-modus for these particular parameters needs to be
* canceled, to avoid further automatic adjustment
/*******************************************************************/
///
/// Sets the parameter NumLevel to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
public void setNumLevel(double val)
{
mNumLevel = (int)val;
if(paramAuto.Contains(AUTO_NUM_LEVEL))
paramAuto.Remove(AUTO_NUM_LEVEL);
}
///
/// Sets the parameter Contrast to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
public void setContrast(int val)
{
mContrast = val;
if(paramAuto.Contains(AUTO_CONTRAST))
paramAuto.Remove(AUTO_CONTRAST);
}
///
/// Sets the parameter MinScale to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
public void setMinScale(double val)
{
mMinScale = val;
}
///
/// Sets the parameter MaxScale to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
public void setMaxScale(double val)
{
mMaxScale = val;
}
///
/// Sets the parameter ScaleStep to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
public void setScaleStep(double val)
{
mScaleStep = val;
if(paramAuto.Contains(AUTO_SCALE_STEP))
paramAuto.Remove(AUTO_SCALE_STEP);
}
///
/// Sets the parameter AngleStep to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
public void setAngleStep(double val)
{
mAngleStep = val;
if(paramAuto.Contains(AUTO_ANGLE_STEP))
paramAuto.Remove(AUTO_ANGLE_STEP);
}
///
/// Sets the parameter MinContrast to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
public void setMinContrast(int val)
{
mMinContrast = val;
if(paramAuto.Contains(AUTO_MIN_CONTRAST))
paramAuto.Remove(AUTO_MIN_CONTRAST);
}
///
/// Sets the parameter Optimization to the supplied value;
/// if the parameter has been in auto-mode, cancel this option
///
///
public void setOptimization(string val)
{
mOptimization = val;
if(paramAuto.Contains(AUTO_OPTIMIZATION))
paramAuto.Remove(AUTO_OPTIMIZATION);
}
/*******************************************************************/
/* Setter-methods for the other values */
/*******************************************************************/
///
/// Sets the parameter StartingAngle to the supplied value
///
public void setStartingAngle(double val)
{
mStartingAngle = val;
}
///
/// Sets the parameter AngleExtent to the supplied value
///
public void setAngleExtent(double val)
{
mAngleExtent = val;
}
///
/// Sets the parameter Metric to the supplied value
///
public void setMetric(string val)
{
mMetric = val;
}
///
/// Sets the parameter MinScore to the supplied value
///
public void setMinScore(double val)
{
mMinScore = val;
}
///
/// Sets the parameter NumMatches to the supplied value
///
public void setNumMatches(int val)
{
mNumMatches = val;
}
///
/// Sets the parameter Greediness to the supplied value
///
public void setGreediness(double val)
{
mGreediness = val;
}
///
/// Sets the parameter MaxOverlap to the supplied value
///
public void setMaxOverlap(double val)
{
mMaxOverlap = val;
}
///
/// Sets the parameter Subpixel to the supplied value
///
public void setSubPixel(string val)
{
mSubpixel = val;
}
///
/// Sets the parameter LastPyramidLevel to the supplied value
///
public void setLastPyramLevel(int val)
{
mLastPyramidLevel = val;
}
/*******************************************************************/
/*******************************************************************/
/*******************************************************************/
///
/// Sets the parameter defining the options for the recognition rate
/// to the supplied value
///
public void setRecogRateOption(int val)
{
mRecogRateOpt = val;
}
///
/// Sets the parameter defining the rate for the recognition to the
/// supplied value.
///
public void setRecogitionRate(int val)
{
mRecogRate = val;
}
///
/// Sets the parameter to define the mode of accuracy
///
public void setRecogSpeedMode(string val)
{
mRecogSpeedMode = val;
}
///
/// Sets the number of matches to be recognized to the supplied value.
///
public void setRecogManualSelection(int val)
{
mRecogManualSel = val;
}
///
/// Sets the parameter NumMatches to the supplied value.
///
public void setInspectMaxNoMatchValue(int val)
{
mInspectMaxNoMatch = val;
}
/*******************************************************************/
/*******************************************************************/
///
/// Checks if the parameter referenced by mode is
/// in the auto-mode list, i.e., that it is determined automatically
///
///
/// Constant starting with AUTO_*, describing one of the parameters
/// for the auto-mode.
///
public bool isAuto(string mode)
{
bool isAuto = false;
switch (mode)
{
case AUTO_ANGLE_STEP:
isAuto = paramAuto.Contains(AUTO_ANGLE_STEP);
break;
case AUTO_CONTRAST:
isAuto = paramAuto.Contains(AUTO_CONTRAST);
break;
case AUTO_MIN_CONTRAST:
isAuto = paramAuto.Contains(AUTO_MIN_CONTRAST);
break;
case AUTO_NUM_LEVEL:
isAuto = paramAuto.Contains(AUTO_NUM_LEVEL);
break;
case AUTO_OPTIMIZATION:
isAuto = paramAuto.Contains(AUTO_OPTIMIZATION);
break;
case AUTO_SCALE_STEP:
isAuto = paramAuto.Contains(AUTO_SCALE_STEP);
break;
default: break;
}
return isAuto;
}
///
/// Checks if any parameter is registered for automatic
/// determination. If not, the call for automatic
/// determination can be skipped
///
public bool isOnAuto()
{
if( paramAuto.Count > 0 )
return true;
else
return false;
}
///
/// Adds the parameter val to the list of parameters that
/// will be determined automatically before the application.
///
///
/// Constant starting with AUTO_*, describing one of the parameters
/// for the auto-mode.
///
///
/// Indicates whether the variable is already in auto-mode or
/// was added to the auto-list successfully.
///
public bool setAuto(string val)
{
string mode = "";
switch (val)
{
case AUTO_ANGLE_STEP:
if(!paramAuto.Contains(AUTO_ANGLE_STEP))
mode = AUTO_ANGLE_STEP;
break;
case AUTO_CONTRAST:
if(!paramAuto.Contains(AUTO_CONTRAST))
mode = AUTO_CONTRAST;
break;
case AUTO_MIN_CONTRAST:
if(!paramAuto.Contains(AUTO_MIN_CONTRAST))
mode = AUTO_MIN_CONTRAST;
break;
case AUTO_NUM_LEVEL:
if(!paramAuto.Contains(AUTO_NUM_LEVEL))
mode = AUTO_NUM_LEVEL;
break;
case AUTO_OPTIMIZATION:
if(!paramAuto.Contains(AUTO_OPTIMIZATION))
mode = AUTO_OPTIMIZATION;
break;
case AUTO_SCALE_STEP:
if(!paramAuto.Contains(AUTO_SCALE_STEP))
mode = AUTO_SCALE_STEP;
break;
default: break;
}
if(mode == "")
return false;
paramAuto.Add(mode);
return true;
}
///
/// Removes the parameter val from the list of parameters that
/// will be determined automatically.
///
///
/// Constant starting with AUTO_*, describing one of the parameters for
/// the auto-mode.
///
///
/// Indicates if the variable was removed from the
/// auto-list successfully.
///
public bool removeAuto(string val)
{
string mode = "";
switch (val)
{
case AUTO_ANGLE_STEP:
if(paramAuto.Contains(AUTO_ANGLE_STEP))
mode = AUTO_ANGLE_STEP;
break;
case AUTO_CONTRAST:
if(paramAuto.Contains(AUTO_CONTRAST))
mode = AUTO_CONTRAST;
break;
case AUTO_MIN_CONTRAST:
if(paramAuto.Contains(AUTO_MIN_CONTRAST))
mode = AUTO_MIN_CONTRAST;
break;
case AUTO_NUM_LEVEL:
if(paramAuto.Contains(AUTO_NUM_LEVEL))
mode = AUTO_NUM_LEVEL;
break;
case AUTO_OPTIMIZATION:
if(paramAuto.Contains(AUTO_OPTIMIZATION))
mode = AUTO_OPTIMIZATION;
break;
case AUTO_SCALE_STEP:
if(paramAuto.Contains(AUTO_SCALE_STEP))
mode = AUTO_SCALE_STEP;
break;
default: break;
}
if(mode == "")
return false;
paramAuto.Remove(mode);
return true;
}
///
/// Gets the names of the parameters to be determined
/// automatically
///
///
/// List of parameter names being in auto-mode.
///
public string [] getAutoParList()
{
int count = paramAuto.Count;
string [] paramList = new string[count];
for(int i=0; i