zw版_zw中文增强版Halcon官方Delphi例程

时间:2025-02-19 22:36:44

【《zw版·delphi与halcon系列原创教程》
zw版_zw中文增强版Halcon官方Delphi例程

源码下载:http://files.cnblogs.com/files/ziwang/zw_match.zip

2016 zw中文增强版(delph2007,HALCON 11.01)

主要改动之处:
:: 将位于窗口class的私有变量,改为全局变量,便于分析,修改
:: 增加大量中文注解
:: 将原版的批量自动测量,改为手动单幅测量,便于观测效果
:: 多处节点,增加图像文件输出,便于理解,观测运行效果

zw版_zw中文增强版Halcon官方Delphi例程

注意A点,这个是中文增强版,增加的单步多节点截图,可以清楚看到相关细节。

http:www.cnblogs.com/ziwang/
《zw版·Halcon-delphi系列原创教程》

--------------
全部源码如下

 unit zw_match;
//
// HALCON/Delphi pattern matching and measure example
//
// 2016 zw中文增强版(delph2007,HALCON 11.01)
//
// 主要改动之处:
// :: 将位于窗口class的私有变量,改为全局变量,便于分析,修改
// :: 增加大量中文注解
// :: 将原版的批量自动测量,改为手动单幅测量,便于观测效果
// :: 多处节点,增加图像文件输出,便于理解,观测运行效果
// ::
// ::
//
// http://www.cnblogs.com/ziwang/
//《zw版·Halcon-delphi系列原创教程》
// interface uses
HALCONXLib_TLB,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, StdCtrls, sMemo, ExtCtrls; type
Tmwin = class(TForm)
HXCtrl: THWindowXCtrl;
CopyrightLabel: TLabel;
msg: TsMemo;
Panel1: TPanel;
CreateBtn: TButton;
StartBtn: TButton;
MatchingScoreLabel: TLabel;
MatchingTimeLabel: TLabel;
MatchingLabel: TLabel;
MeasureDistLabel: TLabel;
MeasureNumLabel: TLabel;
MeasureTimeLabel: TLabel;
MeasureLabel: TLabel;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure InitBtnClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure CreateBtnClick(Sender: TObject);
procedure StartBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
mwin: Tmwin;
xwin: HWindowX;
Framegrabber: HFramegrabberX;
Img, ImgReduced, ModelImage: HImageX;
RectangleRegion, ModelRegion, ModelRegionTrans: HRegionX;
Rectangle1, Rectangle2: HRegionX;
ShapeModel: HShapeModelX;
Matrix: HHomMat2DX;
Tuple: HTupleX;
Sys: HSystemX;
Measure1, Measure2: HMeasureX;
ImgWidth, ImgHeight: OleVariant;
Row, Column, Area: OleVariant;
Rect1Row, Rect1Col, Rect2Row, Rect2Col: Double;
RectPhi, RectLength1, RectLength2: Double; implementation {$R *.dfm} //------------------
procedure initData();
var dss:string;
begin
with mwin do begin //--------------
xwin.SetPart(,,,);//设置图像窗口尺寸
Img := Framegrabber.GrabImage();//从输入图像系列,加载图像
Img.DispObj(xwin);//显示图像
Img.WriteImage('png',,'tmp\ImgSr');
//---------检测图像尺寸
ImgWidth:=Img.GetImageSize(ImgHeight);
dss:='pic wid,hei:'+VarToStr(ImgWidth)+','+VarToStr(ImgHeight);msg.Lines.Add(dss);msg.Lines.Add('');
//--------- RectangleRegion.GenRectangle1(,,,);//设置检测区坐标(Row,Column,wid,hei)//??
xwin.SetColor('red');//设置矩形颜色
xwin.SetDraw('margin');//设置填充方式,margin,为边框模式,fill,为填充模式
xwin.SetLineWidth();//设置笔画粗细
RectangleRegion.DispObj(xwin);;//显示矩形box
//--------------
Area :=RectangleRegion.AreaCenter(Row,Column);//获取box区域面积,中心位置
dss:='Row,Column : 188,182';msg.Lines.Add(dss);
dss:='Row2,Column2 : 298,412';msg.Lines.Add(dss);
dss:='box区域面积area : '+VarToStr(area);msg.Lines.Add(dss);
dss:='box区域大小wid,hei : '+VarToStr(-+)+','+VarToStr(-+);msg.Lines.Add(dss);
dss:='box区域中心位置Row,Column : '+VarToStr(Row)+','+VarToStr(Column);msg.Lines.Add(dss);
//-------- 设置加工区参数
Rect1Row := Row - ;
Rect1Col := Column + ;
Rect2Row := Row + ;
Rect2Col := Column + ;
RectPhi := ;
RectLength1 := ;
RectLength2 := ;
end;
end; procedure createShapeModel(); //创建加工模型 ShapeModel
begin
with mwin do begin
//----设置显示窗口参数
xwin.SetColor('red');xwin.SetDraw('margin');xwin.SetLineWidth();
RectangleRegion.DispObj(xwin);
//----设置model图像
ImgReduced := Img.ReduceDomain(RectangleRegion);
ModelImage := ImgReduced.InspectShapeModel(ModelRegion,,);
//ImgReduced.WriteImage('png',0,'tmp\ImgReduced');
//ModelImage.WriteImage('png',0,'tmp\ModelImage');
xwin.DumpWindow('png','tmp\xwin_Shape01');
//------------建立辅助区域
Rectangle1.GenRectangle2(Rect1Row,Rect1Col,RectPhi,RectLength1,RectLength2);
Rectangle2.GenRectangle2(Rect2Row,Rect2Col,RectPhi,RectLength1,RectLength2);
//------------创建模型 ShapeModel
ShapeModel.CreateShapeModel(ImgReduced,,,Tuple.TupleRad(),
Tuple.TupleRad(),'none','use_polarity',,);
//----设置显示窗口参数
xwin.SetColor('green');xwin.SetDraw('fill');
ModelRegion.DispObj(xwin);
xwin.DumpWindow('png','tmp\xwin_Shape02');
xwin.SetColor('blue'); xwin.SetDraw('margin');
Rectangle1.DispObj(xwin);Rectangle2.DispObj(xwin);
xwin.DumpWindow('png','tmp\xwin_Shape03');
end;
end; procedure DoMeasurement;
var
S1, S2: Double;
RowCheck, ColumnCheck, AngleCheck, Score: OleVariant;
Rect1RowCheck, Rect1ColCheck, Rect2RowCheck, Rect2ColCheck: OleVariant;
RowEdgeFirst1, ColumnEdgeFirst1: OleVariant;
AmplitudeFirst1, RowEdgeSecond1: OleVariant;
ColumnEdgeSecond1, AmplitudeSecond1: OleVariant;
IntraDistance1, InterDistance1: OleVariant;
RowEdgeFirst2, ColumnEdgeFirst2: OleVariant;
AmplitudeFirst2, RowEdgeSecond2: OleVariant;
ColumnEdgeSecond2, AmplitudeSecond2: OleVariant;
IntraDistance2, InterDistance2: OleVariant;
MinDistance: OleVariant;
NumLeads: Longint;
dss:string;
begin
// 本demo,演示的是如何测量IC针脚间的距离
with mwin do begin
Sys.SetSystem('flush_graphic','false');//曲线系统图像自动刷新
Img := Framegrabber.GrabImage();//设置新图像
Img.Dispobj(xwin);//显示加工图像
//----注意,Mea00sr.png与 ImgSr.png的不同,旋转了一个角度
//----Framegrabber图像源是系列图片,通过旋转模拟加工现场,
//----每次调用GrabImage,会自动更新
xwin.DumpWindow('png','tmp\Mea00sr');
// Find the IC in the current image. 匹配模型
S1 := Sys.CountSeconds;
RowCheck := ShapeModel.FindShapeModel(Img,,Tuple.TupleRad(),0.7,,0.5,
'least_squares',,0.7,ColumnCheck,
AngleCheck,Score);
S2 := Sys.CountSeconds;
MatchingTimeLabel.Caption := Format('Time: %4.2f',[(S2-S1)*]);
MatchingScoreLabel.Caption := 'Score: ';
xwin.DumpWindow('png','tmp\Mea01');
//dss:=VarToStr(High(RowCheck));msg.lines.Add(dss);
//-----找到模型,开始测量
if not VarIsEmpty(RowCheck) and not VarIsArray(RowCheck) then begin
MatchingScoreLabel.Caption := Format('Score: %7.5f',[double(Score)]);
// Rotate the model for visualization purposes.旋转模型
Matrix.VectorAngleToRigid(Row,Column,,RowCheck,ColumnCheck,AngleCheck);
ModelRegionTrans := ModelRegion.AffineTransRegion(Matrix,'false');
xwin.SetColor('green');xwin.SetDraw('fill');
ModelRegionTrans.DispObj(xwin);
xwin.DumpWindow('png','tmp\Mea02');
// Compute the parameters of the measurement rectangles.
Rect1RowCheck := Matrix.AffineTransPixel(Rect1Row,Rect1Col,Rect1ColCheck);
Rect2RowCheck := Matrix.AffineTransPixel(Rect2Row,Rect2Col,Rect2ColCheck);
// For visualization purposes, generate the two rectangles as regions and
// display them.
Rectangle1.GenRectangle2(Rect1RowCheck,Rect1ColCheck,RectPhi+AngleCheck,
RectLength1,RectLength2);
Rectangle2.GenRectangle2(Rect2RowCheck,Rect2ColCheck,RectPhi+AngleCheck,
RectLength1,RectLength2);
xwin.SetColor('blue');xwin.SetDraw('margin');
Rectangle1.DispObj(xwin);Rectangle2.DispObj(xwin);
xwin.DumpWindow('png','tmp\Mea03');
// Do the actual measurements.开始测量
S1 := Sys.CountSeconds;
Measure1.GenMeasureRectangle2(Rect1RowCheck,Rect1ColCheck,
RectPhi+AngleCheck,RectLength1,RectLength2,
ImgWidth,ImgHeight,'bilinear');
Measure2.GenMeasureRectangle2(Rect2RowCheck,Rect2ColCheck,
RectPhi+AngleCheck,RectLength1,RectLength2,
ImgWidth,ImgHeight,'bilinear');
RowEdgeFirst1 := Measure1.MeasurePairs(Img,,,'positive','all',
ColumnEdgeFirst1,AmplitudeFirst1,
RowEdgeSecond1,ColumnEdgeSecond1,
AmplitudeSecond1,IntraDistance1,
InterDistance1);
RowEdgeFirst2 := Measure2.MeasurePairs(Img,,,'positive','all',
ColumnEdgeFirst2,AmplitudeFirst2,
RowEdgeSecond2,ColumnEdgeSecond2,
AmplitudeSecond2,IntraDistance2,
InterDistance2);
S2 := Sys.CountSeconds;
MeasureTimeLabel.Caption := Format('Time: %4.2f',[(S2-S1)*]);
xwin.SetColor('red');
xwin.DispLine(Tuple.TupleSub(RowEdgeFirst1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleSub(ColumnEdgeFirst1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
Tuple.TupleAdd(RowEdgeFirst1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleAdd(ColumnEdgeFirst1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
xwin.DumpWindow('png','tmp\Mea041');
xwin.DispLine(Tuple.TupleSub(RowEdgeSecond1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleSub(ColumnEdgeSecond1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
Tuple.TupleAdd(RowEdgeSecond1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleAdd(ColumnEdgeSecond1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
xwin.DumpWindow('png','tmp\Mea042');
xwin.DispLine(Tuple.TupleSub(RowEdgeFirst2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleSub(ColumnEdgeFirst2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
Tuple.TupleAdd(RowEdgeFirst2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleAdd(ColumnEdgeFirst2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
xwin.DumpWindow('png','tmp\Mea043');
xwin.DispLine(Tuple.TupleSub(RowEdgeSecond2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleSub(ColumnEdgeSecond2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
Tuple.TupleAdd(RowEdgeSecond2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
Tuple.TupleAdd(ColumnEdgeSecond2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
xwin.DumpWindow('png','tmp\Mea044');
NumLeads := (Tuple.TupleLength(IntraDistance1)+Tuple.TupleLength(IntraDistance2));
MeasureNumLabel.Caption := Format('Number of leads: %2d',[NumLeads]);
MinDistance := Tuple.TupleMin(Tuple.TupleConcat(InterDistance1,InterDistance2));
MeasureDistLabel.Caption := Format('Minimum lead distance: %6.3f',[double(MinDistance)]);
end;
xwin.DumpWindow('png','tmp\Mea09');
end;
Sys.SetSystem('flush_graphic','true');
// Force the graphics window to be updated by displaying an empty circle.
xwin.DispCircle(-,-,);
end; //------------------ procedure Tmwin.FormCreate(Sender: TObject);
begin
Set8087CW($27f);//设置FPU浮点计算参数,一般很少见
HXCtrl.ControlInterface.QueryInterface(IID_IHWindowX,xwin);//设置Halcon控件接口
Framegrabber := CoHFramegrabberX.Create;//设置图像输入,文件或相机
ShapeModel := CoHShapeModelX.Create;//创建匹配模型变量
//---------------
RectangleRegion := CoHRegionX.Create;//建立一个矩形,作为检测区
Rectangle1 := CoHRegionX.Create;//建立辅助测区
Rectangle2 := CoHRegionX.Create;//建立辅助测区 Matrix := CoHHomMat2DX.Create;//建立 矩阵变量,用于传递模型参数
Tuple := CoHTupleX.Create;//建立 Tuple元组变量,用于传递模型参数
Sys := CoHSystemX.Create;//建立sys系统变量,修改系统设置
Measure1 := CoHMeasureX.Create;//建立检测变量
Measure2 := CoHMeasureX.Create;//建立检测变量
//--------------
msg.Clear;
//--------------
{
OpenFramegrabber函数声明
变量Device:输入图像列表文件名,其他变量,一般采用默认参数
procedure OpenFramegrabber(Name:OleVariant; HorizontalResolution:OleVariant;
VerticalResolution:OleVariant; ImageWidth:OleVariant; ImageHeight:OleVariant;
StartRow:OleVariant; StartColumn:OleVariant; Field:OleVariant;
BitsPerChannel:OleVariant; ColorSpace:OleVariant; Generic:OleVariant;
ExternalTrigger:OleVariant; CameraType:OleVariant;
Device:OleVariant;
Port:OleVariant; LineIn:OleVariant; out AcqHandle:OleVariant);
} Framegrabber.OpenFramegrabber('File',,,,,,,'default',-,'default',
-,'default','pic/board.seq','default',,-);
msg.Lines.Add('pic from : '+'pic/board.seq');msg.Lines.Add('');
//------------- end; procedure Tmwin.FormDestroy(Sender: TObject);
begin
FreeAndNil(HXCtrl);
// Framegrabber:=nil;ShapeModel:=nil; xwin:=nil;img:=nil;box:=nil;
end; procedure Tmwin.FormShow(Sender: TObject);
begin
initData;
//-----
CreateBtn.Enabled := True;
StartBtn.Enabled := False;
end; procedure Tmwin.InitBtnClick(Sender: TObject);
begin
initData;
end; procedure Tmwin.StartBtnClick(Sender: TObject);
begin
DoMeasurement;
end; procedure Tmwin.CreateBtnClick(Sender: TObject);
begin
createShapeModel();
StartBtn.Enabled :=true;
end; end.

【《zw版·delphi与halcon系列原创教程》,网址,cnblogs.com/ziwang/】