using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvBase;
using HalconDotNet;
using CWindowTool;
using System.IO;
namespace CvImageTool.FindContourByBoundary
{
public class FindContourByBoundary : BaseImageOpt
{
private string cProcessSettingFilePath = null;
private string cImageOptSettingFilePath = null;
private FindContourByBoundaryForm findContourByBoundaryForm = null;
private FindContourByBoundaryTool findContourByBoundaryTool = null;
private int CProcessIndex;
public HObject ROI = null;
public HObject resultContour = null;
public HObject measureContour;
//public HObject cross;
//public HTuple num;
public List<BaseProcess> processes;
public CWindowTool.CWindows[] cCWindows;
public FindContourByBoundary(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 FindContourByBoundaryOutput());
//创建算法抓圆图像处理工具
findContourByBoundaryTool = new FindContourByBoundaryTool(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, description, processes, cCWindows);
//创建算法抓圆窗体
findContourByBoundaryForm = new FindContourByBoundaryForm(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 = findContourByBoundaryTool.imageSelect.Split('_');
if (imageSelects.Length != 2) return;
if (imageSelects[0] == "相机")
{
if (imageSelects[1] == "灰度图")
{
findContourByBoundaryTool.Image = processes[CProcessIndex].ImageGray;
}
else
{
findContourByBoundaryTool.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);
findContourByBoundaryTool.Image = value;
}
else
{
return;
}
}
#endregion
#region 仿射矩阵参数赋值
//string[] matrixSelect;
//if (contourDetectionByDistancePPTool.contourDetectionByDistancePPParam)
// matrixSelect = contourDetectionByDistancePPTool.affinMatrixSelect.Split('_');
//else
// matrixSelect = new string[0];
//if (matrixSelect.Length != 2 && contourDetectionByDistancePPTool.grayDiff_MMParam.isAffin) return;
#endregion
findContourByBoundaryTool.Running(out measureContour, out resultContour, out ROI);
#region 输出参数赋值
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Dispose();
//processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasureContours = ROI;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].ResultContours = resultContour;
#endregion
#region 添加显示
if (findContourByBoundaryTool.isShowMeasureROI)
{
CObject cObject1 = new CObject();
cObject1._Object = ROI;
cObject1._Color = "blue";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (findContourByBoundaryTool.isShowMeasureCountour)
{
CObject cObject1 = new CObject();
cObject1._Object = measureContour;
cObject1._Color = "blue";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
//if (num.Length > 0)
//{
//CText cText01 = new CText();
//cText01._Text = "Area:" + num.TupleSum().D.ToString();
//cText01._X = 100;
//cText01._Y = 100;
//cText01._Color = "blue";
//cCWindows[CProcessIndex].AText.Add(cText01);
if (findContourByBoundaryTool.isShowResultCountour)
{
CObject cObject1 = new CObject();
cObject1._Object = resultContour;
cObject1._Color = "green";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
//}
#endregion
}
/// <summary>
/// 显示算法相关界面
/// </summary>
public override void show(int processIndex, int imageIndex)
{
findContourByBoundaryForm.ShowDialog();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public override bool ReadFile()
{
return findContourByBoundaryTool.ReadFile(Description);
}
}
}