mirror of
https://github.com/eggplantlwj/VisionEdit.git
synced 2026-04-01 22:56:40 +08:00
1、添加PMA匹配工具
2、建立工程概念,对工程进行保存和恢复 3、将图像显示窗口更改为SmartwindowControl 4、其他bug修复
This commit is contained in:
976
PMAlignTool/FormPMAlignTool.Designer.cs
generated
976
PMAlignTool/FormPMAlignTool.Designer.cs
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,7 @@
|
||||
using ChoiceTech.Halcon.Control;
|
||||
using CommonMethods;
|
||||
using HalconDotNet;
|
||||
using Logger;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@@ -16,12 +17,13 @@ namespace PMAlignTool
|
||||
{
|
||||
public partial class FormPMAlignTool : Form
|
||||
{
|
||||
private PMAlign myPMAlign = null;
|
||||
private IToolInfo myToolInfo = null;
|
||||
private PMAlign myPMAlign = new PMAlign();
|
||||
public IToolInfo myToolInfo = new IToolInfo();
|
||||
public HWindowTool_Smart myHwindow = new HWindowTool_Smart();
|
||||
private HDrawingObject selected_drawing_object = new HDrawingObject();
|
||||
private List<HDrawingObject> templateModelListAdd = new List<HDrawingObject>() { };
|
||||
private List<HDrawingObject> templateModelListSub = new List<HDrawingObject>() { };
|
||||
public List<HDrawingObject> templateModelListAdd = new List<HDrawingObject>() { };
|
||||
public List<HDrawingObject> templateModelListSub = new List<HDrawingObject>() { };
|
||||
|
||||
public FormPMAlignTool(ref object pmalign)
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -30,6 +32,7 @@ namespace PMAlignTool
|
||||
{
|
||||
myToolInfo = (IToolInfo)pmalign;
|
||||
myPMAlign = (PMAlign)myToolInfo.tool;
|
||||
myPMAlign.toolName = myToolInfo.FormToolName;
|
||||
myPMAlign.DispImage();
|
||||
}
|
||||
}
|
||||
@@ -70,15 +73,22 @@ namespace PMAlignTool
|
||||
myHwindow.Dock = DockStyle.Fill;
|
||||
InitTool();
|
||||
}
|
||||
|
||||
bool isInitTool = false;
|
||||
private void InitTool()
|
||||
{
|
||||
isInitTool = true;
|
||||
cNumErosionValue1.Value = myPMAlign.imageProcess.erosionValue1.algValue;
|
||||
cbCErosion1.Checked = myPMAlign.imageProcess.erosionValue1.isEnable;
|
||||
cmbErsion1.TextStr = myPMAlign.imageProcess.erosionValue1.algName;
|
||||
|
||||
cNumDilationValue2.Value = myPMAlign.imageProcess.dilationValue.algValue;
|
||||
cbCDilation1.Checked = myPMAlign.imageProcess.dilationValue.isEnable;
|
||||
cmbDilation.TextStr = myPMAlign.imageProcess.dilationValue.algName;
|
||||
|
||||
cNumErosionValue2.Value = myPMAlign.imageProcess.erosionValue2.algValue;
|
||||
cbCErosion2.Checked = myPMAlign.imageProcess.erosionValue2.isEnable;
|
||||
cmbErsion2.TextStr = myPMAlign.imageProcess.erosionValue2.algName;
|
||||
isInitTool = false;
|
||||
}
|
||||
|
||||
private void btnAcqNewModelImage_Click(object sender, EventArgs e)
|
||||
@@ -121,10 +131,12 @@ namespace PMAlignTool
|
||||
myHwindow.DispHWindow.AttachDrawingObjectToWindow(temp_object);
|
||||
if(rdo_templateRegionAdd.Checked)
|
||||
{
|
||||
temp_object.SetDrawingObjectParams("color","green");
|
||||
templateModelListAdd.Add(temp_object);
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_object.SetDrawingObjectParams("color", "red");
|
||||
templateModelListSub.Add(temp_object);
|
||||
}
|
||||
}
|
||||
@@ -156,27 +168,124 @@ namespace PMAlignTool
|
||||
selectObj.Dispose();
|
||||
}
|
||||
|
||||
public HObject contour;
|
||||
private void btnCreateModel_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(myPMAlign.modelID == -1)
|
||||
if(myPMAlign.inputImage != null)
|
||||
{
|
||||
myPMAlign.oldTrainImage = myPMAlign.inputImage;
|
||||
if(myPMAlign.CreateModelTemplate() == 0)
|
||||
{
|
||||
if (myPMAlign.matchMode == MatchMode.BasedShape)
|
||||
{
|
||||
HOperatorSet.GetShapeModelContours(out contour, myPMAlign.modelID, (HTuple)1);
|
||||
HTuple area, row, col;
|
||||
HOperatorSet.AreaCenter(myPMAlign.templateRegion, out area, out row, out col);
|
||||
HTuple homMat2D;
|
||||
HOperatorSet.HomMat2dIdentity(out homMat2D);
|
||||
HOperatorSet.HomMat2dTranslate(homMat2D, row, col, out homMat2D);
|
||||
HOperatorSet.AffineTransContourXld(contour, out contour, homMat2D);
|
||||
}
|
||||
myHwindow.DispHWindow.ClearWindow();
|
||||
hWindowTool_Smart1.DispHWindow.ClearWindow();
|
||||
myHwindow.DispImage(myPMAlign.inputImage);
|
||||
//在模板窗口显示模板
|
||||
HTuple row1, col1, row2, col2;
|
||||
HOperatorSet.SmallestRectangle1(myPMAlign.templateRegion, out row1, out col1, out row2, out col2);
|
||||
HObject outRectangle1;
|
||||
HOperatorSet.GenRectangle1(out outRectangle1, row1 - 20, col1 - 20, row2 + 20, col2 + 20);
|
||||
HObject imageReduced, imagePart;
|
||||
HOperatorSet.ReduceDomain(myPMAlign.inputImage, outRectangle1, out imageReduced);
|
||||
HObject outBoundary, inBoundary;
|
||||
HOperatorSet.Boundary(myPMAlign.templateRegion, out outBoundary, "inner_filled");
|
||||
HOperatorSet.Boundary(myPMAlign.templateRegion, out inBoundary, "outer");
|
||||
HOperatorSet.CropDomain(imageReduced, out imagePart);
|
||||
HOperatorSet.SetSystem("flush_graphic", "true");
|
||||
hWindowTool_Smart1.DispImage(imagePart);
|
||||
hWindowTool_Smart1.DispHWindow.SetColor("green");
|
||||
hWindowTool_Smart1.DispHWindow.SetDraw("margin");
|
||||
hWindowTool_Smart1.DispHWindow.DispObj(outBoundary);
|
||||
hWindowTool_Smart1.DispHWindow.DispObj(inBoundary);
|
||||
if (myPMAlign.matchMode == MatchMode.BasedShape)
|
||||
{
|
||||
HOperatorSet.SetLineStyle(myHwindow.DispHWindow, new HTuple());
|
||||
HOperatorSet.SetColor(myHwindow.DispHWindow, new HTuple("orange"));
|
||||
HOperatorSet.DispObj(contour, myHwindow.DispHWindow);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LoggerClass.WriteLog("创建模板失败!", MsgLevel.Exception);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
myPMAlign.isCreateModel = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设定工具运行状态
|
||||
/// </summary>
|
||||
/// <param name="msg">运行中的信息</param>
|
||||
/// <param name="status">运行状态</param>
|
||||
public void SetToolStatus(string msg, ToolRunStatu status, Exception ex = null)
|
||||
{
|
||||
if (myPMAlign != null)
|
||||
{
|
||||
myPMAlign.runMessage = msg;
|
||||
myPMAlign.toolRunStatu = status;
|
||||
lb_RunStatus.Text = myPMAlign.toolRunStatu == ToolRunStatu.Succeed ? "工具运行成功!" : $"工具运行异常, 异常原因:{myPMAlign.runMessage}";
|
||||
lb_RunTime.Text = myPMAlign.runTime;
|
||||
if (myPMAlign.toolRunStatu == ToolRunStatu.Succeed)
|
||||
{
|
||||
statusStrip.BackColor = Color.LimeGreen;
|
||||
}
|
||||
else
|
||||
{
|
||||
statusStrip.BackColor = Color.Red;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PreProcess_CheckChanged(object sender, EventArgs e)
|
||||
{
|
||||
myPMAlign.imageProcess.erosionValue1.isEnable = cbCErosion1.Checked;
|
||||
myPMAlign.imageProcess.dilationValue.isEnable = cbCDilation1.Checked;
|
||||
myPMAlign.imageProcess.erosionValue2.isEnable = cbCErosion2.Checked;
|
||||
if(!isInitTool)
|
||||
{
|
||||
myPMAlign.imageProcess.erosionValue1.isEnable = cbCErosion1.Checked;
|
||||
myPMAlign.imageProcess.dilationValue.isEnable = cbCDilation1.Checked;
|
||||
myPMAlign.imageProcess.erosionValue2.isEnable = cbCErosion2.Checked;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void PreValueChanged(double value)
|
||||
{
|
||||
myPMAlign.imageProcess.erosionValue1.algValue = cNumErosionValue1.Value;
|
||||
myPMAlign.imageProcess.dilationValue.algValue = cNumDilationValue2.Value;
|
||||
myPMAlign.imageProcess.erosionValue2.algValue = cNumErosionValue2.Value;
|
||||
if (!isInitTool)
|
||||
{
|
||||
myPMAlign.imageProcess.erosionValue1.algValue = cNumErosionValue1.Value;
|
||||
myPMAlign.imageProcess.dilationValue.algValue = cNumDilationValue2.Value;
|
||||
myPMAlign.imageProcess.erosionValue2.algValue = cNumErosionValue2.Value;
|
||||
}
|
||||
}
|
||||
|
||||
private void rabShape_CheckedChanged(object sender, EventArgs e)
|
||||
{
|
||||
myPMAlign.matchMode = rabShape.Checked ? MatchMode.BasedShape : MatchMode.BasedGray;
|
||||
}
|
||||
|
||||
private void cmbErsion1_SelectedIndexChanged()
|
||||
{
|
||||
if (!isInitTool)
|
||||
{
|
||||
myPMAlign.imageProcess.erosionValue1.algName = cmbErsion1.TextStr;
|
||||
myPMAlign.imageProcess.dilationValue.algName = cmbDilation.TextStr;
|
||||
myPMAlign.imageProcess.erosionValue2.algName = cmbErsion2.TextStr;
|
||||
}
|
||||
}
|
||||
|
||||
private void tsbtRunTool_Click(object sender, EventArgs e)
|
||||
{
|
||||
myPMAlign.Run(SoftwareRunState.Debug);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="toolStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
|
||||
@@ -20,13 +20,25 @@ using CommonMethods;
|
||||
using HalconDotNet;
|
||||
using ToolBase;
|
||||
using static DataStruct.DataStruct;
|
||||
using System.Windows.Forms;
|
||||
using Logger;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace PMAlignTool
|
||||
{
|
||||
[Serializable]
|
||||
public class PMAlign:IToolBase
|
||||
public class PMAlign : IToolBase
|
||||
{
|
||||
|
||||
public string toolName { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 输入姿态
|
||||
/// </summary>
|
||||
public PosXYU inputPose = new PosXYU();
|
||||
/// <summary>
|
||||
/// 制作模板时的输入位姿
|
||||
/// </summary>
|
||||
public PosXYU templatePose = new PosXYU();
|
||||
/// <summary>
|
||||
/// 是否显示匹配到的模板
|
||||
/// </summary>
|
||||
@@ -50,7 +62,7 @@ namespace PMAlignTool
|
||||
/// <summary>
|
||||
/// 模板句柄
|
||||
/// </summary>
|
||||
internal HTuple modelID { get; set; } = -1;
|
||||
internal HTuple modelID = -1;
|
||||
/// <summary>
|
||||
/// 行列间隔像素数
|
||||
/// </summary>
|
||||
@@ -62,13 +74,11 @@ namespace PMAlignTool
|
||||
/// <summary>
|
||||
/// 模板区域
|
||||
/// </summary>
|
||||
internal HObject templateRegion { get; set; }
|
||||
internal HObject templateRegion;
|
||||
/// <summary>
|
||||
/// 在进行模板创建及匹配时进行的图像预处理
|
||||
/// </summary>
|
||||
public ImagePreProcess imageProcess { get; set; } = new ImagePreProcess();
|
||||
|
||||
internal HObject totalRegion;
|
||||
/// <summary>
|
||||
/// 搜索区域图像
|
||||
/// </summary>
|
||||
@@ -101,10 +111,14 @@ namespace PMAlignTool
|
||||
/// 训练时所使用的模板图像,不点击获取图像时,不进行更新
|
||||
/// </summary>
|
||||
public HObject oldTrainImage { get; set; }
|
||||
public bool isCreateModel { get; set; }
|
||||
|
||||
internal void DispMainWindow(object dispHWindow)
|
||||
public override void DispMainWindow(HWindow dispHWindow)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (showFeature)
|
||||
{
|
||||
ShowTemplate(dispHWindow, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -119,30 +133,365 @@ namespace PMAlignTool
|
||||
/// <summary>
|
||||
/// 模板匹配结果
|
||||
/// </summary>
|
||||
internal List<XYU> L_result = new List<XYU>();
|
||||
public List<MatchResult> L_resultList { get; set; } = new List<MatchResult>() { };
|
||||
/// <summary>
|
||||
/// 匹配模式
|
||||
/// </summary>
|
||||
public MatchMode matchMode { get; set; } = MatchMode.BasedShape;
|
||||
/// <summary>
|
||||
/// 缩放最小值
|
||||
/// </summary>
|
||||
public HTuple minScale { get; internal set; } = 0.9;
|
||||
/// <summary>
|
||||
/// 缩放最大值
|
||||
/// </summary>
|
||||
public HTuple maxScale { get; internal set; } = 1.1;
|
||||
public bool isAutoConstants { get; set; }
|
||||
public string modelFilePath { get; set; }
|
||||
public RegionType searchRegionType { get; set; }
|
||||
public HObject SearchRegion { get; private set; }
|
||||
|
||||
public override void Run(SoftwareRunState softwareRunState)
|
||||
{
|
||||
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Restart();
|
||||
|
||||
if (inputImage == null)
|
||||
{
|
||||
FormPMAlignTool.Instance.SetToolStatus("工具输入图像为空", ToolRunStatu.Not_Input_Image);
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (softwareRunState == SoftwareRunState.Debug)
|
||||
{
|
||||
DispImage();
|
||||
}
|
||||
if (inputPose != null)
|
||||
{
|
||||
HTuple Row = inputPose.X - templatePose.X;
|
||||
HTuple Col = inputPose.Y - templatePose.Y;
|
||||
HTuple angle = inputPose.U - templatePose.U;
|
||||
|
||||
HTuple _homMat2D;
|
||||
HOperatorSet.HomMat2dIdentity(out _homMat2D);
|
||||
HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D);
|
||||
HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D);
|
||||
//对预期线的找模板区域做放射变换
|
||||
|
||||
}
|
||||
minScore = FormPMAlignTool.Instance.nud_minScore.Value;
|
||||
startAngle = Convert.ToInt16(FormPMAlignTool.Instance.nud_angleStart.Value);
|
||||
angleRange = Convert.ToInt16(FormPMAlignTool.Instance.nud_angleRange.Value);
|
||||
angleStep = Convert.ToInt16(FormPMAlignTool.Instance.nud_angleStep.Value);
|
||||
polarity = FormPMAlignTool.Instance.cbx_polarity.TextStr;
|
||||
isAutoConstants = FormPMAlignTool.Instance.ckb_autoContrast.Checked;
|
||||
minScale = FormPMAlignTool.Instance.nud_ScaleStart.Value;
|
||||
maxScale = FormPMAlignTool.Instance.nud_ScaleRange.Value;
|
||||
HObject findModelImg = ProcessImage(inputImage);
|
||||
int ret = FindModelTemplate(findModelImg);
|
||||
ToolRunStatu myState = ret == 0 ? ToolRunStatu.Succeed : ToolRunStatu.Model_UnFound;
|
||||
string retMsg = ret == 0 ? "工具运行成功,已找到匹配项!" : "未找到匹配项";
|
||||
sw.Stop();
|
||||
runTime = $"运行时间: {sw.ElapsedMilliseconds} ms";
|
||||
FormPMAlignTool.Instance.SetToolStatus(retMsg, myState);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FormPMAlignTool.Instance.SetToolStatus($"工具运行异常,异常原因: {ex}", ToolRunStatu.Tool_Run_Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public override void DispImage()
|
||||
{
|
||||
FormPMAlignTool.Instance.myHwindow.DispHWindow.ClearWindow();
|
||||
if (inputImage != null)
|
||||
{
|
||||
FormPMAlignTool.Instance.myHwindow.DispHWindow.DispObj(inputImage);
|
||||
}
|
||||
}
|
||||
|
||||
public override void DispMainWindow(HWindow window)
|
||||
private void CreateModelRegion()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
HOperatorSet.GenEmptyObj(out templateRegion);
|
||||
foreach (var item in FormPMAlignTool.Instance.templateModelListAdd)
|
||||
{
|
||||
HObject selectObj = item.GetDrawingObjectIconic();
|
||||
HOperatorSet.Union2(selectObj, templateRegion, out templateRegion);
|
||||
}
|
||||
foreach (var item in FormPMAlignTool.Instance.templateModelListSub)
|
||||
{
|
||||
HObject selectObj = item.GetDrawingObjectIconic();
|
||||
HOperatorSet.Difference(templateRegion, selectObj, out templateRegion);
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateModelTemplate()
|
||||
public int CreateModelTemplate()
|
||||
{
|
||||
|
||||
HObject template;
|
||||
oldTrainImage = inputImage;
|
||||
if (FormPMAlignTool.Instance.templateModelListAdd.Count == 0)
|
||||
{
|
||||
LoggerClass.WriteLog("未划定模板建立区域", MsgLevel.Exception);
|
||||
isCreateModel = false;
|
||||
return -1;
|
||||
}
|
||||
CreateModelRegion();
|
||||
HObject createModelImg;
|
||||
HOperatorSet.GenEmptyObj(out createModelImg);
|
||||
createModelImg = ProcessImage(inputImage);
|
||||
HOperatorSet.ReduceDomain(createModelImg, templateRegion, out template);
|
||||
try
|
||||
{
|
||||
HTuple rows, cols, angles, scores, scale;
|
||||
if (matchMode == MatchMode.BasedShape)
|
||||
{
|
||||
HOperatorSet.CreateScaledShapeModel(template,
|
||||
"auto",
|
||||
((HTuple)startAngle).TupleRad(),
|
||||
((HTuple)angleRange).TupleRad(),
|
||||
"auto",
|
||||
minScale,
|
||||
maxScale,
|
||||
"auto",
|
||||
"auto",
|
||||
polarity,
|
||||
isAutoConstants ? (HTuple)"auto" : (HTuple)contrast,
|
||||
"auto",
|
||||
out modelID);
|
||||
HOperatorSet.FindScaledShapeModel(inputImage,
|
||||
(HTuple)modelID,
|
||||
((HTuple)startAngle).TupleRad(),
|
||||
((HTuple)angleRange - startAngle).TupleRad(),
|
||||
minScale,
|
||||
maxScale,
|
||||
(HTuple)minScore,
|
||||
(HTuple)matchNum,
|
||||
(HTuple)0.5,
|
||||
(HTuple)"least_squares",
|
||||
(HTuple)0,
|
||||
(HTuple)0.9,
|
||||
out rows,
|
||||
out cols,
|
||||
out angles,
|
||||
out scale,
|
||||
out scores);
|
||||
}
|
||||
else
|
||||
{
|
||||
HOperatorSet.CreateNccModel(template,
|
||||
"auto",
|
||||
((HTuple)startAngle).TupleRad(),
|
||||
((HTuple)angleRange).TupleRad(),
|
||||
"auto",
|
||||
"use_polarity",
|
||||
out modelID);
|
||||
HOperatorSet.FindNccModel(inputImage,
|
||||
(HTuple)modelID,
|
||||
((HTuple)startAngle).TupleRad(),
|
||||
((HTuple)angleRange - startAngle).TupleRad(),
|
||||
(HTuple)minScore,
|
||||
(HTuple)matchNum,
|
||||
(HTuple)0.5,
|
||||
(HTuple)"true",
|
||||
(HTuple)0,
|
||||
out rows,
|
||||
out cols,
|
||||
out angles,
|
||||
out scores);
|
||||
}
|
||||
isCreateModel = true;
|
||||
HOperatorSet.WriteRegion(templateRegion, FormPMAlignTool.Instance.myToolInfo.FormToolName + ".hobj");
|
||||
if (scores != null && scores.Type != HTupleType.EMPTY)
|
||||
{
|
||||
templatePose = new PosXYU { X = rows[0].D, Y = cols[0].D , U = angles[0].D };
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LoggerClass.WriteLog("创建模板时出现异常", ex);
|
||||
isCreateModel = false;
|
||||
return -1;
|
||||
}
|
||||
finally
|
||||
{
|
||||
FormPMAlignTool.Instance.templateModelListAdd.Clear();
|
||||
FormPMAlignTool.Instance.templateModelListSub.Clear();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int FindModelTemplate(HObject findModelImage)
|
||||
{
|
||||
if (!isCreateModel)
|
||||
{
|
||||
LoggerClass.WriteLog("未创建或加载模板", MsgLevel.Exception);
|
||||
return -1;
|
||||
}
|
||||
if (File.Exists(toolName + ".hobj"))
|
||||
{
|
||||
HOperatorSet.ReadRegion(out templateRegion, toolName + ".hobj");
|
||||
LoggerClass.WriteLog($"{FormPMAlignTool.Instance.myToolInfo.FormToolName} 已加载模板", MsgLevel.Info);
|
||||
}
|
||||
HObject image;
|
||||
if (searchRegionType == RegionType.AllImage)
|
||||
{
|
||||
image = findModelImage;
|
||||
}
|
||||
else
|
||||
{
|
||||
HOperatorSet.ReduceDomain(inputImage, SearchRegion, out reducedImage);
|
||||
image = reducedImage;
|
||||
}
|
||||
HTuple rows, cols, angles, scores;
|
||||
L_resultList.Clear();
|
||||
try
|
||||
{
|
||||
if (matchMode == MatchMode.BasedShape)
|
||||
{
|
||||
HTuple temp;
|
||||
|
||||
HOperatorSet.FindScaledShapeModel(image,
|
||||
(HTuple)modelID,
|
||||
((HTuple)startAngle).TupleRad(),
|
||||
((HTuple)angleRange - startAngle).TupleRad(),
|
||||
minScale,
|
||||
maxScale,
|
||||
(HTuple)minScore,
|
||||
(HTuple)matchNum,
|
||||
(HTuple)0.5,
|
||||
(HTuple)"least_squares",
|
||||
(HTuple)0,
|
||||
(HTuple)0.9,
|
||||
out rows,
|
||||
out cols,
|
||||
out angles,
|
||||
out temp,
|
||||
out scores);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
HOperatorSet.FindNccModel(image,
|
||||
(HTuple)modelID,
|
||||
((HTuple)startAngle).TupleRad(),
|
||||
((HTuple)angleRange - startAngle).TupleRad(),
|
||||
(HTuple)minScore,
|
||||
(HTuple)matchNum,
|
||||
(HTuple)0.5,
|
||||
(HTuple)"true",
|
||||
(HTuple)0,
|
||||
out rows,
|
||||
out cols,
|
||||
out angles,
|
||||
out scores);
|
||||
}
|
||||
FormPMAlignTool.Instance.myHwindow.DispHWindow.ClearWindow();
|
||||
FormPMAlignTool.Instance.myHwindow.DispImage(inputImage);
|
||||
if (rows.TupleLength() > 0)
|
||||
{
|
||||
for (int i = 0; i < rows.TupleLength(); i++)
|
||||
{
|
||||
MatchResult matchResult = new MatchResult();
|
||||
matchResult.Row = Math.Round((double)rows[i], 3);
|
||||
matchResult.Col = Math.Round((double)cols[i], 3);
|
||||
matchResult.Angle = Math.Round((double)angles[i], 3);
|
||||
matchResult.Socre = Math.Round((double)scores[i], 3);
|
||||
L_resultList.Add(matchResult);
|
||||
}
|
||||
MatchResult temp;
|
||||
for (int i = 0; i < L_resultList.Count - 1; i++)
|
||||
{
|
||||
for (int j = i + 1; j < L_resultList.Count; j++)
|
||||
{
|
||||
if (L_resultList[i].Socre < L_resultList[j].Socre)
|
||||
{
|
||||
temp = L_resultList[i];
|
||||
L_resultList[i] = L_resultList[j];
|
||||
L_resultList[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(L_resultList.Count > 0)
|
||||
{
|
||||
toolRunStatu = ToolRunStatu.Succeed;
|
||||
ShowTemplate(FormPMAlignTool.Instance.myHwindow.DispHWindow);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerClass.WriteLog("寻找模板时出现异常!", ex);
|
||||
toolRunStatu = ToolRunStatu.Not_Succeed;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public HObject contour;
|
||||
/// <summary>
|
||||
/// 显示模板
|
||||
/// </summary>
|
||||
internal void ShowTemplate(HWindow dispHWindow, bool clearImg = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (modelID == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(clearImg)
|
||||
{
|
||||
dispHWindow.ClearWindow();
|
||||
dispHWindow.DispObj(inputImage);
|
||||
}
|
||||
if (matchMode == MatchMode.BasedShape)
|
||||
{
|
||||
HOperatorSet.GetShapeModelContours(out contour, modelID, new HTuple(1));
|
||||
HTuple area, row, col;
|
||||
HOperatorSet.AreaCenter(templateRegion, out area, out row, out col);
|
||||
HTuple homMat2D;
|
||||
HOperatorSet.HomMat2dIdentity(out homMat2D);
|
||||
HOperatorSet.HomMat2dTranslate(homMat2D, row, col, out homMat2D);
|
||||
HOperatorSet.AffineTransContourXld(contour, out contour, homMat2D);
|
||||
dispHWindow.SetColor("green");
|
||||
dispHWindow.DispObj(contour);
|
||||
}
|
||||
HObject outBoundary, inBoundary;
|
||||
HOperatorSet.Boundary(templateRegion, out outBoundary, "inner_filled");
|
||||
HOperatorSet.Boundary(templateRegion, out inBoundary, "outer");
|
||||
HOperatorSet.SetColor(FormPMAlignTool.Instance.myHwindow.DispHWindow, "green");
|
||||
HOperatorSet.SetLineStyle(FormPMAlignTool.Instance.myHwindow.DispHWindow, new HTuple());
|
||||
dispHWindow.DispObj(outBoundary);
|
||||
dispHWindow.DispObj(inBoundary);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LoggerClass.WriteLog("显示模板时出现错误", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private HObject ProcessImage(HObject inputImg)
|
||||
{
|
||||
if (inputImage == null) return inputImage;
|
||||
if (imageProcess.erosionValue1.isEnable)
|
||||
{
|
||||
HOperatorSet.GrayErosionShape(inputImg, out inputImg, imageProcess.erosionValue1.algValue, imageProcess.erosionValue1.algValue, imageProcess.erosionValue1.algName);
|
||||
}
|
||||
if (imageProcess.dilationValue.isEnable)
|
||||
{
|
||||
HOperatorSet.GrayDilationShape(inputImg, out inputImg, imageProcess.erosionValue1.algValue, imageProcess.erosionValue1.algValue, imageProcess.dilationValue.algName);
|
||||
}
|
||||
if (imageProcess.erosionValue2.isEnable)
|
||||
{
|
||||
HOperatorSet.GrayErosionShape(inputImg, out inputImg, imageProcess.erosionValue1.algValue, imageProcess.erosionValue1.algValue, imageProcess.erosionValue2.algName);
|
||||
}
|
||||
return inputImg;
|
||||
}
|
||||
|
||||
}
|
||||
[Serializable]
|
||||
public class ImagePreProcess
|
||||
@@ -156,5 +505,35 @@ namespace PMAlignTool
|
||||
{
|
||||
public double algValue { get; set; } = 0;
|
||||
public bool isEnable { get; set; } = false;
|
||||
public string algName { get; set; } = "rectangle";
|
||||
}
|
||||
[Serializable]
|
||||
public enum MatchMode
|
||||
{
|
||||
BasedShape,
|
||||
BasedGray,
|
||||
}
|
||||
[Serializable]
|
||||
public enum RegionType
|
||||
{
|
||||
AllImage,
|
||||
Rectangle1,
|
||||
Rectangle2,
|
||||
Circle,
|
||||
Ellipse,
|
||||
MultPoint,
|
||||
Ring,
|
||||
Any,
|
||||
}
|
||||
/// <summary>
|
||||
/// 模板匹配结果
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public struct MatchResult
|
||||
{
|
||||
internal double Row;
|
||||
internal double Col;
|
||||
internal double Angle;
|
||||
internal double Socre;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user