using Bro.Common.Base;
|
using Bro.Common.Helper;
|
using Bro.Common.Interface;
|
using Bro.Common.Model;
|
using Bro.UI.Model.Winform;
|
using HalconDotNet;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Runtime.CompilerServices;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using static Bro.Common.Helper.EnumHelper;
|
|
namespace Bro.Process
|
{
|
public partial class ProcessControl
|
{
|
protected FrmCalibration_9P _frmCalibration_9P = null;
|
public int MultipleStepsCalibration(CameraBase camera,
|
IMotion motionDevice,
|
CalibrationConfig config,
|
Action<CameraBase, CalibrationConfig, string, int> singleResultAction,
|
Func<CameraBase, IMotion, CalibrationConfig, AutoWaitConfirm, string> finalCalculation,
|
[CallerMemberName]string methodName = "")
|
{
|
if (config.InputPara == null || config.InputPara.Count <= 0)
|
{
|
config.Results.ForEach(u => u.Image = null);
|
|
_frmCalibration_9P = new FrmCalibration_9P(camera, motionDevice, config, singleResultAction, finalCalculation, ResetHalconTool, methodName);
|
Task.Run(() =>
|
{
|
_frmCalibration_9P.FormClosed += (s, e) =>
|
{
|
_frmCalibration_9P?.Dispose();
|
_frmCalibration_9P = null;
|
};
|
_frmCalibration_9P.ShowDialog();
|
});
|
|
return (int)ReplyValue.NG;
|
}
|
|
if (config.InputPara[0] <= 0 || config.InputPara[0] > config.Results.Count)
|
{
|
config.InputPara = null;
|
return (int)ReplyValue.IGNORE;
|
}
|
|
int sequence = config.InputPara[0];
|
|
//第一次 sequence从1开始
|
if (sequence == 1)
|
{
|
config.Results.ForEach(c =>
|
{
|
c.Image = null;
|
c.OfflineImagePath = "";
|
c.CurrentPlatPoint = new CustomizedPoint();
|
c.ImageMarkPoint = new CustomizedPoint();
|
});
|
}
|
|
singleResultAction(camera, config, methodName, sequence);
|
|
//获取当前平台点位
|
var locationList = motionDevice.GetCurrentAxisInfo("X", "Y").Select(u => u.AxisLocation).ToList();
|
config.Results[sequence - 1].CurrentPlatPoint = new CustomizedPoint(locationList[0], locationList[1]);
|
|
//和标定界面互动 1.输出结果显示 2.部分人工确认操作
|
_frmCalibration_9P?.OnCalibStepDone(sequence);
|
|
//最后一次 和标定界面互动 1.最终计算结果显示 2.人工确认是否写入配置(不是保存操作)
|
string finalResult = "";
|
if (sequence == config.Results.Count)
|
{
|
AutoWaitConfirm saveConfigConfirm = new AutoWaitConfirm()
|
{
|
WaitHandle = new AutoResetEvent(true),
|
WaitResult = true,
|
};
|
|
if (_frmCalibration_9P != null)
|
{
|
saveConfigConfirm.WaitHandle.Reset();
|
saveConfigConfirm.WaitResult = false;
|
}
|
|
finalResult = finalCalculation?.Invoke(camera, motionDevice, config, saveConfigConfirm);
|
|
_frmCalibration_9P?.OnCalibFinalDone(finalResult, saveConfigConfirm);
|
}
|
|
|
//if (_frmCalibration_9P?.OnCalibFinalDone(finalResult) ?? true)
|
//{
|
// modifyConfigAction?.Invoke(camera, motionDevice);
|
//}
|
|
config.InputPara = null;
|
return (int)ReplyValue.OK;
|
}
|
|
public void GetCalibrationPoints(CameraBase camera, CalibrationConfig config, string methodName, int sequence)
|
{
|
//string imgSetId = "";
|
List<IShapeElement> pointList = new List<IShapeElement>();
|
|
IImageSet set = CollectHImage(camera, config.CameraOpConfig);
|
if (set == null)
|
return;
|
|
using (HObject hImage = set.HImage)
|
{
|
var tool = GetHalconTool(config.CameraOpConfig);
|
|
tool.InputImageDic["INPUT_Image"] = hImage;
|
if (!tool.RunProcedure(out string error))
|
{
|
throw new ProcessException($"{methodName}算法执行异常:{error}");
|
}
|
|
var xList = tool.GetResultTuple("OUTPUT_X").HTupleToDouble();
|
var yList = tool.GetResultTuple("OUTPUT_Y").HTupleToDouble();
|
|
for (int i = 0; i < xList.Count; i++)
|
{
|
pointList.Add(new PointIndicator((float)xList[i], (float)yList[i]));
|
}
|
|
camera.SaveFitImage(pointList, set.Id);
|
}
|
|
config.Results[sequence - 1].ImageMarkPoint = new CustomizedPoint((pointList[0] as PointIndicator).Center);
|
}
|
|
public List<double> GetMatrixByCalibrationResult(CalibrationConfig config, out string msg)
|
{
|
List<double> list = new List<double>();
|
|
//try
|
{
|
var xList = config.Results.Select(u => u.CurrentPlatPoint.X).ToArray();
|
var yList = config.Results.Select(u => u.CurrentPlatPoint.Y).ToArray();
|
var uList = config.Results.Select(u => u.ImageMarkPoint.X).ToArray();
|
var vList = config.Results.Select(u => u.ImageMarkPoint.Y).ToArray();
|
|
HOperatorSet.VectorToHomMat2d(new HTuple(uList), new HTuple(vList), new HTuple(xList), new HTuple(yList), out HTuple matrix);
|
|
double sum = 0;
|
for (int i = 0; i < xList.Length; i++)
|
{
|
HOperatorSet.AffineTransPoint2d(matrix, uList[i], vList[i], out HTuple m, out HTuple n);
|
|
sum += Math.Sqrt((Math.Pow((m.D - xList[i]), 2) + Math.Pow((n.D - yList[i]), 2)));
|
}
|
|
sum = (sum / (double)xList.Length);
|
|
//设置偏移量为0
|
//matrix[2] = matrix[5] = 0;
|
|
list = matrix.HTupleToDouble();
|
|
msg = $"标定点数量:{xList.Length}; 单点误差:{sum.ToString()}脉冲; 矩阵:{string.Join(" ", list)}";
|
}
|
//catch (Exception ex)
|
//{
|
// msg = ex.Message;
|
//}
|
//LogAsync(DateTime.Now, $"{camera.Name}标定完成", msg);
|
|
return list;
|
}
|
}
|
}
|