机器视觉通用平台之线线距离算法工具类
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.DistanceLL
{
public class DistanceLLTool
{
public FileINI INI = new FileINI();
public DistancePLParam distanceLLParam; //匹配参数
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 Line01;
public HTuple Line02;
private List<BaseProcess> process;
private CWindows[] cCWindows;
public DistanceLLTool(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;
distanceLLParam = new DistancePLParam();
Image = new HObject();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
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));
distanceLLParam = JsonConvert.DeserializeObject<DistancePLParam>(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, "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(distanceLLParam, Formatting.Indented);
File.WriteAllText(cImageOptSettingFilePath, json);
return true;
}
public void Running(out HObject MearsureLine, out HObject Line, out HTuple dist)
{
MearsureLine = new HObject(); 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 Line01);
else
Line01 = 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 Line02);
else
Line02 = null;
if (Line01 == null || Line02 == null || Line02.Length < 4 || Line01.Length < 4) return;
HOperatorSet.SetSystem("clip_region", "false");
//计算直线距离
HTuple row = (Line01[0] + Line01[2]) / 2.0;
HTuple col = (Line01[1] + Line01[3]) / 2.0;
HOperatorSet.DistancePl(row, col, Line02[0], Line02[1], Line02[2], Line02[3], out dist);
HOperatorSet.ProjectionPl(row, col, Line02[0], Line02[1], Line02[2], Line02[3], out HTuple nr, out HTuple nc);
HTuple lRow = new HTuple();
HTuple lCol = new HTuple();
lRow.Append(row);
lRow.Append(nr);
lCol.Append(col);
lCol.Append(nc);
HOperatorSet.GenContourPolygonXld(out HObject Line1, lRow, lCol);
lRow = new HTuple();
lCol = new HTuple();
lRow.Append(Line01[0]);
lRow.Append(nr);
lCol.Append(Line01[1]);
lCol.Append(nc);
HOperatorSet.GenContourPolygonXld(out HObject Line2, lRow, lCol);
HOperatorSet.ConcatObj(Line1, Line2, out Line);
Line01.Dispose();
Line02.Dispose();
lRow = new HTuple();
lCol = new HTuple();
lRow.Append(Line01[0]);
lRow.Append(Line01[2]);
lCol.Append(Line01[1]);
lCol.Append(Line01[3]);
HOperatorSet.GenContourPolygonXld(out HObject L1, lRow, lCol);
lRow = new HTuple();
lCol = new HTuple();
lRow.Append(Line02[0]);
lRow.Append(Line02[2]);
lCol.Append(Line02[1]);
lCol.Append(Line02[3]);
HOperatorSet.GenContourPolygonXld(out HObject L2, lRow, lCol);
//HOperatorSet.GenRegionLine(out Line, row, col, nr, nc);
//HOperatorSet.GenRegionLine(out HObject L1, Line01[0], Line01[1], Line01[2], Line01[3]);
//HOperatorSet.GenRegionLine(out HObject L2, Line02[0], Line02[1], Line02[2], Line02[3]);
HOperatorSet.ConcatObj(L1, L2, out MearsureLine);
L1.Dispose(); L2.Dispose();
}
}
}
更多推荐
所有评论(0)