using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using CvBase;
using System.IO;
using System.Drawing.Drawing2D;
using Newtonsoft.Json;
using HalconDotNet;
using CWindowTool;
namespace CvImageTool
{
public class FindCircle : BaseImageOpt
{
#region 私有属性和构造函数
private string cProcessSettingFilePath = null;
private string cImageOptSettingFilePath = null;
private FindCircleForm findCircleForm = null;
private FindCircleTool findCircleTool = null;
private int CProcessIndex;
public HObject measureContour = null;
public HObject resultContour = null;
public HTuple circle;
public HTuple measureCol;
public HTuple measureRow;
public List
public CWindowTool.CWindows[] cCWindows = null;
public FindCircle(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 FindCircleOutput());
//创建算法抓圆图像处理工具
findCircleTool = new FindCircleTool(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, processes, cCWindows);
//创建算法抓圆窗体
findCircleForm = new FindCircleForm(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, description, processes, cCWindows);
}
#endregion
///
/// 算法名称
///
public override string Name { get; set; }
///
/// 算法描述(参数文件的文件名/流程算法显示名称)
///
public override string Description { get; set; }
///
/// 英文名称,用于配置参数的读写和区分
///
public override string EnglishName { get; set; }
///
/// 算法所属流程名称
///
public override string ParentProcessName { get; set; }
///
/// 在所属流程中其索引
///
public override int ImageOptIndex { get; set; }
///
/// 算法运行
///
public override void Running(bool isTest = false)
{
ReadFile();
#region 处理图像参数赋值
string[] imageSelects = findCircleTool.imageSelect.Split('_');
if (imageSelects.Length != 2) return;
if (imageSelects[0] == "相机")
{
if (imageSelects[1] == "灰度图")
findCircleTool.Image = processes[CProcessIndex].ImageGray;
else
findCircleTool.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);
findCircleTool.Image = value;
}
else
{
return;
}
}
#endregion
#region 仿射矩阵参数赋值
string[] matrixSelect;
if (findCircleTool.findCircleParam.affin)
matrixSelect = findCircleTool.affinMatrixSelect.Split('_');
else
matrixSelect = new string[0];
if (matrixSelect.Length != 2 && findCircleTool.findCircleParam.affin) return;
#endregion
//运行算法
findCircleTool.Running(out measureContour, out resultContour, out measureRow, out measureCol, out circle);
#region 输出参数赋值
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Dispose();
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasureContours = measureContour;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].ResultContours = resultContour;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasurePointRow = measureRow;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasurePointCol = measureCol;
if (circle.Length < 3) return;
HTuple center = new HTuple();
center.Append(circle[0]);
center.Append(circle[1]);
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].CenterPoint = center;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Radius = circle[2];
#endregion
#region 添加显示
if (findCircleTool.isShowCircleResultCountour)
{
CObject cObject1 = new CObject();
cObject1._Object = resultContour;
cObject1._Color = "green";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (findCircleTool.isShowCircleMeasuresCountour)
{
CObject cObject1 = new CObject();
cObject1._Object = measureContour;
cObject1._Color = "blue";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (findCircleTool.isShowCircleCenter && circle.Length >= 3)
{
CText cText01 = new CText();
cText01._Text = "X:" + circle[0].D.ToString("0.00") + "; Y:" + circle[1].D.ToString("0.00") + "; R:" + circle[2].D.ToString("0.00");
cText01._X = 5;
cText01._Y = 5;
cText01._Color = "blue";
cCWindows[CProcessIndex].AText.Add(cText01);
}
#endregion
}
///
/// 显示算法相关界面
///
public override void show(int processIndex, int imageIndex)
{
findCircleForm.ShowDialog();
}
///
/// 读取配置文件
///
///
///
///
public override bool ReadFile()
{
return findCircleTool.ReadFile(Description);
}
}
}