机器视觉通用平台之线与X轴角度算法工具类

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

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

        public AngleLxTool(int cProcessIndex, int cImageOptIndex,
           string cProcessSettingFilePath, string cImageOptSettingFilePath,
           List<BaseProcess> processes, CWindows[] cWindows)
        {
            this.cProcessSettingFilePath = cProcessSettingFilePath;
            this.cImageOptSettingFilePath = cImageOptSettingFilePath;
            this.cProcessIndex = cProcessIndex;
            this.cImageOptIndex = cImageOptIndex;
            this.process = processes;
            this.cCWindows = cWindows;
            angleLxParam = new AngleLxParam();
            Image = new HObject();
        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="cProcessSettingFilePath"></param>
        /// <param name="cImgeOptSettingFilePath"></param>
        /// <returns></returns>
        public bool ReadFile(string cProcessDescription)
        {
            imageSelect = ""; line1Select = "";
            if (!File.Exists(cProcessSettingFilePath) || !File.Exists(cImageOptSettingFilePath)) return false;
            imageSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Image", "", cProcessSettingFilePath);
            line1Select = FileINI.ReadValueFromIniFile(cProcessDescription, "Line1", "", cProcessSettingFilePath);
            isShowResult = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResult", "", cProcessSettingFilePath));
            isShowMeasureLines = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowMeasureLines", "", cProcessSettingFilePath));
            angleLxParam = JsonConvert.DeserializeObject<AngleLxParam>(File.ReadAllText(cImageOptSettingFilePath));
            return true;
        }

        /// <summary>
        /// 写配置文件
        /// </summary>
        /// <param name="cProcessSettingFilePath"></param>
        /// <param name="cImgeOptSettingFilePath"></param>
        /// <returns></returns>
        public bool WriteFile(string cProcessDescription)
        {
            FileINI.WriteValueFromIniFile(cProcessDescription, "Image", imageSelect, cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "Line1", line1Select, cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResult", isShowResult.ToString(), cProcessSettingFilePath);
            FileINI.WriteValueFromIniFile(cProcessDescription, "isShowMeasureLines", isShowMeasureLines.ToString(), cProcessSettingFilePath);
            var json = JsonConvert.SerializeObject(angleLxParam, Formatting.Indented);
            File.WriteAllText(cImageOptSettingFilePath, json);
            return true;
        }

        public void Running(out HObject MearsureLine, out HTuple angle)
        {
            MearsureLine = new HObject(); angle = 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 Line01);
            else
                Line01 = null;
            if (Line01 == null || Line01.Length < 4) return;

            HOperatorSet.AngleLx(Line01[0], Line01[1], Line01[2], Line01[3], out angle);
            angle = angle.D / Math.PI * 180.0;
            //计算直线距离
            //HOperatorSet.GenRegionLine(out MearsureLine, Line01[0], Line01[1], Line01[2], Line01[3]);
            HTuple lRow = new HTuple();
            HTuple lCol = new HTuple();
            lRow.Append(Line01[0]);
            lRow.Append(Line01[2]);
            lCol.Append(Line01[1]);
            lCol.Append(Line01[3]);
            HOperatorSet.GenContourPolygonXld(out MearsureLine, lRow, lCol);
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值