using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
namespace HalconTools
{
public class CalibImage
{
///
/// Reference to the controller class that performs all
/// calibration operations and interacts with the GUI.
///
private CalibrationAssistant mAssistant;
/// Calibration image
private HImage mImage;
///
/// Width of calibration image
///
public int mWidth;
///
/// Height of calibration image
///
public int mHeight;
///
/// Tuple with row coordinates of the detected marks
///
private HTuple mMarkCenterRows;
///
/// Tuple with column coordinates of the detected marks
///
private HTuple mMarkCenterCols;
private HTuple mWorldX;
private HTuple mWorldY;
///
/// Estimation for the external camera parameters (position and
/// orientation)
///
private HPose mEstimatedPose;
///
/// HALCON error message that occurs when calculating the
/// basic information for the calibration image
/// (plate region, marks and pose).
///
public string mErrorMessage;
///
/// Flag that describes the degree of success or failure
/// after an update of the basic information.
///
public string mPlateStatus;
///
/// Flag that permits or forbids this calibration image
/// to be part of the calibration process
///
public int mCanCalib; // true = 0 || false = 1
///
/// Region of the plane calibration plate in the calibration image
///
private HRegion mCaltabRegion;
///
/// XLD contour points of the marks detected in
/// the calibration image, generated from the row and
/// column values mMarkCenterRows and
/// mMarkCenterCols
///
private HXLDCont mMarkCenter;
///
/// Estimated world coordinate system (pose of the calibration plate
/// in camera coordinates), based on the
/// mEstimatedPose and the camera parameters
/// for this calibration image
///
private HObject mEstimatedWCS;
private double mEstimatedPlateSize;
// for quality measurement
private ArrayList mQualityIssuesList;
///
/// Initializes all status flags and objects to set up
/// this image for the calibration process
///
/// Calibration image
/// Reference to the Calibration Assistant
public CalibImage(HImage img, CalibrationAssistant assist)
{
string tmp;
mImage = img;
mAssistant = assist;
mCanCalib = 1; //labeled as 'for not having been evaluated'
mPlateStatus = CalibrationAssistant.PS_NOT_FOUND;// "No Plate found" yet
mImage.GetImagePointer1(out tmp, out mWidth, out mHeight);
mEstimatedPlateSize = 0;
mErrorMessage = "";
// initialize all instances
mCaltabRegion = new HRegion();
mMarkCenter = new HXLDCont();
mEstimatedWCS = new HObject();
mQualityIssuesList = new ArrayList(15);
mMarkCenterRows = new HTuple();
mMarkCenterCols = new HTuple();
mEstimatedPose = new HPose();
}
/************** getter-methods *************/
/********************************************/
public HImage getImage()
{
return mImage;
}
public HTuple getMarkCenterRows()
{
return mMarkCenterRows;
}
public HTuple getMarkCenterColumns()
{
return mMarkCenterCols;
}
public HTuple getWorldX()
{
return mWorldX;
}
public HTuple getWorldY()
{
return mWorldY;
}
public HXLDCont getMarkCenters()
{
return mMarkCenter;
}
public HTuple getEstimatedPose()
{
return mEstimatedPose;
}
public HObject getEstimatedWCS()
{
return mEstimatedWCS;
}
public double getEstimatedPlateSize()
{
return mEstimatedPlateSize;
}
public HObject getCaltabRegion()
{
return mCaltabRegion;
}
public ArrayList getQualityIssueList()
{
return mQualityIssuesList;
}
public string getPlateStatus()
{
return mPlateStatus;
}
///
/// Determine s(or updates) the basic information for this
/// calibration image, which are the values for the region
/// plate, the center marks, and the estimated pose.
/// The flag mPlateStatus describes the evaluation
/// of the computation process.
/// If desired the quality assessment can be recalculated
/// as well.
///
///
/// Triggers the recalculation of the quality assessment for
/// this calibration image
///
public void UpdateCaltab(bool updateQuality)
{
HTuple unit = new HTuple("mm");
bool failed = false;
QualityProcedures proc = new QualityProcedures();
string descrFile;
HTuple startCamp;
mErrorMessage = "";
mCaltabRegion.Dispose();
mMarkCenter.Dispose();
mEstimatedWCS.Dispose();
//reset this variable
mMarkCenterRows = new HTuple();
mPlateStatus = CalibrationAssistant.PS_NOT_FOUND;
descrFile = mAssistant.getDesrcFile();
try
{
mCaltabRegion = mImage.FindCaltab(descrFile,
(int)mAssistant.mFilterSize,
(int)mAssistant.mMarkThresh,
(int)mAssistant.mMinMarkDiam);
mPlateStatus = CalibrationAssistant.PS_MARKS_FAILED;
//-- Quality issue measurements --
if (updateQuality)
{
mQualityIssuesList.Clear();
failed = mAssistant.testQualityIssues(this);
}
startCamp = mAssistant.getCameraParams(this);
mMarkCenterRows = mImage.FindMarksAndPose(mCaltabRegion,
descrFile,
startCamp,
(int)mAssistant.mInitThresh,
(int)mAssistant.mThreshDecr,
(int)mAssistant.mMinThresh,
mAssistant.mSmoothing,
mAssistant.mMinContLength,
mAssistant.mMaxMarkDiam,
out mMarkCenterCols,
out mEstimatedPose);
mMarkCenter.GenCrossContourXld(mMarkCenterRows,
mMarkCenterCols,
new HTuple(6.0),
0.785398);
if (failed)
mAssistant.addQualityIssue(this, CalibrationAssistant.QUALITY_ISSUE_FAILURE, 0.0);
HOperatorSet.ImagePointsToWorldPlane(startCamp, mEstimatedPose,
mMarkCenterRows, mMarkCenterCols,
unit, out mWorldX, out mWorldY);
mEstimatedPlateSize = HMisc.DistancePp(mWorldY[0].D, mWorldX[0].D,
mWorldY[1].D, mWorldX[1].D);
mEstimatedPlateSize *= 10.0;
proc.get_3d_coord_system(mImage, out mEstimatedWCS,
startCamp, mEstimatedPose,
new HTuple(mEstimatedPlateSize / 2.0));
mPlateStatus = mQualityIssuesList.Count > 0 ? CalibrationAssistant.PS_QUALITY_ISSUES : CalibrationAssistant.PS_OK; // "Quality Issues found": "OK";
mCanCalib = 0;
}
catch (HOperatorException e)
{
this.mErrorMessage = e.Message;
mCanCalib = 1;
/* if exception was raised due to lack of memory,
* forward the error to the calling method */
if (e.Message.IndexOf("not enough") != -1)
throw (e);
}
}
///
/// Releases the memory for all iconic HALCON objects contained in
/// this instance.
///
public void Clear()
{
mImage.Dispose();
mCaltabRegion.Dispose();
}
}
}