using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using CWindowTool;
using CvBase;
using System.IO;

namespace CvImageTool.FindShapeModelByXld
{
    public class FindShapeModelByXld : BaseImageOpt
    {
        private string cProcessSettingFilePath = null;
        private string cImageOptSettingFilePath = null;
        private string cCutImageFilePath = null;
        private string cModelHandleFilePatH = null;
        private FindShapeModelByXldForm findShapeModelByXldForm = null;
        private FindShapeModelByXldTool findShapeModelByXldTool = null;
        private int CProcessIndex;
        public HObject ROI = null;
        public HObject resultContour = null;
        public HTuple row;
        public HTuple col;
        public HTuple angle;
        public HTuple score;

        public List<BaseProcess> processes = null;
        public CWindows[] cCWindows;

        public FindShapeModelByXld(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";
                    cModelHandleFilePatH = Path.Combine(cProcessSettingFilePath, description) + ".sh";
                    cCutImageFilePath = Path.Combine(cProcessSettingFilePath, description) + ".hobj";
                    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";
                    cModelHandleFilePatH = Path.Combine(cProcessSettingFilePath, description) + ".sh";
                    cCutImageFilePath = Path.Combine(cProcessSettingFilePath, description) + ".hobj";
                    cProcessSettingFilePath = Path.Combine(cProcessSettingFilePath, AppSetting.Instance.ProcessSettingFileName);
                    break;
            }
            this.Description = description;
            this.CProcessIndex = processIndex;
            this.ImageOptIndex = imageOptIndex;

            //将当前算法的输入输出参数添加到公共资源类的输入输出列表
            processes[processIndex].BaseImageOutParams.Add(new FindShapeModelByXldOutput());
            //创建算法抓圆图像处理工具
            findShapeModelByXldTool = new FindShapeModelByXldTool(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, cCutImageFilePath, cModelHandleFilePatH, processes, cCWindows);
            //创建算法抓圆窗体
            findShapeModelByXldForm = new FindShapeModelByXldForm(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, description, cCutImageFilePath, cModelHandleFilePatH, processes, cCWindows);
        }

        /// <summary>
        /// 算法名称
        /// </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 = findShapeModelByXldTool.imageSelect.Split('_');
            if (imageSelects.Length != 2) return;
            if (imageSelects[0] == "相机")
            {
                if (imageSelects[1] == "灰度图")
                    findShapeModelByXldTool.Image = processes[CProcessIndex].ImageGray;
                else
                    findShapeModelByXldTool.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);
                    findShapeModelByXldTool.Image = value;
                }
                else
                {
                    return;
                }
            }
            #endregion

            #region 仿射矩阵参数赋值
            string[] matrixSelect;
            if (findShapeModelByXldTool.findShapeModelByXldParam.Affin)
                matrixSelect = findShapeModelByXldTool.affinMatrixSelect.Split('_');
            else
                matrixSelect = new string[0];
            if (matrixSelect.Length != 2 && findShapeModelByXldTool.findShapeModelByXldParam.Affin) return;
            #endregion

            //运行算法
            findShapeModelByXldTool.Running(out resultContour, out row, out col, out angle, out score);
            HTuple HomMat2D = null;
            if (score.Length > 0)
            {
                if (findShapeModelByXldTool.findShapeModelByXldParam.AngleEnable)
                    HOperatorSet.VectorAngleToRigid(findShapeModelByXldTool.findShapeModelByXldParam.ModelCenterRow,
                        findShapeModelByXldTool.findShapeModelByXldParam.ModelCenterCol,
                        findShapeModelByXldTool.findShapeModelByXldParam.ModelCenterAngle, row, col, angle, out HomMat2D);
                else
                    HOperatorSet.VectorAngleToRigid(findShapeModelByXldTool.findShapeModelByXldParam.ModelCenterRow,
                        findShapeModelByXldTool.findShapeModelByXldParam.ModelCenterCol,
                        0, row, col, 0, out HomMat2D);
            }
            else
            {
                HOperatorSet.VectorAngleToRigid(0, 0, 0, 0, 0, 0, out HomMat2D);
            }

            #region 输出参数赋值
            processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Dispose();
            processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].ResultContours = resultContour;
            processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].AffinMatrix = HomMat2D;
            HTuple center = new HTuple();
            center.Append(row);
            center.Append(col);
            processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].CenterPoint = center;
            processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Angle = angle;
            processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Score = score;
            #endregion

            #region 添加显示
            if (findShapeModelByXldTool.isShowModelContour)
            {
                CObject cObject1 = new CObject();
                cObject1._Object = resultContour;
                cObject1._Color = "green";
                cObject1._Draw = false;
                cCWindows[CProcessIndex].AObject.Add(cObject1);
            }
            if (findShapeModelByXldTool.isShowROI)
            {
                CObject cObject1 = new CObject();
                HOperatorSet.GenRectangle1(out HObject ROI, findShapeModelByXldTool.findShapeModelByXldParam.ROIStartRow,
                    findShapeModelByXldTool.findShapeModelByXldParam.ROIStartColumn, findShapeModelByXldTool.findShapeModelByXldParam.ROIEndRow,
                    findShapeModelByXldTool.findShapeModelByXldParam.ROIEndColumn);
                processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasureContours = ROI;
                cObject1._Object = ROI;
                cObject1._Color = "blue";
                cObject1._Draw = false;
                cCWindows[CProcessIndex].AObject.Add(cObject1);
            }
            if (findShapeModelByXldTool.isShowModelResult && score.Length >= 1)
            {
                CText cText01 = new CText();
                cText01._Text = "X:" + col.D.ToString("0.00") + "; Y:" + row.D.ToString("0.00") + "; A:" + angle.D.ToString("0.00") + "; Score:" + score.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)
        {
            findShapeModelByXldForm.ShowDialog();
        }


        //public abstract bool ReadFile(string cProcessSettingFilePath, string cImgeOptSettingFilePath);
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="cProcessSettingFilePath"></param>
        /// <param name="cImgeOptSettingFilePath"></param>
        /// <returns></returns>
        public override bool ReadFile()
        {
            return findShapeModelByXldTool.ReadFile(Description);
        }

    }
}
 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐