using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HalconDotNet;
namespace HalconTools
{
public partial class frmCalibration : Form
{
private HWndCtrl mView;
private CalibrationAssistant mAssistant;
private int currIdx;
private int currLineW;
private double tThickness = 0;
private double tCellWidth = 0;
private double tCellHeight = 0;
private double tFocalLength = 0;
private double tMotionX = 0;
private double tMotionY = 0;
private double tMotionZ = 0;
private bool locked;
private bool plateRegionDisp;
private bool markCentersDisp;
private bool coordSystemDisp;
private string plateRegionColor;
private string markCenterColor;
private string coordSystemColor;
public frmCalibration()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
mView = new HWndCtrl(viewPort);
mView.setViewState(HWndCtrl.MODE_VIEW_NONE);
mView.changeGraphicSettings(GraphicsContext.GC_DRAWMODE, comboBoxDraw.Text);
mView.changeGraphicSettings(GraphicsContext.GC_LINEWIDTH, 1);
mAssistant = new CalibrationAssistant();
mAssistant.NotifyCalibObserver = new CalibDelegate(UpdateCalibResults);
Init();
}
/*******************************************************************
* Initialize the GUI front end with the values defined for the
* corresponding parameters in the Calibration Assistant
* ****************************************************************/
private void Init()
{
currIdx = 0;
currLineW = 1;
locked = true;
plateRegionDisp = checkBoxPlateRegion.Checked;
markCentersDisp = checkBoxMarkCenter.Checked;
coordSystemDisp = checkBoxCoordSys.Checked;
plateRegionColor = comboBoxPlateRegion.Text;
markCenterColor = comboBoxMarkCenters.Text;
coordSystemColor = comboBoxCoordSys.Text;
CamTypComboBox.SelectedIndex = 0;
SxUpDown.Value = (decimal)mAssistant.getCellWidth();
SxUpDown.Increment = 0.100m;
SyUpDown.Value = (decimal)mAssistant.getCellHeight();
SyUpDown.Increment = 0.100m;
FocalLengthUpDown.Value = (decimal)mAssistant.getFocalLength();
FocalLengthUpDown.Increment = 0.100m;
ThicknessUpDown.Value = (decimal)(mAssistant.getThickness());
ThicknessUpDown.Increment = 0.100m;
textBoxDescr.Text = mAssistant.mDescrFileName;
FilterSizeUpDown.Value = (int)mAssistant.getFilterSize();
MarkThreshUpDown.Value = (int)mAssistant.getMarkThresh();
MinDiamUpDown.Value = (int)mAssistant.getMinMarkDiam();
InitThreshUpDown.Value = (int)mAssistant.getInitThresh();
ThreshDecrUpDown.Value = (int)mAssistant.getThreshDecr();
MinThreshUpDown.Value = (int)mAssistant.getMinThresh();
SmoothUpDown.Value = (int)(mAssistant.getSmoothing() * 100);
MinContLUpDown.Value = (int)mAssistant.getMinContLength();
MaxDiamUpDown.Value = (int)mAssistant.getMaxMarkDiam();
WarnlevelUpDown.Value = (int)mAssistant.getWarnLevel();
MotionXUpDown.Value = (decimal)mAssistant.getMotionX();
MotionXUpDown.Increment = 0.100m;
MotionYUpDown.Value = (decimal)mAssistant.getMotionY();
MotionYUpDown.Increment = 0.100m;
MotionZUpDown.Value = (decimal)mAssistant.getMotionZ();
MotionZUpDown.Increment = 0.100m;
string imPathValue =
(string)HSystem.GetSystem("image_dir").TupleSplit(";");
openFileDialogImg.InitialDirectory = imPathValue + "\\calib";
string halconPathValue = Environment.GetEnvironmentVariable(
"HALCONROOT");
openFileDialogDescr.InitialDirectory = halconPathValue + "\\calib";
// set initial directory to standard user's working directory
openFileDialogImportParams.InitialDirectory =
Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
// set initial directory to standard user's working directory
saveParamFileDialog.InitialDirectory =
Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
locked = false;
}
/********************************************************************/
/* Display Parameters */
/********************************************************************/
private void checkBoxPlateRegion_CheckedChanged(object sender, System.EventArgs e)
{
plateRegionDisp = checkBoxPlateRegion.Checked;
UpdateView();
}
/****************************************************/
private void checkBoxMarkCenter_CheckedChanged(object sender, System.EventArgs e)
{
markCentersDisp = checkBoxMarkCenter.Checked;
UpdateView();
}
/****************************************************/
private void checkBoxCoordSys_CheckedChanged(object sender, System.EventArgs e)
{
coordSystemDisp = checkBoxCoordSys.Checked;
UpdateView();
}
/****************************************************/
private void comboBoxPlateRegion_SelectedIndexChanged(object sender, System.EventArgs e)
{
plateRegionColor = comboBoxPlateRegion.Text;
UpdateView();
}
/****************************************************/
private void comboBoxMarkCenters_SelectedIndexChanged(object sender, System.EventArgs e)
{
markCenterColor = comboBoxMarkCenters.Text;
UpdateView();
}
/****************************************************/
private void comboBoxCoordSys_SelectedIndexChanged(object sender, System.EventArgs e)
{
coordSystemColor = comboBoxCoordSys.Text;
UpdateView();
}
/****************************************************/
private void comboBoxDraw_SelectedIndexChanged(object sender, System.EventArgs e)
{
mView.changeGraphicSettings(GraphicsContext.GC_DRAWMODE, comboBoxDraw.Text);
UpdateView();
}
private void upDownLineWidth_ValueChanged(object sender, System.EventArgs e)
{
currLineW = (int)upDownLineWidth.Value;
mView.changeGraphicSettings(GraphicsContext.GC_LINEWIDTH, currLineW);
UpdateView();
}
/********************************************************************/
/* MouseInteraction Parameters */
/********************************************************************/
private void buttonMove_CheckedChanged(object sender, System.EventArgs e)
{
mView.setViewState(HWndCtrl.MODE_VIEW_MOVE);
}
/****************************************************/
private void buttonZoom_CheckedChanged(object sender, System.EventArgs e)
{
mView.setViewState(HWndCtrl.MODE_VIEW_ZOOM);
}
/****************************************************/
private void buttonMagnify_CheckedChanged(object sender, System.EventArgs e)
{
mView.setViewState(HWndCtrl.MODE_VIEW_ZOOMWINDOW);
}
/****************************************************/
private void buttonNone_CheckedChanged(object sender, System.EventArgs e)
{
mView.setViewState(HWndCtrl.MODE_VIEW_NONE);
}
/****************************************************/
private void buttonReset_Click(object sender, System.EventArgs e)
{
mView.resetWindow();
UpdateView();
}
/********************************************************************/
/********************************************************************/
/* 1. Tab */
/********************************************************************/
/********************************************************************/
private void buttonLoad_Click(object sender, System.EventArgs e)
{
string[] files;
ListViewItem item;
int count = 0;
CalibImage data = null;
int idx = StatusLabel.Text.Length;
if (openFileDialogImg.ShowDialog() == DialogResult.OK)
{
files = openFileDialogImg.FileNames;
count = files.Length;
for (int i = 0; i < count; i++)
{
if ((data = mAssistant.addImage(files[i])) != null)
{
item = new ListViewItem("");
item.SubItems.Add(files[i]);
item.SubItems.Add(data.getPlateStatus());
ListCalibImg.Items.AddRange(new ListViewItem[] { item });
}
}//for
mAssistant.UpdateSequenceIssues();
buttonCalibrate.Enabled = (mAssistant.mCanCalib && (mAssistant.mReferenceIndex != -1));
buttonSetReference.Enabled = true;
if (data != null)
{
ListCalibImg.Items[currIdx].BackColor = System.Drawing.SystemColors.Window;
currIdx = ListCalibImg.Items.Count - 1;
UpdateCalibResults(CalibrationAssistant.UPDATE_MARKS_POSE);
UpdateCalibResults(CalibrationAssistant.UPDATE_QUALITY_TABLE);
UpdateCalibResults(CalibrationAssistant.UPDATE_CALIBRATION_RESULTS);
}
}//if
}
/****************************************************/
private void ListCalibImg_SelectedIndexChanged(object sender, System.EventArgs e)
{
string path = "";
ListView.SelectedListViewItemCollection items = ListCalibImg.SelectedItems;
foreach (ListViewItem item in items)
{
path = item.SubItems[1].Text;
if (path != "")
{
ListCalibImg.Items[currIdx].BackColor = System.Drawing.SystemColors.Window;
currIdx = ListCalibImg.Items.IndexOf(item);
ListCalibImg.Items[currIdx].BackColor = System.Drawing.SystemColors.Control;
showLabelErrMessage();
UpdateView();
UpdateQualityIssueTable();
break;
}//if
}//foreach
}
/****************************************************/
private void showLabelErrMessage()
{
string message = mAssistant.getCalibDataAt(currIdx).mErrorMessage;
showLabelErrMessage(message);
}
/****************************************************/
private void showLabelErrMessage(string message)
{
int idx = message.IndexOf(":");
string tmp;
if (idx > 0)
{
message = "Error" + message.Remove(0, idx);
if (message.Length > 90)
{
idx = message.LastIndexOf(" ", 80);
tmp = message.Substring(0, idx) + "\n " + message.Substring(idx + 1);
message = tmp;
}
StatusLabel.Text = message;
}
else
{
StatusLabel.Text = "";
}
}
/****************************************************/
private void buttonDelete_Click(object sender, System.EventArgs e)
{
int cIdx = -1;
int refIdx = -1;
string path = "";
ListView.SelectedListViewItemCollection items = ListCalibImg.SelectedItems;
currIdx = 0;
foreach (ListViewItem item in items)
{
path = item.SubItems[1].Text;
if (path != "")
{
refIdx = mAssistant.mReferenceIndex;
if ((cIdx = ListCalibImg.Items.IndexOf(item)) == refIdx)
refIdx = -1;
else if (cIdx < refIdx)
refIdx--;
mAssistant.setReferenceIdx(refIdx);
mAssistant.removeImage(cIdx);
buttonCalibrate.Enabled = (mAssistant.mCanCalib && (refIdx != -1));
ListCalibImg.Items.Remove(item);
ListQualityCheck.Items.Clear();
break;
}//if
}//foreach
if (ListCalibImg.Items.Count == 0)
buttonSetReference.Enabled = false;
else
UpdateView();
}
/****************************************************/
private void buttonDeleteAll_Click(object sender, System.EventArgs e)
{
mAssistant.removeImage();
mView.resetWindow();
mView.clearList();
currIdx = 0;
ListCalibImg.Items.Clear();
ListQualityCheck.Items.Clear();
buttonSetReference.Enabled = false;
buttonCalibrate.Enabled = false;
mAssistant.setReferenceIdx(-1);
mAssistant.resetCanCalib();
UpdateView();
}
/****************************************************/
private void buttonSetReference_Click(object sender, System.EventArgs e)
{
int val = mAssistant.mReferenceIndex;
if (val > -1)
ListCalibImg.Items[val].SubItems[0].Text = "";
mAssistant.setReferenceIdx(currIdx);
ListCalibImg.Items[currIdx].SubItems[0].Text = " *";
buttonCalibrate.Enabled = mAssistant.mCanCalib;
}
/*************************************************************/
private void buttonCalibrate_Click(object sender, System.EventArgs e)
{
StatusLabel.Text = " Calibrating ... ";
StatusLabel.Refresh();
mAssistant.applyCalibration();
tabControl.SelectedTab = tabControl.TabPages[2];
}
/**************************************************************/
private void buttonLoadDescrFile_Click(object sender, System.EventArgs e)
{
string file;
string[] val;
if (openFileDialogDescr.ShowDialog() == DialogResult.OK)
{
file = openFileDialogDescr.FileNames[0];
if (file.EndsWith(".descr"))
{
mAssistant.setDesrcFile(file);
val = file.Split(new Char[] { '\\' });
file = val[val.Length - 1];
textBoxDescr.Text = file;
}
else
{
MessageBox.Show("Fileformat is wrong, it's not a description file!",
"Calibration Assistant",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
/**************************************************************/
private void buttonImportParams_Click(object sender, System.EventArgs e)
{
string file;
bool success;
if (openFileDialogImportParams.ShowDialog() == DialogResult.OK)
{
file = openFileDialogImportParams.FileNames[0];
if (file.EndsWith(".cal") || file.EndsWith(".dat"))
{
success = mAssistant.importCamParams(file);
if (success)
{
locked = true;
resetGUICameraSetup();
locked = false;
}
}
else
{
UpdateCalibResults(CalibrationAssistant.ERR_READING_FILE);
}
}
}
/**************************************************************/
private void resetGUICameraSetup()
{
int camType, val;
try
{
camType = mAssistant.getCameraType();
if (camType == CalibrationAssistant.CAMERA_TYP_AREA_SCAN_DIV)
val = 0;
else if (camType == CalibrationAssistant.CAMERA_TYP_AREA_SCAN_POLY)
val = 1;
else if (camType == CalibrationAssistant.CAMERA_TYP_LINE_SCAN)
val = 2;
else
throw (new ArgumentException());
CamTypComboBox.SelectedIndex = val;
SxUpDown.Value = (decimal)mAssistant.getCellWidth();
SyUpDown.Value = (decimal)mAssistant.getCellHeight();
FocalLengthUpDown.Value = (decimal)mAssistant.getFocalLength();
TelecentricCheckBox.Checked = mAssistant.isTelecentric;
MotionXUpDown.Value = (decimal)mAssistant.getMotionX();
MotionYUpDown.Value = (decimal)mAssistant.getMotionY();
MotionZUpDown.Value = (decimal)mAssistant.getMotionZ();
}
catch (ArgumentException)
{
locked = false;
}
}
/**************************************************************/
private void buttonDefaultParams_Click(object sender, System.EventArgs e)
{
mAssistant.resetCameraSetup(false);
locked = true;
resetGUICameraSetup();
locked = false;
}
/**************************************************************/
private void numUpDownThickness_ValueChanged(object sender, System.EventArgs e)
{
tThickness = (double)ThicknessUpDown.Value;
mAssistant.setThickness((double)ThicknessUpDown.Value);
}
/**************************************************************/
private void comboBoxCamTyp_SelectedIndexChanged(object sender, System.EventArgs e)
{
int val = 0;
AreaScanPolynomPanel.Visible = false;
KappaPanel.Visible = false;
LineScanPanel.Visible = false;
LineScanAddPanel.Visible = false;
switch ((int)CamTypComboBox.SelectedIndex)
{
case 0:
val = CalibrationAssistant.CAMERA_TYP_AREA_SCAN_DIV;
KappaPanel.Visible = true;
TelecentricCheckBox.Enabled = true;
break;
case 1:
val = CalibrationAssistant.CAMERA_TYP_AREA_SCAN_POLY;
AreaScanPolynomPanel.Visible = true;
TelecentricCheckBox.Enabled = true;
break;
case 2:
val = CalibrationAssistant.CAMERA_TYP_LINE_SCAN;
KappaPanel.Visible = true;
LineScanPanel.Visible = true;
LineScanAddPanel.Visible = true;
TelecentricCheckBox.Checked = false;
TelecentricCheckBox.Enabled = false;
break;
}
if (!locked)
mAssistant.setCameraType(val);
}
/**************************************************************/
private void numUpDownSx_ValueChanged(object sender, System.EventArgs e)
{
tCellWidth = (double)SxUpDown.Value;
if (!locked)
mAssistant.setCellWidth((double)SxUpDown.Value);
}
/**************************************************************/
private void numUpDownSy_ValueChanged(object sender, System.EventArgs e)
{
tCellHeight = (double)SyUpDown.Value;
if (!locked)
mAssistant.setCellHeight((double)SyUpDown.Value);
}
/**************************************************************/
private void numUpDownFocalLength_ValueChanged(object sender, System.EventArgs e)
{
tFocalLength = (double)FocalLengthUpDown.Value;
if (!locked)
mAssistant.setFocalLength((double)FocalLengthUpDown.Value);
}
/**************************************************************/
private void checkBoxTelecentric_CheckedChanged(object sender, System.EventArgs e)
{
bool check = TelecentricCheckBox.Checked;
if (check)
FocalLengthUpDown.Enabled = false;
else
FocalLengthUpDown.Enabled = true;
if (!locked)
mAssistant.setIsTelecentric(check);
}
/**************************************************************/
private void MotionXUpDown_ValueChanged(object sender, System.EventArgs e)
{
tMotionX = (double)MotionXUpDown.Value;
if (!locked)
mAssistant.setMotionX((double)MotionXUpDown.Value);
}
/**************************************************************/
private void MotionYUpDown_ValueChanged(object sender, System.EventArgs e)
{
tMotionY = (double)MotionYUpDown.Value;
if (!locked)
mAssistant.setMotionY((double)MotionYUpDown.Value);
}
/**************************************************************/
private void MotionZUpDown_ValueChanged(object sender, System.EventArgs e)
{
tMotionZ = (double)MotionZUpDown.Value;
if (!locked)
mAssistant.setMotionZ((double)MotionZUpDown.Value);
}
/*************************************************************************/
/*************************************************************************/
/* 2. Tab */
/*************************************************************************/
/*************************************************************************/
private void comboBoxImgTests_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (!locked)
mAssistant.setImageTests(ImgTestsComboBox.SelectedIndex);
}
/**************************************************************/
private void comboBoxSeqTests_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (!locked)
mAssistant.setSequenceTests(SeqTestsComboBox.SelectedIndex);
}
/**************************************************************/
private void numUpDownWarnlevel_ValueChanged(object sender, System.EventArgs e)
{
if (!locked)
mAssistant.setWarnLevel((int)WarnlevelUpDown.Value);
}
/**************************************************************/
/******************** FillSize ***************************/
private void numUpDownFilterSize_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)FilterSizeUpDown.Value;
FilterSizeTrackBar.Value = val;
if (!locked)
setFilterSize(val);
}
private void trackBarFilterSize_Scroll(object sender, System.EventArgs e)
{
FilterSizeUpDown.Value = FilterSizeTrackBar.Value;
FilterSizeUpDown.Refresh();
}
private void buttonRFilterSize_Click(object sender, System.EventArgs e)
{
FilterSizeUpDown.Value = mAssistant.resetFilterSize;
FilterSizeResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setFilterSize(int val)
{
FilterSizeResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setFilterSize((double)val);
}
/**************************************************************/
/********************** Mark Threshold ************************/
private void numUpDownMarkThresh_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)MarkThreshUpDown.Value;
MarkThreshTrackBar.Value = val;
if (!locked)
setMarkThresh(val);
}
private void trackBarMarkThresh_Scroll(object sender, System.EventArgs e)
{
MarkThreshUpDown.Value = (int)MarkThreshTrackBar.Value;
MarkThreshUpDown.Refresh();
}
private void buttonRMarkThresh_Click(object sender, System.EventArgs e)
{
MarkThreshUpDown.Value = mAssistant.resetMarkThresh;
MarkThreshResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setMarkThresh(int val)
{
MarkThreshResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setMarkThresh((double)val);
}
/**************************************************************/
/*********************** Min Diameter ***********************/
private void numUpDownMinDiam_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)MinDiamUpDown.Value;
MinDiamTrackBar.Value = val;
if (!locked)
setMinDiam(val);
}
private void trackBarMinDiam_Scroll(object sender, System.EventArgs e)
{
MinDiamUpDown.Value = MinDiamTrackBar.Value;
MinDiamUpDown.Refresh();
}
private void buttonRMinDiam_Click(object sender, System.EventArgs e)
{
MinDiamUpDown.Value = mAssistant.resetMinMarkDiam;
MinDiamResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setMinDiam(int val)
{
MinDiamResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setMinMarkDiam((double)val);
}
/**************************************************************/
/******************** Init Threshold ************************/
private void numUpDownInitThresh_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)InitThreshUpDown.Value;
InitThreshTrackBar.Value = val;
if (!locked)
setInitThresh(val);
}
private void trackBarInitThresh_Scroll(object sender, System.EventArgs e)
{
InitThreshUpDown.Value = InitThreshTrackBar.Value;
InitThreshUpDown.Refresh();
}
private void buttonRInitThresh_Click(object sender, System.EventArgs e)
{
InitThreshUpDown.Value = mAssistant.resetInitThresh;
InitThreshResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setInitThresh(int val)
{
InitThreshResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setInitThresh((double)val);
}
/**************************************************************/
/****************** Threshold Decrement **********************/
private void numUpDownThreshDecr_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)ThreshDecrUpDown.Value;
ThreshDecrTrackBar.Value = val;
if (!locked)
setThreshDecr(val);
}
private void trackBarThreshDecr_Scroll(object sender, System.EventArgs e)
{
ThreshDecrUpDown.Value = (int)ThreshDecrTrackBar.Value;
ThreshDecrUpDown.Refresh();
}
private void buttonRThreshDecr_Click(object sender, System.EventArgs e)
{
ThreshDecrUpDown.Value = mAssistant.resetThreshDecr;
ThreshDecrResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setThreshDecr(int val)
{
ThreshDecrResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setThreshDecr((double)val);
}
/**************************************************************/
/****************** Minimum Threshold ************************/
private void numUpDownMinThresh_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)MinThreshUpDown.Value;
MinThreshTrackBar.Value = val;
if (!locked)
setMinThresh(val);
}
private void trackBarMinThresh_Scroll(object sender, System.EventArgs e)
{
MinThreshUpDown.Value = MinThreshTrackBar.Value;
MinThreshUpDown.Refresh();
}
private void buttonRMinThresh_Click(object sender, System.EventArgs e)
{
MinThreshUpDown.Value = mAssistant.resetMinThresh;
MinThreshResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setMinThresh(int val)
{
MinThreshResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setMinThresh((double)val);
}
/**************************************************************/
/******************** Smoothing *****************************/
private void numUpDownSmooth_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)SmoothUpDown.Value;
SmoothTrackBar.Value = val;
if (!locked)
setSmoothing(val);
}
private void trackBarSmooth_Scroll(object sender, System.EventArgs e)
{
SmoothUpDown.Value = SmoothTrackBar.Value;
SmoothUpDown.Refresh();
}
private void buttonRSmooting_Click(object sender, System.EventArgs e)
{
SmoothUpDown.Value = (int)(mAssistant.resetSmoothing * 100);
SmootingResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setSmoothing(int val)
{
SmootingResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setSmoothing((double)val / 100.0);
}
/**************************************************************/
/***************** Min Contour Length ************************/
private void numUpDownMinContL_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)MinContLUpDown.Value;
MinContLTrackBar.Value = val;
if (!locked)
setMinContLength(val);
}
private void trackBarMinContL_Scroll(object sender, System.EventArgs e)
{
MinContLUpDown.Value = (int)MinContLTrackBar.Value;
MinContLUpDown.Refresh();
}
private void buttonRMinContL_Click(object sender, System.EventArgs e)
{
MinContLUpDown.Value = mAssistant.resetMinContL;
MinContLResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setMinContLength(int val)
{
MinContLResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setMinContLength((double)val);
}
/**************************************************************/
/******************** Max Diameter ***************************/
private void numUpDownMaxDiam_ValueChanged(object sender, System.EventArgs e)
{
int val = (int)MaxDiamUpDown.Value;
MaxDiamTrackBar.Value = val;
if (!locked)
setMaxDiam(val);
}
private void trackBarMaxDiam_Scroll(object sender, System.EventArgs e)
{
MaxDiamUpDown.Value = MaxDiamTrackBar.Value;
MaxDiamUpDown.Refresh();
}
private void buttonRMaxDiam_Click(object sender, System.EventArgs e)
{
MaxDiamUpDown.Value = mAssistant.resetMaxMarkDiam;
MaxDiamResetButton.ForeColor = System.Drawing.Color.Gray;
}
private void setMaxDiam(int val)
{
MaxDiamResetButton.ForeColor = System.Drawing.Color.Black;
mAssistant.setMaxMarkDiam((double)val);
}
/*************************************************************************/
/*************************************************************************/
/* 3. Tab */
/*************************************************************************/
/*************************************************************************/
private void checkBoxOrigImgCoord_CheckedChanged(object sender, System.EventArgs e)
{
mAssistant.setAtImgCoord(checkBoxOrigImgCoord.Checked);
UpdateView();
}
private void buttonSaveCamParams_Click(object sender, System.EventArgs e)
{
string files;
if (saveParamFileDialog.ShowDialog() == DialogResult.OK)
{
files = saveParamFileDialog.FileName;
if (!files.EndsWith(".cal") && !files.EndsWith(".CAL"))
files += ".cal";
if (mAssistant.mCalibValid)
mAssistant.saveCamParams(files);
}
}
/*************************************************************************/
/*************************************************************************/
private void buttonSaveCamPose_Click(object sender, System.EventArgs e)
{
string files;
if (saveParamFileDialog.ShowDialog() == DialogResult.OK)
{
files = saveParamFileDialog.FileName;
if (!files.EndsWith(".dat") && !files.EndsWith(".DAT"))
files += ".dat";
if (mAssistant.mCalibValid)
mAssistant.saveCamPose(files);
}
}
/*************************************************************************/
private void buttonRefImg_CheckedChanged(object sender, System.EventArgs e)
{
UpdateView();
}
/*************************************************************************/
private void radioSimulatedImg_CheckedChanged(object sender, System.EventArgs e)
{
UpdateView();
}
/********************************************************************/
/* Update methods invoked by delegates */
/********************************************************************/
///
/// This update method is invoked for all changes occurring in the
/// CalibrationAssistant that need to be forwarded to the GUI. The
/// referring delegate invokes an update for changes in the model
/// data, which leads to an update in the graphics view. Also, errors
/// that occur during IO functions and for single calibration steps
/// are mapped here.
///
///
/// Constant of the class CalibrationAssistant, which starts with
/// UPDATE_* or ERR_* and which describes an update of the model
/// data or an HALCON error, respectively.
///
public void UpdateCalibResults(int mode)
{
switch (mode)
{
case CalibrationAssistant.UPDATE_MARKS_POSE:
UpdateView();
break;
case CalibrationAssistant.UPDATE_QUALITY_TABLE:
UpdateQualityIssueTable();
break;
case CalibrationAssistant.UPDATE_CALTAB_STATUS:
UpdateImageStatus();
break;
case CalibrationAssistant.UPDATE_CALIBRATION_RESULTS:
UpdateResultTab(mAssistant.mCalibValid);
StatusLabel.Text = " ";
break;
case CalibrationAssistant.ERR_READING_FILE:
MessageBox.Show("Problem occured while reading file! \n" + mAssistant.mErrorMessage,
"Calibration Assistant",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
break;
case CalibrationAssistant.ERR_QUALITY_ISSUES:
MessageBox.Show("Error occured while testing for quality issues \n" + mAssistant.mErrorMessage,
"Calibration Assistant",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
ListQualityCheck.Items.Clear();
break;
case CalibrationAssistant.ERR_IN_CALIBRATION:
MessageBox.Show("Problem occured while calibrating",
"Calibration Assistant",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
UpdateResultTab(mAssistant.mCalibValid);
showLabelErrMessage(mAssistant.mErrorMessage);
break;
case CalibrationAssistant.ERR_REFINDEX_INVALID:
MessageBox.Show("Problem occured: \n" +
"Please check, whether your reference index is valid",
"Calibration Assistant",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
break;
case CalibrationAssistant.ERR_WRITE_CALIB_RESULTS:
MessageBox.Show("Problem occured while ! \n" + mAssistant.mErrorMessage,
"Calibration Assistant",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
break;
default:
break;
}
}
HObject mObj;
///
/// Update the graphical window, by adding all objects of either
/// the training data set or the test data set, including the
/// reference world coordinate system and the calibrated world
/// coordinate system.
///
public void UpdateView()
{
HImage img = null;
CalibImage data = mAssistant.getCalibDataAt(currIdx);
if (data == null)
{
mView.clearList();
}
else
{
if (tabControl.SelectedIndex == 2 && mAssistant.mCalibValid)
{
if (buttonRefImg.Checked)
img = mAssistant.getRefImage();
else
img = (HImage)mAssistant.getSimulatedImage();
mView.addIconicVar(img);
if (coordSystemDisp && mAssistant.getReferenceWCS().IsInitialized())
{
mView.changeGraphicSettings(GraphicsContext.GC_COLOR, coordSystemColor);
mView.changeGraphicSettings(GraphicsContext.GC_LINEWIDTH, currLineW + 1);
mView.addIconicVar(mAssistant.getReferenceWCS());
}
}
else if (tabControl.SelectedIndex == 3 && mAssistant.mCalibValid)
{
mView.addIconicVar(data.getImage());
mView.changeGraphicSettings(GraphicsContext.GC_LINEWIDTH, currLineW + 1);
mView.changeGraphicSettings(GraphicsContext.GC_COLOR, coordSystemColor);
HTuple dis = new HTuple();
HTuple worldRow = data.getWorldY();
HTuple worldCol = data.getWorldX();
HTuple Rows = data.getMarkCenterRows();
HTuple Cols = data.getMarkCenterColumns();
HTuple tmprows = new HTuple();
HTuple tmpcols = new HTuple();
int i, j;
#region P1-P2
i = (int)numericUpDownP1.Value;
j = (int)numericUpDownP2.Value;
tmprows.Append(Rows.ToDArr()[i - 1]);
tmprows.Append(Rows.ToDArr()[j - 1]);
tmpcols.Append(Cols.ToDArr()[i - 1]);
tmpcols.Append(Cols.ToDArr()[j - 1]);
HOperatorSet.DistancePp((HTuple)worldRow.ToDArr()[i - 1], (HTuple)worldCol.ToDArr()[i - 1], (HTuple)worldRow.ToDArr()[j - 1], (HTuple)worldCol.ToDArr()[j - 1], out dis);
txtDistanceP1P2.Text = dis.D.ToString("0.0000");
#endregion
HOperatorSet.GenEmptyObj(out mObj);
HOperatorSet.GenContourPolygonXld(out mObj, tmprows, tmpcols);
mView.addIconicVar(mObj);
tmprows = new HTuple();
tmpcols = new HTuple();
#region P3-P4
i = (int)numericUpDownP3.Value;
j = (int)numericUpDownP4.Value;
tmprows.Append(Rows.ToDArr()[i - 1]);
tmprows.Append(Rows.ToDArr()[j - 1]);
tmpcols.Append(Cols.ToDArr()[i - 1]);
tmpcols.Append(Cols.ToDArr()[j - 1]);
HOperatorSet.DistancePp((HTuple)worldRow.ToDArr()[i - 1], (HTuple)worldCol.ToDArr()[i - 1], (HTuple)worldRow.ToDArr()[j - 1], (HTuple)worldCol.ToDArr()[j - 1], out dis);
txtDistanceP3P4.Text = dis.D.ToString("0.0000");
#endregion
HOperatorSet.GenEmptyObj(out mObj);
HOperatorSet.GenContourPolygonXld(out mObj, tmprows, tmpcols);
mView.addIconicVar(mObj);
}
else
{
mView.addIconicVar(data.getImage());
mView.changeGraphicSettings(GraphicsContext.GC_LINEWIDTH, currLineW);
if (plateRegionDisp && data.getCaltabRegion().IsInitialized())
{
mView.changeGraphicSettings(GraphicsContext.GC_COLOR, plateRegionColor);
mView.addIconicVar(data.getCaltabRegion());
}
if (markCentersDisp && data.getMarkCenters().IsInitialized())
{
mView.changeGraphicSettings(GraphicsContext.GC_COLOR, markCenterColor);
mView.addIconicVar(data.getMarkCenters());
}
if (coordSystemDisp && data.getEstimatedWCS().IsInitialized())
{
mView.changeGraphicSettings(GraphicsContext.GC_COLOR, coordSystemColor);
mView.changeGraphicSettings(GraphicsContext.GC_LINEWIDTH, currLineW + 1);
mView.addIconicVar(data.getEstimatedWCS());
}
}
}
mView.repaint();
}
///
/// If the quality issues are recalculated for all calibration
/// images, the information table depicting the quality
/// measure for each image must be updated.
///
public void UpdateQualityIssueTable()
{
ListViewItem item;
QualityIssue issue;
int count;
string text = "";
ArrayList qList;
qList = (mAssistant.getCalibDataAt(currIdx)).getQualityIssueList();
count = qList.Count;
ListQualityCheck.Items.Clear();
for (int i = 0; i < count; i++)
{
issue = (QualityIssue)qList[i];
item = new ListViewItem("Image");
//Images
switch (issue.getIssueType())
{
case CalibrationAssistant.QUALITY_ISSUE_FAILURE:
text = "Quality assessment failed";
break;
case CalibrationAssistant.QUALITY_ISSUE_IMG_CALTAB_SIZE:
text = "Plate in image is too small";
break;
case CalibrationAssistant.QUALITY_ISSUE_IMG_CONTRAST:
text = "Contrast is too low";
break;
case CalibrationAssistant.QUALITY_ISSUE_IMG_EXPOSURE:
text = "Plate is too overexposed";
break;
case CalibrationAssistant.QUALITY_ISSUE_IMG_FOCUS:
text = "Marks on plate are out of focus";
break;
case CalibrationAssistant.QUALITY_ISSUE_IMG_HOMOGENEITY:
text = "Illumination is inhomogeneous";
break;
}
item.SubItems.Add(text);
item.SubItems.Add(((int)(issue.getScore() * 100.0)) + " %");
ListQualityCheck.Items.AddRange(new ListViewItem[] { item });
}//for
qList = mAssistant.mSeqQualityList;
count = qList.Count;
for (int i = 0; i < count; i++)
{
issue = (QualityIssue)qList[i];
item = new ListViewItem("Sequence");
//Sequences
switch (issue.getIssueType())
{
case CalibrationAssistant.QUALITY_ISSUE_FAILURE:
text = "Quality assessment failed";
break;
case CalibrationAssistant.QUALITY_ISSUE_SEQ_ALL_OVER:
text = "Quality issues detected for some images";
break;
case CalibrationAssistant.QUALITY_ISSUE_SEQ_CALTAB_TILT:
text = "Tilt angles are not covered by sequence";
break;
case CalibrationAssistant.QUALITY_ISSUE_SEQ_MARKS_DISTR:
text = "Field of view is not covered by plate images";
break;
case CalibrationAssistant.QUALITY_ISSUE_SEQ_NUMBER:
text = "Number of images is too low";
break;
case CalibrationAssistant.QUALITY_ISSUE_SEQ_ERROR:
text = "Mark extraction failed for some images";
break;
default:
text = "unknown issue";
break;
}
item.SubItems.Add(text);
item.SubItems.Add(((int)(issue.getScore() * 100.0)) + " %");
ListQualityCheck.Items.AddRange(new ListViewItem[] { item });
}//for
}//end of method
///
/// For each change in the calibration parameter set the basic parts,
/// like finding the calibration plate and the marks, need to be
/// recalculated.
/// The success or failure for detecting each of these basic parts
/// are described for each calibration image by one of the following
/// status messages:
/// ["Plate not found", "Marks not found",
/// "Quality issues detected", "Ok"]
///
public void UpdateImageStatus()
{
ListViewItem item;
int count = ListCalibImg.Items.Count;
for (int i = 0; i < count; i++)
{
item = ListCalibImg.Items[i];
item.SubItems[2].Text = ((CalibImage)mAssistant.getCalibDataAt(i)).getPlateStatus();
}
showLabelErrMessage();
buttonCalibrate.Enabled = (mAssistant.mCanCalib && (mAssistant.mReferenceIndex != -1));
}
/// Displays the calibration results
///
/// Depicts success or failure of the calibration process
///
public void UpdateResultTab(bool CalibSuccess)
{
HTuple campar, reference;
HTuple from = new HTuple(7 * mAssistant.mReferenceIndex);
HTuple to = new HTuple(7 * mAssistant.mReferenceIndex + 6);
if (CalibSuccess)
{
int offset = 0;
mAssistant.getCalibrationResult(out campar, out reference);
StatusCalibLabel.Text = "Calibration successful";
ErrorLabel.Text = mAssistant.mErrorMean.ToString("f5");
FocalLResultLabel.Text = (campar[0].D * 1000.0).ToString("f4");
if (mAssistant.getCameraType() == CalibrationAssistant.CAMERA_TYP_AREA_SCAN_POLY)
{
offset = 4;
K1Label.Text = campar[1].D.ToString("f2");
K2Label.Text = campar[2].D.ToString("e10");
K3Label.Text = campar[3].D.ToString("e10");
P1Label.Text = campar[4].D.ToString("f6");
P2Label.Text = campar[5].D.ToString("f6");
}
else
{
KappaResultLabel.Text = campar[1].D.ToString("f2");
}
SxResultLabel.Text = (campar[2 + offset].D * 1000000.0).ToString("f3");
SyResultLabel.Text = (campar[3 + offset].D * 1000000.0).ToString("f3");
CxResultLabel.Text = campar[4 + offset].D.ToString("f3");
CyResultLabel.Text = campar[5 + offset].D.ToString("f3");
ImgWResultLabel.Text = campar[6 + offset].I + "";
ImgHResultLabel.Text = campar[7 + offset].I + "";
if (campar.Length == 11)
{
VxResultLabel.Text = (campar[8].D * 1000000.0).ToString("f3");
VyResultLabel.Text = (campar[9].D * 1000000.0).ToString("f3");
VzResultLabel.Text = (campar[10].D * 1000000.0).ToString("f3");
}
if (reference.Length >= 6)
{
CamPoseXLabel.Text = (reference[0].D * 1000).ToString("f3");
CamPoseYLabel.Text = (reference[1].D * 1000).ToString("f3");
CamPoseZLabel.Text = (reference[2].D * 1000).ToString("f3");
CamAlphaLabel.Text = (reference[3].D).ToString("f3");
CamBetaLabel.Text = (reference[4].D).ToString("f3");
CamGammaLabel.Text = (reference[5].D).ToString("f3");
}
}
else //leave all fields empty
{
StatusCalibLabel.Text = "No calibration data available";
ErrorLabel.Text = "";
SxResultLabel.Text = "";
SyResultLabel.Text = "";
FocalLResultLabel.Text = "";
KappaResultLabel.Text = "";
CxResultLabel.Text = "";
CyResultLabel.Text = "";
ImgWResultLabel.Text = "";
ImgHResultLabel.Text = "";
VxResultLabel.Text = "";
VyResultLabel.Text = "";
VzResultLabel.Text = "";
CamPoseXLabel.Text = "";
CamPoseYLabel.Text = "";
CamPoseZLabel.Text = "";
CamAlphaLabel.Text = "";
CamBetaLabel.Text = "";
CamGammaLabel.Text = "";
K1Label.Text = "";
K2Label.Text = "";
K3Label.Text = "";
P1Label.Text = "";
P2Label.Text = "";
}
}
/**************************************************************/
private void tabControl_SelectedIndexChanged(object sender, System.EventArgs e)
{
UpdateView();
}
/**************************************************************/
/********* Event on leave - for not to miss on changes *******/
/**************************************************************/
private void FocalLengthUpDown_Leave(object sender, System.EventArgs e)
{
if ((double)FocalLengthUpDown.Value != tFocalLength)
mAssistant.setFocalLength((double)FocalLengthUpDown.Value);
}
private void ThicknessUpDown_Leave(object sender, System.EventArgs e)
{
if ((double)ThicknessUpDown.Value != tThickness)
mAssistant.setThickness((double)ThicknessUpDown.Value);
}
private void SxUpDown_Leave(object sender, System.EventArgs e)
{
if ((double)SxUpDown.Value != tCellWidth)
mAssistant.setCellWidth((double)SxUpDown.Value);
}
private void SyUpDown_Leave(object sender, System.EventArgs e)
{
if ((double)SyUpDown.Value != tCellHeight)
mAssistant.setCellHeight((double)SyUpDown.Value);
}
private void MotionXUpDown_Leave(object sender, System.EventArgs e)
{
if ((double)MotionXUpDown.Value != tMotionX)
mAssistant.setMotionX((double)MotionXUpDown.Value);
}
private void MotionYUpDown_Leave(object sender, System.EventArgs e)
{
if ((double)MotionYUpDown.Value != tMotionY)
mAssistant.setMotionY((double)MotionYUpDown.Value);
}
private void MotionZUpDown_Leave(object sender, System.EventArgs e)
{
if ((double)MotionZUpDown.Value != tMotionZ)
mAssistant.setMotionZ((double)MotionZUpDown.Value);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
UpdateView();
}
private void numericUpDownP2_ValueChanged(object sender, EventArgs e)
{
UpdateView();
}
private void numericUpDownP4_ValueChanged(object sender, EventArgs e)
{
UpdateView();
}
private void numericUpDownP3_ValueChanged(object sender, EventArgs e)
{
UpdateView();
}
}
}