From ebcc1d53f14112363bbf539bcaf0202aadcdc9d7 Mon Sep 17 00:00:00 2001
From: C3032 <1057644574@qq.com>
Date: 星期一, 13 四月 2026 12:58:58 +0800
Subject: [PATCH] 2D取图计数功能完成
---
LB_SmartVision/ProcessRun/ProcessContext.cs | 189 +++++++++++++++++++++++++++++++++--------------
1 files changed, 132 insertions(+), 57 deletions(-)
diff --git a/LB_SmartVision/ProcessRun/ProcessContext.cs b/LB_SmartVision/ProcessRun/ProcessContext.cs
index 79d4ada..c680c4f 100644
--- a/LB_SmartVision/ProcessRun/ProcessContext.cs
+++ b/LB_SmartVision/ProcessRun/ProcessContext.cs
@@ -6,9 +6,11 @@
using LB_VisionProcesses.Alogrithms;
using LB_VisionProcesses.Cameras;
using LB_VisionProcesses.Communicators;
+using LB_VisionProcesses.Processes;
using LB_VisionProcesses.Processes.ScriptTool;
using OpenCvSharp;
using System.Collections.Concurrent;
+using System.Windows.Media.Media3D;
namespace LB_SmartVision.ProcessRun
{
@@ -301,7 +303,7 @@
return res;
}
- public bool GetImage(Layout layout, out HImage InputImage, out HImage RecordImage)
+ public bool GetImage(Layout layout, out HObject InputImage, out HObject RecordImage)
{
InputImage = null; RecordImage = null;
try
@@ -329,37 +331,24 @@
IndexValueName = arrOutputs[2];
object o_InputImage = ((IProcess)dicContext[IndexProcessName]).OutputImage;
- if (o_InputImage != null && o_InputImage is HImage ho_image && ho_image.IsInitialized())
- {
- InputImage = ho_image.Clone();
- ho_image.Dispose();
- }
- else if (o_InputImage != null && o_InputImage is Bitmap)
+ if (o_InputImage is HObject ho_image && ho_image.IsInitialized())
+ InputImage = ho_image;
+ else if (o_InputImage is Bitmap)
{
//灏哅at杞崲涓篐Object
- using (HImage ho_RecordImage = TAlgorithm.Bitmap2HImage((Bitmap)o_InputImage))
- {
- if (ho_RecordImage != null && ho_RecordImage.Key != IntPtr.Zero)
- {
- InputImage = ho_RecordImage.Clone();
- }
- }
+ TAlgorithm.Bitmap2HObject((Bitmap)o_InputImage, out HObject ho_RecordImage);
+ InputImage = ho_RecordImage;
}
- else if (o_InputImage != null && o_InputImage is Mat)
+ else if (o_InputImage is Mat)
{
//灏哅at杞崲涓篐Object
- using (HImage ho_RecordImage = TAlgorithm.Mat2HImage((Mat)o_InputImage))
- {
- if (ho_RecordImage != null && ho_RecordImage.Key != IntPtr.Zero)
- {
- InputImage = ho_RecordImage.Clone();
- }
- }
+ TAlgorithm.Mat2HObject((Mat)o_InputImage, out HObject ho_RecordImage);
+ InputImage = ho_RecordImage;
}
- if (InputImage != null && InputImage.Key != IntPtr.Zero && InputImage.IsInitialized())
+ if (InputImage != null && InputImage.IsInitialized())
{
- InputImage.GetImageSize(out ho_ImageWidth, out ho_ImageHeight);
+ HOperatorSet.GetImageSize(InputImage, out ho_ImageWidth, out ho_ImageHeight);
//鍥剧墖灏哄鍙樺寲鎵嶆洿鏂扮獥鍙e昂瀵竅鎻愰珮閫熷害]
if ((ho_ImageWidth.Length > 0 && ho_ImageWidth.TupleInt() != hWindowControl.Size.Width)
@@ -387,13 +376,11 @@
if (dicContext.ContainsKey(IndexProcessName))
{
- using (ObjectRecord objectRecord1 = ((IProcess)dicContext[IndexProcessName]).Record)
+ ObjectRecord objectRecord1 = ((IProcess)dicContext[IndexProcessName]).Record;
+ if (objectRecord1 != null)
{
- if (objectRecord1 != null)
- {
- objectRecord1.Display(hWindowControl.HalconWindow);
- objectRecord1.Dispose();
- }
+ objectRecord1.Display(hWindowControl.HalconWindow);
+ objectRecord1.Dispose();
}
}
}
@@ -407,13 +394,11 @@
if (dicContext.ContainsKey(IndexProcessName))
{
- using (ObjectRecord objectRecord2 = ((IProcess)dicContext[IndexProcessName]).Record)
+ ObjectRecord objectRecord2 = ((IProcess)dicContext[IndexProcessName]).Record;
+ if (objectRecord2 != null)
{
- if (objectRecord2 != null)
- {
- objectRecord2.Display(hWindowControl.HalconWindow);
- objectRecord2.Dispose();
- }
+ objectRecord2.Display(hWindowControl.HalconWindow);
+ objectRecord2.Dispose();
}
}
}
@@ -427,13 +412,11 @@
if (dicContext.ContainsKey(IndexProcessName))
{
- using (ObjectRecord objectRecord3 = ((IProcess)dicContext[IndexProcessName]).Record)
+ ObjectRecord objectRecord3 = ((IProcess)dicContext[IndexProcessName]).Record;
+ if (objectRecord3 != null)
{
- if (objectRecord3 != null)
- {
- objectRecord3.Display(hWindowControl.HalconWindow);
- objectRecord3.Dispose();
- }
+ objectRecord3.Display(hWindowControl.HalconWindow);
+ objectRecord3.Dispose();
}
}
}
@@ -441,13 +424,13 @@
if (Result)
{
Msg = "杩愯鎴愬姛";
- hWindowControl.HalconWindow.SetColor("green");
+ HOperatorSet.SetColor(hWindowControl.HalconWindow, "green");
}
else
- hWindowControl.HalconWindow.SetColor("red");
+ HOperatorSet.SetColor(hWindowControl.HalconWindow, "red");
TAlgorithm.DispMsg(Msg, hWindowControl.HalconWindow, Result ? "green" : "red", 0, 0);
- RecordImage = hWindowControl.HalconWindow.DumpWindowImage();
+ HOperatorSet.DumpWindowImage(out RecordImage, hWindowControl.HalconWindow);
#endregion
return true;
@@ -508,37 +491,36 @@
if (dicContext.TryGetValue(ProcessName, out IProcess obj)
&& obj is IProcess process)
{
+ //process.InputImage = null;
+
if (process.InputImage != null)
{
if (process.InputImage is HObject)
- {
((HObject)process.InputImage).Dispose();
- }
else if (process.InputImage is Mat)
- {
((Mat)process.InputImage).Dispose();
- }
else if (process.InputImage is Bitmap)
- {
((Bitmap)process.InputImage).Dispose();
- }
+
process.InputImage = null;
}
+
if (process.OutputImage != null)
{
if (process.OutputImage is HObject)
- {
((HObject)process.OutputImage).Dispose();
- }
else if (process.OutputImage is Mat)
- {
((Mat)process.OutputImage).Dispose();
- }
else if (process.OutputImage is Bitmap)
- {
((Bitmap)process.OutputImage).Dispose();
- }
+
process.OutputImage = null;
+ }
+
+ if (process.Record != null)
+ {
+ process.Record.Dispose();
+ process.Record = null;
}
UpdateInputs(process);
@@ -589,9 +571,102 @@
[Node("鐩告満鍙栧浘", "鍙栧儚宸ュ叿", "Basic", "鐩告満鍙栧浘")]
public void 鐩告満鍙栧浘(FlowNode node) { RunNodeAsync(node); }
+ [Node("璇荤爜宸ュ叿", "鍙栧儚宸ュ叿", "Basic", "璇荤爜宸ュ叿")]
+ public void 璇荤爜宸ュ叿(FlowNode node) { RunNodeAsync(node); }
+
[Node("Halcon2D鏂戠偣宸ュ叿", "Halcon2D宸ュ叿", "Basic", "Halcon2D鏂戠偣宸ュ叿")]
public void Halcon2D鏂戠偣宸ュ叿(FlowNode node) { RunNodeAsync(node); }
+ [Node("閫氳妯″潡", "閫氳宸ュ叿", "Basic", "閫氳妯″潡")]
+ public void 閫氳妯″潡(FlowNode node) { RunNodeAsync(node); }
+
+ [Node("Halcon2D鍥惧儚澧炲己", "Halcon2D宸ュ叿", "Basic", "Halcon2D鍥惧儚澧炲己")]
+ public void Halcon2D鍥惧儚澧炲己(FlowNode node) { RunNodeAsync(node); }
+
+ [Node("Halcon2D鍥惧儚婊ゆ尝", "Halcon2D宸ュ叿", "Basic", "Halcon2D鍥惧儚婊ゆ尝")]
+ public void Halcon2D鍥惧儚婊ゆ尝(FlowNode node) { RunNodeAsync(node); }
+
+ [Node("杞儙璁℃暟", "鎺у埗", "Logic", "杞儙璁℃暟鑺傜偣")]
+ public void 杞儙璁℃暟(FlowNode node)
+ {
+ string ProcessName = node.Text;
+ try
+ {
+ if (dicContext.TryGetValue(ProcessName, out IProcess obj)
+ && obj is TyreCounterProcess counter)
+ {
+ UpdateInputs(counter);
+
+ if (node.Break)
+ {
+ node.BranchIndex = "0";
+ node.Result = true;
+ counter.Result = true;
+ counter.Msg = "杞儙璁℃暟鑺傜偣宸茶烦杩�";
+ return;
+ }
+
+ // 浠庡墠缃妭鐐硅幏鍙栨娴嬬粨鏋滐紙閫氳繃杈撳叆鏄犲皠锛�
+ bool imageResult = GetPreviousResult(node);
+
+ // 璁剧疆杈撳叆缁撴灉
+ counter.InputImage = imageResult;
+
+ // 杩愯杞儙璁℃暟
+ if (!counter.Run())
+ {
+ node.Result = false;
+ Result &= false;
+ Msg += $"[{ProcessName}]{counter.Msg}";
+ }
+ else
+ {
+ node.Result = true;
+
+ // 濡傛灉瀹屾垚涓�涓疆鑳庯紝杈撳嚭瀹屾垚淇″彿
+ if (counter.IsTyreComplete)
+ {
+ Msg += $"[{ProcessName}]瀹屾垚杞儙 #{counter.CurrentTyreID - 1}锛岀粨鏋�: {(counter.TyreResult ? "OK" : "NG")}";
+ }
+ }
+ }
+ else
+ {
+ node.Result = false;
+ Result &= false;
+ Msg += $"[{ProcessName}]鏈壘鍒拌疆鑳庤鏁板伐鍏峰疄渚�";
+ }
+ }
+ catch (Exception ex)
+ {
+ node.Result = false;
+ Result &= false;
+ Msg += $"[{ProcessName}]杩愯鍙戠敓鎰忓,{ex.Message}";
+ }
+ }
+
+ /// <summary>
+ /// 鑾峰彇鍓嶇疆鑺傜偣鐨勬娴嬬粨鏋�
+ /// </summary>
+ private bool GetPreviousResult(FlowNode node)
+ {
+ try
+ {
+ // 鏌ユ壘鍓嶇疆鑺傜偣
+ var prevNode = dicContext.Values
+ .OfType<IProcess>()
+ .FirstOrDefault(p => p.Result == true || p.Result == false);
+
+ if (prevNode != null)
+ {
+ return prevNode.Result;
+ }
+ }
+ catch { }
+
+ return true; // 榛樿杩斿洖OK
+ }
+
#endregion
--
Gitblit v1.9.3