在FindNccModel中使用Halcon 13函数C#会导致以下错误:
HALCON #6001:运算符find_ncc_model中内存不足
class Program
{
static void Main(string[] args)
{
HImage Image = new HImage(@"08_09_09_41_33_582_OK_000000153000.png");
double MidpointRow = 1053.5210373923057, MidpointCol = 1223.5205413999142;
int iCounter = 0;
while (true)
{
HNCCModel model = new HNCCModel(@"000000135000Mark_0.ncm");
HXLDCont hxCont = new HXLDCont();
hxCont.GenRectangle2ContourXld(
721.9213759213759,
1775.862648221344,
-0.99483767363676778,
72,
14.5);
HTuple htRowXLD, htColXLD;
hxCont.GetContourXld(out htRowXLD, out htColXLD);
HTuple htRadius = new HTuple();
htRadius = new HTuple(htRowXLD.TupleSub(MidpointRow).TuplePow(2) + htColXLD.TupleSub(MidpointCol).TuplePow(2)).TupleSqrt();
HRegion hrAnnulus = new HRegion();
hrAnnulus = hrAnnulus.GenAnnulus(MidpointRow, MidpointCol, htRadius.TupleMin() - 5.0, htRadius.TupleMax() + 5.0);
HImage hiTemp = Image.Clone();
HImage hiTemp2 = hiTemp.Rgb1ToGray();
HImage hiTemp3 = hiTemp2.ReduceDomain(hrAnnulus);
HTuple htRow, htColumn, Angle, Score;
model.FindNccModel(hiTemp3, -0.39, 6.29, 0.65, 1, 0, "true", 0, out htRow, out htColumn, out Angle, out Score);
hxCont.DisposeIfNotNull();
hrAnnulus.DisposeIfNotNull();
model.Dispose();
hiTemp.DisposeIfNotNull();
hiTemp2.DisposeIfNotNull();
hiTemp3.DisposeIfNotNull();
Console.WriteLine(iCounter++.ToString());
}
}
}
public static class DL_HalconUtilityClass
{
public static HRegion GenAnnulus(this HRegion region, double dCenterRow, double dCenterColumn, double dRadiusSmall, double dRadiusBig)
{
region.GenEmptyRegion();
if (dRadiusSmall > dRadiusBig)
{
throw new NotSupportedException("Wrong input parameters. Small radius is bigger than big radius.");
}
HRegion hrCircleSmall = new HRegion(dCenterRow, dCenterColumn, dRadiusSmall);
HRegion hrCircleBig = new HRegion(dCenterRow, dCenterColumn, dRadiusBig);
region = new HRegion();
region = hrCircleBig.Difference(hrCircleSmall);
hrCircleSmall.Dispose();
hrCircleBig.Dispose();
return region;
}
public static void DisposeIfNotNull(this HImage hiImage)
{
if (hiImage != null) hiImage.Dispose();
}
public static void DisposeIfNotNull(this HRegion hrRegion)
{
if (hrRegion != null) hrRegion.Dispose();
}
public static void DisposeIfNotNull(this HObject hoObject)
{
if (hoObject != null) hoObject.Dispose();
}
}函数本身可以在while循环中无休止地运行,但是如果它与我们的程序相结合,则会导致内存异常。另一方面,没有这个功能,程序本身就可以无休止地运行。同样有趣的是,错误发生在程序达到典型的1,1GB内存之前,这意味着存在内存泄漏。
我没有在Halcon文档中找到任何关于这个问题的参考,升级到最新的Halcon 13版本或使用Halcon XL也没有帮助。有人知道什么会导致这个问题吗?
发布于 2019-01-30 19:44:49
Halcon有两个内存管理优化系统设置: global_mem_cache和temporary_mem_cache。global_mem_cache没有影响,但是将temporary_mem_cache参数设置为“空闲”或“共享”解决了问题。
默认设置为“独占”,其中临时内存在本地为每个线程缓存。这是Halcon文档的摘录:
'temporary_mem_cache‘*),'tsp_temporary_mem_cache’这个参数控制临时内存缓存的操作模式。临时内存缓存用于通过缓存在执行操作符期间临时使用的内存来加速应用程序。对于大多数应用程序,默认设置(“独占”)将产生最佳结果。支持下列模式:
请注意,缓存模式“空闲”设置为独占运行模式,而其他模式则设置为可重入模式。
为了向后兼容,值'false‘和'true’也被接受;它们分别对应于‘空闲’和‘排他性’。
发布于 2018-09-06 13:44:42
在您的代码中,您已经按照建议手动处理了大多数HALCON对象。正如您可能知道的那样,这是必要的,因为.NET垃圾收集器不知道被托管对象可能使用的HALCON库处理的非托管内存量。
但是,您无法释放包含HTuples htRow、htColumn、Angle和Score结果的htColumn。
您还可能希望将HNCCModel的创建移出while循环。
https://stackoverflow.com/questions/52198938
复制相似问题