在上位机中通常需要使用到绘制ROI模块或者获取已经绘制好的ROI区域的参数,在这里通过使用hsmartwindow窗体控件进行对ROI的绘制和获取。
先上代码:
csharp
/// <summary>
/// 创建ROI
/// </summary>
/// <param name="Win">传入HSmartWindowControl</param>
/// <param name="ROItype">需要绘制的ROI类型</param>
/// <param name="drawingObject">绘制好的ROI参数,传出</param>
public void Set_DrawHalconROI(HSmartWindowControl Win, HDrawingObject.HDrawingObjectType ROItype, out HDrawingObject drawingObject
)
{
switch (ROItype)
{
case HDrawingObject.HDrawingObjectType.CIRCLE:
drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, 500, 500, 300);
Win.HalconWindow.AttachDrawingObjectToWindow(drawingObject);
break;
case HDrawingObject.HDrawingObjectType.RECTANGLE2:
drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, 500, 500, 0, 500, 500);
Win.HalconWindow.AttachDrawingObjectToWindow(drawingObject);
break;
case HDrawingObject.HDrawingObjectType.LINE:
drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.LINE, 200, 200, 500, 500);
Win.HalconWindow.AttachDrawingObjectToWindow(drawingObject);
break;
default:
drawingObject = null;
break;
}
}
csharp
/// <summary>
/// 获取ROI参数
/// </summary>
/// <param name="ROItype">需要输入的ROI类型</param>
/// <param name="drawingObject">传入的ROI对象参数</param>
/// <param name="Result">输出结果double数组</param>
/// <param name="HROI">输出ROI形状,Hobject类型</param>
public void Get_DrawHalconROI(HDrawingObject.HDrawingObjectType ROItype, HDrawingObject drawingObject
, out double[] Result, out HObject HROI)
{
HTuple paramName = new HTuple(), param = new HTuple();
string[] paramlist = new string[10];
switch (ROItype)
{
case HDrawingObject.HDrawingObjectType.RECTANGLE2:
paramlist = new string[] { "row", "column", "phi", "length1", "length2" };
paramName = paramlist;
param = drawingObject.GetDrawingObjectParams(paramName);
HOperatorSet.GenRectangle2(out HROI, param.DArr[0], param.DArr[1], param.DArr[2], param.DArr[3], param.DArr[4]);
Result = param.DArr;
break;
case HDrawingObject.HDrawingObjectType.CIRCLE:
paramlist = new string[] { "row", "column", "radius" };
paramName = paramlist;
param = drawingObject.GetDrawingObjectParams(paramName);
HOperatorSet.GenCircle(out HROI, param.DArr[0], param.DArr[1], param.DArr[2]);
Result = param.DArr;
break;
case HDrawingObject.HDrawingObjectType.LINE:
paramlist = new string[] { "row1", "column1", "row2", "column2" };
paramName = paramlist;
param = drawingObject.GetDrawingObjectParams(paramName);
HOperatorSet.GenRegionLine(out HROI, param.DArr[0], param.DArr[1], param.DArr[2], param.DArr[3]);
Result = param.DArr;
break;
default:
Result = new double[] { 0 };
HROI = null;
break;
}
}
实际效果:
延申说明:
1.Halcon自带的.netROI是通过调用HDrawingObject中的算子实现的,获取ROI同样也是,如何去得到延申的其他ROI。在Halcon的帮助文档中,搜索SetDrawingObjectParams并在.net格式中我们可以找到halcon所支持的全部ROI区域的参数,我们根据区域参数去获取即可。
注意事项:
1.我们在编写的时候一定要主语获取的ROI的变量名称是否输入错误。我们在传入获取的参数名称需要与文档中的参数名称一一对应,并且大小写不能错误。