using CvBase;
using CWindowTool;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CvImageTool.GrayDiff_MM
{
public class GrayDiff_MM : BaseImageOpt
{
private string cProcessSettingFilePath = null;
private string cImageOptSettingFilePath = null;
private GrayDiff_MMForm grayDiff_MMForm = null;
private GrayDiff_MMTool grayDiff_MMTool = null;
private int CProcessIndex;
public HObject ROI = null;
public HObject resultRegion = null;
public HTuple x;
public HTuple y;
public HTuple area;
public List<BaseProcess> processes;
public CWindowTool.CWindows[] cCWindows;
public GrayDiff_MM(int processIndex, int imageOptIndex, string description, EnumOrStruct.RunProcessOrign runProcessOrign)
{
switch (runProcessOrign)
{
case EnumOrStruct.RunProcessOrign.MainProcess:
processes = Refrence.Instance.baseProcesses.ToList();
cCWindows = Refrence.Instance.CWindows;
//获取流程配置文件和算法参数文件路径
cProcessSettingFilePath = Path.Combine(AppSetting.Instance.SubAppSettingRootPath, processes[processIndex].Name);
if (!Directory.Exists(cProcessSettingFilePath)) Directory.CreateDirectory(cProcessSettingFilePath);
cImageOptSettingFilePath = Path.Combine(cProcessSettingFilePath, description) + ".json";
cProcessSettingFilePath = Path.Combine(cProcessSettingFilePath, AppSetting.Instance.ProcessFormSettingName);
break;
case EnumOrStruct.RunProcessOrign.CalibrationProcess:
processes = Refrence.Instance.calProcesses.ToList();
cCWindows = Refrence.Instance.calCWindows;
cProcessSettingFilePath = Path.Combine(AppSetting.Instance.CalGroupedProcessDir, processes[processIndex].Name);
if (!Directory.Exists(cProcessSettingFilePath)) Directory.CreateDirectory(cProcessSettingFilePath);
cImageOptSettingFilePath = Path.Combine(cProcessSettingFilePath, description) + ".json";
cProcessSettingFilePath = Path.Combine(cProcessSettingFilePath, AppSetting.Instance.ProcessSettingFileName);
break;
}
this.Description = description;
this.CProcessIndex = processIndex;
this.ImageOptIndex = imageOptIndex;
//将当前算法的输入输出参数添加到公共资源类的输入输出列表
processes[processIndex].BaseImageOutParams.Add(new GrayDiff_MMOutput());
//创建算法抓圆图像处理工具
grayDiff_MMTool = new GrayDiff_MMTool(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, description, processes, cCWindows);
//创建算法抓圆窗体
grayDiff_MMForm = new GrayDiff_MMForm(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, description, processes, cCWindows);
}
/// </summary>
public override string Name { get; set; }
/// <summary>
/// 算法描述(参数文件的文件名/流程算法显示名称)
/// </summary>
public override string Description { get; set; }
/// <summary>
/// 英文名称,用于配置参数的读写和区分
/// </summary>
public override string EnglishName { get; set; }
/// <summary>
/// 算法所属流程名称
/// </summary>
public override string ParentProcessName { get; set; }
/// <summary>
/// 在所属流程中其索引
/// </summary>
public override int ImageOptIndex { get; set; }
/// <summary>
/// 算法运行
/// </summary>
public override void Running(bool isTest = false)
{
ReadFile();
#region 处理图像参数赋值
string[] imageSelects = grayDiff_MMTool.imageSelect.Split('_');
if (imageSelects.Length != 2) return;
if (imageSelects[0] == "相机")
{
if (imageSelects[1] == "灰度图")
{
grayDiff_MMTool.Image = processes[CProcessIndex].ImageGray;
}
else
{
grayDiff_MMTool.Image = processes[CProcessIndex].ImageHeight;
}
}
else
{
ProcessTool.Instance.FindImageOpt(processes[CProcessIndex], imageSelects[0], out BaseImageOpt paramByImageOpt1, out int paramByImageOptIndex1);
if (paramByImageOptIndex1 >= 0)
{
processes[CProcessIndex].BaseImageOutParams[paramByImageOptIndex1].GetHObjectValue(imageSelects[1], out HObject value);
grayDiff_MMTool.Image = value;
}
else
{
return;
}
}
#endregion
#region 仿射矩阵参数赋值
string[] matrixSelect;
if (grayDiff_MMTool.grayDiff_MMParam.isAffin)
matrixSelect = grayDiff_MMTool.affinMatrixSelect.Split('_');
else
matrixSelect = new string[0];
if (matrixSelect.Length != 2 && grayDiff_MMTool.grayDiff_MMParam.isAffin) return;
#endregion
grayDiff_MMTool.Running(out ROI, out resultRegion, out x, out y, out area);
#region 输出参数赋值
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Dispose();
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasureContours = ROI;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].ResultRegions = resultRegion;
if (x.Length > 0)
{
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasurePointCol = x;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasurePointRow = y;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].CenterPoint = y;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].CenterPoint.Append(x);
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Area = area;
}
#endregion
#region 添加显示
if (grayDiff_MMTool.isShowResultRegion)
{
CObject cObject1 = new CObject();
cObject1._Object = resultRegion;
cObject1._Color = "green";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (grayDiff_MMTool.isShowROI)
{
CObject cObject1 = new CObject();
cObject1._Object = ROI;
cObject1._Color = "blue";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (grayDiff_MMTool.isShowResult && x.Length > 4)
{
CText cText01 = new CText();
cText01._Text = "Area:" + area.TupleSum().D.ToString("0.00");
cText01._X = 5;
cText01._Y = 5;
cText01._Color = "blue";
cCWindows[CProcessIndex].AText.Add(cText01);
}
#endregion
}
/// <summary>
/// 显示算法相关界面
/// </summary>
public override void show(int processIndex, int imageIndex)
{
grayDiff_MMForm.ShowDialog();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public override bool ReadFile()
{
return grayDiff_MMTool.ReadFile(Description);
}
}
}