搜档网
当前位置:搜档网 › Halcon导出C#代码结构学习

Halcon导出C#代码结构学习

// File generated by HDevelop for HALCON/DOTNET (C#) Version 10.0
// 这个文件是 Halcon 10.0 版本 导出的 C# 代码
// 函数体
// 变量声明
// 主要的处理函数
// 异常捕获与处理
// 内存释放
// 主函数
//
//
// 添加对HalconDotNet的引用
using HalconDotNet;
// 导出的类名: HDevelopExport
public partial class HDevelopExport
{
#if !NO_EXPORT_APP_MAIN
//构造函数
public HDevelopExport()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("do_low_error", "false");
action();
}
#endif


// Procedures 函数体
// External procedures 外部函数体
// Chapter: Matching / Shape-Based 匹配
// Short Description: Display the results of Shape-Based Matching.简述函数功能:显示形状匹配的结果
public void dev_display_shape_matching_results (HTuple hv_ModelID, HTuple hv_Color,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_ScaleR, HTuple hv_ScaleC,
HTuple hv_Model)
{
// Local iconic variables
// 局部图像变量
HObject ho_ModelContours=null, ho_ContoursAffinTrans=null;
// Local control variables
// 局部控制变量
HTuple hv_NumMatches, hv_Index=new HTuple();
HTuple hv_Match=new HTuple(), hv_HomMat2DIdentity=new HTuple();
HTuple hv_HomMat2DScale=new HTuple(), hv_HomMat2DRotate=new HTuple();
HTuple hv_HomMat2DTranslate=new HTuple();
HTuple hv_Model_COPY_INP_TMP = hv_Model.Clone();
HTuple hv_ScaleC_COPY_INP_TMP = hv_ScaleC.Clone();
HTuple hv_ScaleR_COPY_INP_TMP = hv_ScaleR.Clone();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_ModelContours);
HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
try
{
//This procedure displays the results of Shape-Based Matching.
//这个函数处理过程的作用是显示匹配的结果
hv_NumMatches = new HTuple(hv_Row.TupleLength());
if ((int)(new HTuple(hv_NumMatches.TupleGreater(0))) != 0)
{
if ((int)(new HTuple((new HTuple(hv_ScaleR_COPY_INP_TMP.TupleLength())).TupleEqual(
1))) != 0)
{
HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleR_COPY_INP_TMP, out hv_ScaleR_COPY_INP_TMP);
}
if ((int)(new HTuple((new HTuple(hv_ScaleC_COPY_INP_TMP.TupleLength())).TupleEqual(
1))) != 0)
{
HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleC_COPY_INP_TMP, out hv_ScaleC_COPY_INP_TMP);
}
if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength())).TupleEqual(
0))) != 0)
{
HOperatorSet.TupleGenConst(hv_NumMatches, 0, out hv_Model_COPY_INP_TMP);
}
else if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength()
)).TupleEqual(1))) != 0)
{

HOperatorSet.TupleGenConst(hv_NumMatches, hv_Model_COPY_INP_TMP, out hv_Model_COPY_INP_TMP);
}
for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_ModelID.TupleLength()
))-1); hv_Index = (int)hv_Index + 1)
{
ho_ModelContours.Dispose();
HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID.TupleSelect(
hv_Index), 1);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetColor(HDevWindowStack.GetActive(), hv_Color.TupleSelect(
hv_Index%(new HTuple(hv_Color.TupleLength()))));
}
for (hv_Match=0; hv_Match.Continue(hv_NumMatches-1, 1); hv_Match = hv_Match.TupleAdd(1))
{
if ((int)(new HTuple(hv_Index.TupleEqual(hv_Model_COPY_INP_TMP.TupleSelect(
hv_Match)))) != 0)
{
HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, hv_ScaleR_COPY_INP_TMP.TupleSelect(
hv_Match), hv_ScaleC_COPY_INP_TMP.TupleSelect(hv_Match), 0, 0,
out hv_HomMat2DScale);
HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, hv_Angle.TupleSelect(
hv_Match), 0, 0, out hv_HomMat2DRotate);
HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate, hv_Row.TupleSelect(
hv_Match), hv_Column.TupleSelect(hv_Match), out hv_HomMat2DTranslate);
ho_ContoursAffinTrans.Dispose();
HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffinTrans,
hv_HomMat2DTranslate);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_ContoursAffinTrans, HDevWindowStack.GetActive()
);
}
}
}
}
}
// 内存释放
ho_ModelContours.Dispose();
ho_ContoursAffinTrans.Dispose();
return;
}
//捕获异常
catch (HalconException HDevExpDefaultException)
{
ho_ModelContours.Dispose();
ho_ContoursAffinTrans.Dispose();
//处理异常
throw HDevExpDefaultException;
}
}
// Chapter: Develop
// Short Description: Switch dev_update_pc, dev_update_var and dev_update_window to 'off'.
public void dev_update_off ()
{
// Initialize local and output iconic variables
//This procedure sets different update settings to 'off'.
//This is useful to get the best performance and reduce overhead.
//
// dev_update_pc(...); only in hdevelop
// dev_update_var(...); only in hdevelop
// dev_update_window(...); only in hdevelop
return;
}
// Chapter: Graphics / Text
// Short Description: This procedure writes a text message.
public void disp_message (HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple

hv_Column, HTuple hv_Color, HTuple hv_Box)
{
// Local control variables
HTuple hv_Red, hv_Green, hv_Blue, hv_Row1Part;
HTuple hv_Column1Part, hv_Row2Part, hv_Column2Part, hv_RowWin;
HTuple hv_ColumnWin, hv_WidthWin, hv_HeightWin, hv_MaxAscent;
HTuple hv_MaxDescent, hv_MaxWidth, hv_MaxHeight, hv_R1=new HTuple();
HTuple hv_C1=new HTuple(), hv_FactorRow=new HTuple(), hv_FactorColumn=new HTuple();
HTuple hv_Width=new HTuple(), hv_Index=new HTuple(), hv_Ascent=new HTuple();
HTuple hv_Descent=new HTuple(), hv_W=new HTuple(), hv_H=new HTuple();
HTuple hv_FrameHeight=new HTuple(), hv_FrameWidth=new HTuple();
HTuple hv_R2=new HTuple(), hv_C2=new HTuple(), hv_DrawMode=new HTuple();
HTuple hv_Exception=new HTuple(), hv_CurrentColor=new HTuple();
HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
HTuple hv_String_COPY_INP_TMP = hv_String.Clone();
// Initialize local and output iconic variables
//This procedure displays text in a graphics window.
//
//Input parameters:
//WindowHandle: The WindowHandle of the graphics window, where
// the message should be displayed
//String: A tuple of strings containing the text message to be displayed
//CoordSystem: If set to 'window', the text position is given
// with respect to the window coordinate system.
// If set to 'image', image coordinates are used.
// (This may be useful in zoomed images.)
//Row: The row coordinate of the desired text position
// If set to -1, a default value of 12 is used.
//Column: The column coordinate of the desired text position
// If set to -1, a default value of 12 is used.
//Color: defines the color of the text as string.
// If set to [], '' or 'auto' the currently set color is used.
// If a tuple of strings is passed, the colors are used cyclically
// for each new textline.
//Box: If set to 'true', the text is written within a white box.
//
//prepare window
HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,
out hv_Column2Part);
HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,
out hv_WidthWin, out hv_HeightWin);
HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin-1, hv_WidthWin-1);
//
//default settings
if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Row_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Column_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
{
hv_Color_COPY_INP_TMP = "";
}
//
hv_Str

ing_COPY_INP_TMP = (((""+hv_String_COPY_INP_TMP)+"")).TupleSplit("\n");
//
//Estimate extentions of text depending on font size.
HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
out hv_MaxWidth, out hv_MaxHeight);
if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
{
hv_R1 = hv_Row_COPY_INP_TMP.Clone();
hv_C1 = hv_Column_COPY_INP_TMP.Clone();
}
else
{
//transform image to window coordinates
hv_FactorRow = (1.0*hv_HeightWin)/((hv_Row2Part-hv_Row1Part)+1);
hv_FactorColumn = (1.0*hv_WidthWin)/((hv_Column2Part-hv_Column1Part)+1);
hv_R1 = ((hv_Row_COPY_INP_TMP-hv_Row1Part)+0.5)*hv_FactorRow;
hv_C1 = ((hv_Column_COPY_INP_TMP-hv_Column1Part)+0.5)*hv_FactorColumn;
}
//
//display text box depending on text size
if ((int)(new HTuple(hv_Box.TupleEqual("true"))) != 0)
{
//calculate box extents
hv_String_COPY_INP_TMP = (" "+hv_String_COPY_INP_TMP)+" ";
hv_Width = new HTuple();
for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
))-1); hv_Index = (int)hv_Index + 1)
{
HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
hv_Width = hv_Width.TupleConcat(hv_W);
}
hv_FrameHeight = hv_MaxHeight*(new HTuple(hv_String_COPY_INP_TMP.TupleLength()
));
hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
hv_R2 = hv_R1+hv_FrameHeight;
hv_C2 = hv_C1+hv_FrameWidth;
//display rectangles
HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
HOperatorSet.SetDraw(hv_WindowHandle, "fill");
HOperatorSet.SetColor(hv_WindowHandle, "light gray");
HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1+3, hv_C1+3, hv_R2+3, hv_C2+3);
HOperatorSet.SetColor(hv_WindowHandle, "white");
HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
}
else if ((int)(new HTuple(hv_Box.TupleNotEqual("false"))) != 0)
{
hv_Exception = "Wrong value of control parameter Box";
throw new HalconException(hv_Exception);
}
//Write text.
for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
))-1); hv_Index = (int)hv_Index + 1)
{
hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index%(new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
)));
if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
"auto")))) != 0)
{
HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
}
else
{
HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Bl

ue);
}
hv_Row_COPY_INP_TMP = hv_R1+(hv_MaxHeight*hv_Index);
HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index));
}
//reset changed window settings
HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
hv_Column2Part);
return;
}
// Chapter: Graphics / Text
// Short Description: Set font independent of OS
public void set_display_font (HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font,
HTuple hv_Bold, HTuple hv_Slant)
{
// Local control variables
HTuple hv_OS, hv_Exception=new HTuple();
HTuple hv_AllowedFontSizes=new HTuple(), hv_Distances=new HTuple();
HTuple hv_Indices=new HTuple();
HTuple hv_Bold_COPY_INP_TMP = hv_Bold.Clone();
HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone();
HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone();
HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone();
// Initialize local and output iconic variables
//This procedure sets the text font of the current window with
//the specified attributes.
//It is assumed that following fonts are installed on the system:
//Windows: Courier New, Arial Times New Roman
//Linux: courier, helvetica, times
//Because fonts are displayed smaller on Linux than on Windows,
//a scaling factor of 1.25 is used the get comparable results.
//For Linux, only a limited number of font sizes is supported,
//to get comparable results, it is recommended to use one of the
//following sizes: 9, 11, 14, 16, 20, 27
//(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34)
//
//input parameters:
//WindowHandle: The graphics window for which the font will be set
//Size: The font size. If Size=-1, the default of 16 is used.
//Bold: If set to 'true', a bold font is used
//Slant: If set to 'true', a slanted font is used
//
HOperatorSet.GetSystem("operating_system", out hv_OS);
if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
{
hv_Size_COPY_INP_TMP = 16;
}
if ((int)(new HTuple((((hv_OS.TupleStrFirstN(2)).TupleStrLastN(0))).TupleEqual(
"Win"))) != 0)
{
//set font on Windows systems
if ((int)((new HTuple((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(
new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
"courier")))) != 0)
{
hv_Font_COPY_INP_TMP = "Courier New";
}
else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
{
hv_Font_

COPY_INP_TMP = "Arial";
}
else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
{
hv_Font_COPY_INP_TMP = "Times New Roman";
}
if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
{
hv_Bold_COPY_INP_TMP = 1;
}
else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
{
hv_Bold_COPY_INP_TMP = 0;
}
else
{
hv_Exception = "Wrong value of control parameter Bold";
throw new HalconException(hv_Exception);
}
if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
{
hv_Slant_COPY_INP_TMP = 1;
}
else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
{
hv_Slant_COPY_INP_TMP = 0;
}
else
{
hv_Exception = "Wrong value of control parameter Slant";
throw new HalconException(hv_Exception);
}
try
{
HOperatorSet.SetFont(hv_WindowHandle, ((((((("-"+hv_Font_COPY_INP_TMP)+"-")+hv_Size_COPY_INP_TMP)+"-*-")+hv_Slant_COPY_INP_TMP)+"-*-*-")+hv_Bold_COPY_INP_TMP)+"-");
}
// 捕获异常
catch (HalconException HDevExpDefaultException1)
{
//异常处理
HDevExpDefaultException1.ToHTuple(out hv_Exception);
throw new HalconException(hv_Exception);
}
}
else
{
//set font for UNIX systems
hv_Size_COPY_INP_TMP = hv_Size_COPY_INP_TMP*1.25;
hv_AllowedFontSizes = new HTuple();
hv_AllowedFontSizes[0] = 11;
hv_AllowedFontSizes[1] = 14;
hv_AllowedFontSizes[2] = 17;
hv_AllowedFontSizes[3] = 20;
hv_AllowedFontSizes[4] = 25;
hv_AllowedFontSizes[5] = 34;
if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual(
-1))) != 0)
{
hv_Distances = ((hv_AllowedFontSizes-hv_Size_COPY_INP_TMP)).TupleAbs();
HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices);
hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect(
0));
}
if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
"Courier")))) != 0)
{
hv_Font_COPY_INP_TMP = "courier";
}
else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
{
hv_Font_COPY_INP_TMP = "helvetica";
}
else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
{
hv_Font_COPY_INP_TMP = "times";
}
if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
{
hv_Bold_COPY_INP_TMP = "bold";
}
else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
{
hv_Bold_COPY_INP_TMP = "medium";
}
else

{
hv_Exception = "Wrong value of control parameter Bold";
throw new HalconException(hv_Exception);
}
if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
{
if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0)
{
hv_Slant_COPY_INP_TMP = "i";
}
else
{
hv_Slant_COPY_INP_TMP = "o";
}
}
else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
{
hv_Slant_COPY_INP_TMP = "r";
}
else
{
hv_Exception = "Wrong value of control parameter Slant";
throw new HalconException(hv_Exception);
}
try
{
HOperatorSet.SetFont(hv_WindowHandle, ((((((("-adobe-"+hv_Font_COPY_INP_TMP)+"-")+hv_Bold_COPY_INP_TMP)+"-")+hv_Slant_COPY_INP_TMP)+"-normal-*-")+hv_Size_COPY_INP_TMP)+"-*-*-*-*-*-*-*");
}
// 捕获异常
catch (HalconException HDevExpDefaultException1)
{
HDevExpDefaultException1.ToHTuple(out hv_Exception);
throw new HalconException(hv_Exception);
}
}
return;
}


// Main procedure 主要的函数
// 访问修饰符 返回值类型 函数名称
// 类中的私有方法要被外部引用要修改访问修饰符,我们主要用的就是这个函数
// 思考:如何把action()拆分成若个个处理步骤(函数)
private void action()
{
// Local iconic variables
// 局部图像变量
HObject ho_Image=null, ho_Rectangle, ho_ImageReduced;
HObject ho_ModelContours;
// Local control variables
//局部控制变量
HTuple hv_Read_Path, hv_FileExists, hv_Width=new HTuple();
HTuple hv_Height=new HTuple(), hv_WindowHandle=new HTuple();
HTuple hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2;
HTuple hv_ModelID, hv_Row1, hv_Column1, hv_Angle, hv_Score;
HTuple hv_Number;
// Initialize local and output iconic variables
//内部和外部图像变量初始化
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_Rectangle);
HOperatorSet.GenEmptyObj(out ho_ImageReduced);
HOperatorSet.GenEmptyObj(out ho_ModelContours);
try
{
// dev_update_off(...); only in hdevelop
if (HDevWindowStack.IsOpen())
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());
}
//文件路径,Htuple 类型的控制变量,在此相当于C#中的 String
hv_Read_Path = "C:/Users/Administrator/Desktop/1.BMP";
HOperatorSet.FileExists(hv_Read_Path, out hv_FileExists);
if ((int)(hv_FileExists) != 0)
{
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, hv_Read_Path);
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());

}
HOperatorSet.SetWindowAttr("background_color","black");
HOperatorSet.OpenWindow(0,0,hv_Width,hv_Height,0,"","",out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
set_display_font(hv_WindowHandle, 14, "mono", "true", "false");
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
}
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3);
}
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
}
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}
}
else
{
disp_message(3600, "未找到该文件!", "window", 12, 12, "black", "true");
}
//设置ROI区域
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
}
HOperatorSet.DrawRectangle2(3600, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1,
out hv_Length2);
ho_Rectangle.Dispose();
HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row, hv_Column, hv_Phi, hv_Length1,
hv_Length2);
//创建模版
ho_ImageReduced.Dispose();
HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", (new HTuple(-180)).TupleRad()
, (new HTuple(360)).TupleRad(), "auto", "auto", "use_polarity", "auto",
"auto", out hv_ModelID);
ho_ModelContours.Dispose();
HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);
//模版匹配
HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(-180)).TupleRad()
, (new HTuple(360)).TupleRad(), 0.5, 30, 0.5, "least_squares", 0, 0.9,
out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score);
if ((int)(new HTuple((new HTuple(hv_Row1.TupleLength())).TupleGreater(0))) != 0)
{
dev_display_shape_matching_results(hv_ModelID, "red", hv_Row1, hv_Column1,
hv_Angle, 1, 1, 0);
}
hv_Number = new HTuple(hv_Row1.TupleLength());
//用完清除模版
HOperatorSet.ClearShapeModel(hv_ModelID);
}
//捕获异常
catch (HalconException HDevExpDefaultException)
{
//异常处理
ho_Image.Dispose();
ho_Rectangle.Dispose();
ho_ImageReduced.Dispose();
ho_ModelContours.Dispose();
throw HDevExpDefaultException;
}
//处理完要内存释放
ho_Image.Dispose();
ho_Rectangle.Dispose();
ho_ImageReduced.Dispose();
ho_ModelContours.Dispose();
}
}

//主函数
#if !NO_EXPORT_APP_MAIN
public class HDevelopExportApp
{
static void Main(string[] args)
{
new HDevel

opExport();
}
}
#endif

相关主题