From 87a51c004242323d6bc80c470115ef69117bcb1b Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期五, 26 十二月 2025 09:52:47 +0800
Subject: [PATCH] ”修复一部分工具图像转换问题以及内存释放问题。“
---
LB_VisionProcesses/Cameras/CameraConfig.cs | 3
LB_VisionProcesses/Alogrithms/Halcon/2D/HBlobTool/HBlobTool.cs | 2
LB_SmartVision/ProcessRun/ProcessContext.cs | 56 ++++++++++++--
LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs | 137 ++++++++++++++++++++++------------
LB_VisionProcesses/IProcess.cs | 2
5 files changed, 140 insertions(+), 60 deletions(-)
diff --git a/LB_SmartVision/ProcessRun/ProcessContext.cs b/LB_SmartVision/ProcessRun/ProcessContext.cs
index 313c6cb..ff803a2 100644
--- a/LB_SmartVision/ProcessRun/ProcessContext.cs
+++ b/LB_SmartVision/ProcessRun/ProcessContext.cs
@@ -329,28 +329,37 @@
IndexValueName = arrOutputs[2];
object o_InputImage = ((IProcess)dicContext[IndexProcessName]).OutputImage;
- if (o_InputImage is HImage ho_image && ho_image.IsInitialized())
- InputImage = ho_image;
- else if (o_InputImage is Bitmap)
+ 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)
{
//灏哅at杞崲涓篐Object
using (HImage ho_RecordImage = TAlgorithm.Bitmap2HObject((Bitmap)o_InputImage))
{
- InputImage = ho_RecordImage.Clone();
+ if (ho_RecordImage != null)
+ {
+ InputImage = ho_RecordImage.Clone();
+ }
}
}
- else if (o_InputImage is Mat)
+ else if (o_InputImage != null && o_InputImage is Mat)
{
//灏哅at杞崲涓篐Object
using (HImage ho_RecordImage = TAlgorithm.Mat2HObject((Mat)o_InputImage))
{
- InputImage = ho_RecordImage.Clone();
+ if (ho_RecordImage != null)
+ {
+ InputImage = ho_RecordImage.Clone();
+ }
}
}
if (InputImage != null && InputImage.IsInitialized())
{
- HOperatorSet.GetImageSize(InputImage, out ho_ImageWidth, out ho_ImageHeight);
+ InputImage.GetImageSize(out ho_ImageWidth, out ho_ImageHeight);
//鍥剧墖灏哄鍙樺寲鎵嶆洿鏂扮獥鍙e昂瀵竅鎻愰珮閫熷害]
if ((ho_ImageWidth.Length > 0 && ho_ImageWidth.TupleInt() != hWindowControl.Size.Width)
@@ -493,7 +502,38 @@
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;
+ }
UpdateInputs(process);
// 涓嶅悓鑺傜偣璺宠繃鐨勬柟寮忎笉鍚�
diff --git a/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs b/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs
index 830932e..919f3bd 100644
--- a/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs
+++ b/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs
@@ -131,37 +131,6 @@
image = bmp;
return true;
}
- else if (InputImage is HObject ho_image)
- {
- if (!ho_image.IsInitialized())
- return false;
-
- if (Params.Fixture == null)
- Params.Fixture = new Fixture();
-
- HObject hoDomainImage = null;
-
- switch (Params.ROI?.GetType().Name)
- {
- case "HRectangle2":
- HOperatorSet.GenRectangle2(out HObject hRectangle2, (HTuple)(Params.ROI.Row + Params.Fixture.Row), (HTuple)(Params.ROI.Column + Params.Fixture.Column)
- , (HTuple)(Params.ROI.Phi + Params.Fixture.Phi), (HTuple)((HRectangle2)Params.ROI).SemiLength1, (HTuple)((HRectangle2)Params.ROI).SemiLength2);
- HOperatorSet.ReduceDomain(ho_image, hRectangle2, out hoDomainImage);
- break;
- case "HCircle":
- HOperatorSet.GenCircle(out HObject hCircle, (HTuple)(Params.ROI.Row + Params.Fixture.Row), (HTuple)(Params.ROI.Column + Params.Fixture.Column)
- , (HTuple)((HCircle)Params.ROI).Radius);
- HOperatorSet.ReduceDomain(ho_image, hCircle, out hoDomainImage);
- break;
- case "ROI":
- default:
- image = ho_image.CopyObj(1, -1);
- return true;
- }
-
- image = hoDomainImage;
- return true;
- }
else if (InputImage is Mat)
{
Mat src = ((Mat)InputImage);
@@ -244,6 +213,77 @@
}
}
+ public virtual bool ReduceDomainImage(object InputImage, ref HImage image)
+ {
+ image = null;
+ if (InputImage == null)
+ {
+ image = null;
+ Msg = "杈撳叆鍥剧墖涓虹┖";
+ Result = false;
+ return false;
+ }
+
+ lock (InputImage)
+ {
+ try
+ {
+ if (InputImage is HImage ho_image)
+ {
+ if (!ho_image.IsInitialized())
+ return false;
+
+ if (Params.Fixture == null)
+ Params.Fixture = new Fixture();
+
+ HImage hoDomainImage = null;
+
+ switch (Params.ROI?.GetType().Name)
+ {
+ case "HRectangle2":
+ using (HRegion hRectangle2 = new HRegion())
+ {
+ hRectangle2.GenRectangle2((HTuple)(Params.ROI.Row + Params.Fixture.Row), (HTuple)(Params.ROI.Column + Params.Fixture.Column)
+ , (HTuple)(Params.ROI.Phi + Params.Fixture.Phi), (HTuple)((HRectangle2)Params.ROI).SemiLength1, (HTuple)((HRectangle2)Params.ROI).SemiLength2);
+ hoDomainImage = ho_image.ReduceDomain(hRectangle2);
+ }
+ break;
+ case "HCircle":
+ using (HRegion hCircle = new HRegion())
+ {
+ hCircle.GenCircle((HTuple)(Params.ROI.Row + Params.Fixture.Row), (HTuple)(Params.ROI.Column + Params.Fixture.Column)
+ , (HTuple)((HCircle)Params.ROI).Radius);
+ hoDomainImage = ho_image.ReduceDomain(hCircle);
+ }
+ break;
+ case "ROI":
+ default:
+ image = ho_image.CopyObj(1, -1);
+ return true;
+ }
+
+ image = hoDomainImage;
+ return true;
+ }
+ else
+ {
+ image = null;
+ Msg = $"杈撳叆鏍煎紡涓嶆纭畕InputImage.GetType()}";
+ Result = false;
+ return false;
+ }
+ }
+ catch (Exception ex)
+ {
+ image = null;
+ Msg = $"瑁佸壀鍖哄煙澶辫触,鍘熷洜鏄�:{ex.ToString()}";
+ Result = false;
+ return false;
+ }
+ }
+ }
+
+
public override void Dispose()
{
if (InputImage != null)
@@ -318,22 +358,22 @@
Result = true;
bCompleted = false;
Msg = string.Empty;
- if (InputImage != null)
- {
- if (InputImage is HObject)
- {
- ((HObject)InputImage).Dispose();
- }
- else if (InputImage is Mat)
- {
- ((Mat)InputImage).Dispose();
- }
- else if (InputImage is Bitmap)
- {
- ((Bitmap)InputImage).Dispose();
- }
- InputImage = null;
- }
+ //if (InputImage != null)
+ //{
+ // if (InputImage is HObject)
+ // {
+ // ((HObject)InputImage).Dispose();
+ // }
+ // else if (InputImage is Mat)
+ // {
+ // ((Mat)InputImage).Dispose();
+ // }
+ // else if (InputImage is Bitmap)
+ // {
+ // ((Bitmap)InputImage).Dispose();
+ // }
+ // InputImage = null;
+ //}
if (Record != null)
{
Record.Dispose();
@@ -487,12 +527,11 @@
HImage image = null;
try
{
- if (bmp == null || bmp.Width == 0 || bmp.Height == 0)
+ if (bmp.Tag != null && bmp == null || bmp.Width == 0 || bmp.Height == 0)
{
image = null;
return image;
}
-
lock (bmp)
{
image = new HImage();
diff --git a/LB_VisionProcesses/Alogrithms/Halcon/2D/HBlobTool/HBlobTool.cs b/LB_VisionProcesses/Alogrithms/Halcon/2D/HBlobTool/HBlobTool.cs
index 49df5d4..b63d02a 100644
--- a/LB_VisionProcesses/Alogrithms/Halcon/2D/HBlobTool/HBlobTool.cs
+++ b/LB_VisionProcesses/Alogrithms/Halcon/2D/HBlobTool/HBlobTool.cs
@@ -108,7 +108,7 @@
}
#region 瑁佸壀鍖哄煙
- object DomainImage = null;
+ HImage DomainImage = null;
if (!ReduceDomainImage(InputImage, ref DomainImage))
{
Msg = "瑁佸壀鍖哄煙澶辫触";
diff --git a/LB_VisionProcesses/Cameras/CameraConfig.cs b/LB_VisionProcesses/Cameras/CameraConfig.cs
index 86ebc86..8210a2e 100644
--- a/LB_VisionProcesses/Cameras/CameraConfig.cs
+++ b/LB_VisionProcesses/Cameras/CameraConfig.cs
@@ -1,6 +1,7 @@
锘縰sing Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OpenCvSharp;
+using OpenCvSharp.Extensions;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
@@ -190,7 +191,7 @@
Mat src = Cv2.ImRead(SN);
if (src != null && !src.Empty())
- OutputImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);
+ OutputImage = src.ToBitmap();
}
}
}
diff --git a/LB_VisionProcesses/IProcess.cs b/LB_VisionProcesses/IProcess.cs
index 4aad660..0064440 100644
--- a/LB_VisionProcesses/IProcess.cs
+++ b/LB_VisionProcesses/IProcess.cs
@@ -135,7 +135,7 @@
/// <summary>
/// 鍏佽杩愯鏃堕棿
/// </summary>
- public double MaxTimeOut = 2000;
+ public double MaxTimeOut = 200000000000;
/// <summary>
/// 宸ュ叿鍚嶇О
--
Gitblit v1.9.3