机器视觉通用平台之点点距离算法工具类

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

namespace CvImageTool.DistancePP
{
    public class DistancePPTool
    {
        public FileINI INI = new FileINI();
        public DistancePPParam distancePPParam;    //匹配参数
        private string cProcessSettingFilePath;     //当前流程配置文件的路径
        private string cImageOptSettingFilePath;    //当前算法参数文件路径
        public string imageSelect = "";            //图像输入参数选择项
        public string line1Select = "";      //直线1输入参数选择项
        public string line2Select = "";      //直线2输入参数选择项
        private int cProcessIndex = -1;             //当前归属流程的索引
        private int cImageOptIndex = -1;            //当前归属算法的索引
        public bool isShowResultLine = false;
        public bool isShowResult = false;
        public bool isShowMeasureLines = false;
        public HObject Image;
        public HTuple Point1;
        public HTuple Point2;
        private List process;
        private CWindows[] cCWindows;

        public DistancePPTool(int cProcessIndex, int cImageOptIndex,
           string cProcessSettingFilePath, string cImageOptSettingFilePath,
           List processes, CWindows[] cWindows)
        {
            this.cProcessSettingFilePath = cProcessSettingFilePath;
            this.cImageOptSettingFilePath = cImageOptSettingFilePath;
            this.cProcessIndex = cProcessIndex;
            this.cImageOptIndex = cImageOptIndex;
            this.process = processes;
            this.cCWindows = cWindows;
            distancePPParam = new DistancePPParam();
            Image = new HObject();
        }

        ///


        /// 读取配置文件
        ///

        ///
        ///
        ///
        public bool ReadFile(string cProcessDescription)
        {
            imageSelect = ""; line1Select = ""; line2Select = "";
            if (!File.Exists(cProcessSettingFilePath) || !File.Exists(cImageOptSettingFilePath)) return false;
            imageSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Image", "", cProcessSettingFilePath);
            line1Select = FileINI.ReadValueFromIniFile(cProcessDescription, "Line1", "", cProcessSettingFilePath);
            line2Select = FileINI.ReadValueFromIniFile(cProcessDescription, "Line2", "", cProcessSettingFilePath);
            isShowResultLine = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResultLine", "", cProcessSettingFilePath));
            isShowResult = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResult", "", cProcessSettingFilePath));
            isShowMeasureLines = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowMeasureLines", "", cProcessSettingFilePath));
            distancePPParam = JsonConvert.DeserializeObject(File.ReadAllText(cImageOptSettingFilePath));
            return true;
        }

        ///


        /// 写配置文件
        ///

        ///
        ///
        ///
        public bool WriteFile(string cProcessDescription)
        {
            FileINI.WriteValueFromIniFile(cProcessDescription, "Image", imageSelect, cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "Line1", line1Select, cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "Line2", line2Select, cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResultLine", isShowResultLine.ToString(), cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResult", isShowResult.ToString(), cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "isShowMeasureLines", isShowMeasureLines.ToString(), cProcessSettingFilePath);
            var json = JsonConvert.SerializeObject(distancePPParam, Formatting.Indented);
            File.WriteAllText(cImageOptSettingFilePath, json);
            return true;
        }

        public void Running(out HObject Line, out HTuple dist)
        {
            Line = new HObject(); dist = new HTuple();
            //获取直线1
            string[] imageOptAndParamNameL1 = line1Select.Split('_');
            ProcessTool.Instance.FindImageOpt(process[cProcessIndex], imageOptAndParamNameL1[0], out BaseImageOpt byImageOpt, out int byIndex);
            if (byIndex >= 0)
                process[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamNameL1[1], out Point1);
            else
                Point1 = null;
            //获取直线2
            string[] imageOptAndParamNameL2 = line2Select.Split('_');
            byIndex = -1;
            ProcessTool.Instance.FindImageOpt(process[cProcessIndex], imageOptAndParamNameL2[0], out byImageOpt, out byIndex);
            if (byIndex >= 0)
                process[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamNameL2[1], out Point2);
            else
                Point2 = null;
            if (Point2 == null || Point1 == null || Point2.Length < 2 || Point1.Length < 2) return;
            HOperatorSet.SetSystem("clip_region", "false");
            HOperatorSet.DistancePp(Point1[0], Point1[1], Point2[0], Point2[1], out dist);
            //HOperatorSet.GenRegionLine(out Line, Point1[0], Point1[1], Point2[0], Point2[1]);
            HTuple lRow = new HTuple();
            HTuple lCol = new HTuple();
            lRow.Append(Point1[0]);
            lRow.Append(Point2[0]);
            lCol.Append(Point1[1]);
            lCol.Append(Point2[1]);
            HOperatorSet.GenContourPolygonXld(out Line, lRow, lCol);
        }
    }
}
 

你可能感兴趣的:(机器视觉通用平台之点点距离算法工具类)