码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-10-04 12:25:29      阅读:706      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

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

技术分享

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

 

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

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

  1 unit zw_match;
  2 //
  3 // HALCON/Delphi pattern matching and measure example
  4 //
  5 // 2016 zw中文增强版(delph2007,HALCON 11.01)
  6 //
  7 // 主要改动之处:
  8 // :: 将位于窗口class的私有变量,改为全局变量,便于分析,修改
  9 // :: 增加大量中文注解
 10 // :: 将原版的批量自动测量,改为手动单幅测量,便于观测效果
 11 // :: 多处节点,增加图像文件输出,便于理解,观测运行效果
 12 // ::
 13 // ::
 14 //
 15 // http://www.cnblogs.com/ziwang/
 16 //《zw版·Halcon-delphi系列原创教程》
 17 //
 18 
 19 interface
 20 
 21 uses
 22 HALCONXLib_TLB,
 23 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 24 Dialogs, OleCtrls, StdCtrls, sMemo, ExtCtrls;
 25 
 26 type
 27 Tmwin = class(TForm)
 28 HXCtrl: THWindowXCtrl;
 29 CopyrightLabel: TLabel;
 30 msg: TsMemo;
 31 Panel1: TPanel;
 32 CreateBtn: TButton;
 33 StartBtn: TButton;
 34 MatchingScoreLabel: TLabel;
 35 MatchingTimeLabel: TLabel;
 36 MatchingLabel: TLabel;
 37 MeasureDistLabel: TLabel;
 38 MeasureNumLabel: TLabel;
 39 MeasureTimeLabel: TLabel;
 40 MeasureLabel: TLabel;
 41 procedure FormShow(Sender: TObject);
 42 procedure FormCreate(Sender: TObject);
 43 procedure InitBtnClick(Sender: TObject);
 44 procedure FormDestroy(Sender: TObject);
 45 procedure CreateBtnClick(Sender: TObject);
 46 procedure StartBtnClick(Sender: TObject);
 47 private
 48 { Private declarations }
 49 public
 50 { Public declarations }
 51 end;
 52 
 53 var
 54 mwin: Tmwin;
 55 xwin: HWindowX;
 56 Framegrabber: HFramegrabberX;
 57 Img, ImgReduced, ModelImage: HImageX;
 58 RectangleRegion, ModelRegion, ModelRegionTrans: HRegionX;
 59 Rectangle1, Rectangle2: HRegionX;
 60 ShapeModel: HShapeModelX;
 61 Matrix: HHomMat2DX;
 62 Tuple: HTupleX;
 63 Sys: HSystemX;
 64 Measure1, Measure2: HMeasureX;
 65 ImgWidth, ImgHeight: OleVariant;
 66 Row, Column, Area: OleVariant;
 67 Rect1Row, Rect1Col, Rect2Row, Rect2Col: Double;
 68 RectPhi, RectLength1, RectLength2: Double;
 69 
 70 implementation
 71 
 72 {$R *.dfm}
 73 
 74 //------------------
 75 procedure initData();
 76 var dss:string;
 77 begin
 78 with mwin do begin
 79 
 80 //--------------
 81 xwin.SetPart(0,0,491,645);//设置图像窗口尺寸
 82 Img := Framegrabber.GrabImage();//从输入图像系列,加载图像
 83 Img.DispObj(xwin);//显示图像
 84 Img.WriteImage(png,0,tmp\ImgSr);
 85 //---------检测图像尺寸
 86 ImgWidth:=Img.GetImageSize(ImgHeight);
 87 dss:=pic wid,hei:+VarToStr(ImgWidth)+,+VarToStr(ImgHeight);msg.Lines.Add(dss);msg.Lines.Add(‘‘);
 88 //---------
 89 
 90 RectangleRegion.GenRectangle1(188,182,298,412);//设置检测区坐标(Row,Column,wid,hei)//??
 91 xwin.SetColor(red);//设置矩形颜色
 92 xwin.SetDraw(margin);//设置填充方式,margin,为边框模式,fill,为填充模式
 93 xwin.SetLineWidth(3);//设置笔画粗细
 94 RectangleRegion.DispObj(xwin);;//显示矩形box
 95 //--------------
 96 Area :=RectangleRegion.AreaCenter(Row,Column);//获取box区域面积,中心位置
 97 dss:=Row,Column : 188,182;msg.Lines.Add(dss);
 98 dss:=Row2,Column2 : 298,412;msg.Lines.Add(dss);
 99 dss:=box区域面积area : +VarToStr(area);msg.Lines.Add(dss);
100 dss:=box区域大小wid,hei : +VarToStr(412-182+1)+,+VarToStr(298-188+1);msg.Lines.Add(dss);
101 dss:=box区域中心位置Row,Column : +VarToStr(Row)+,+VarToStr(Column);msg.Lines.Add(dss);
102 //-------- 设置加工区参数
103 Rect1Row := Row - 102;
104 Rect1Col := Column + 5;
105 Rect2Row := Row + 107;
106 Rect2Col := Column + 5;
107 RectPhi := 0;
108 RectLength1 := 170;
109 RectLength2 := 5;
110 end;
111 end;
112 
113 
114 procedure createShapeModel(); //创建加工模型 ShapeModel
115 begin
116 with mwin do begin
117 //----设置显示窗口参数
118 xwin.SetColor(red);xwin.SetDraw(margin);xwin.SetLineWidth(3);
119 RectangleRegion.DispObj(xwin);
120 //----设置model图像
121 ImgReduced := Img.ReduceDomain(RectangleRegion);
122 ModelImage := ImgReduced.InspectShapeModel(ModelRegion,1,30);
123 //ImgReduced.WriteImage(‘png‘,0,‘tmp\ImgReduced‘);
124 //ModelImage.WriteImage(‘png‘,0,‘tmp\ModelImage‘);
125 xwin.DumpWindow(png,tmp\xwin_Shape01);
126 //------------建立辅助区域
127 Rectangle1.GenRectangle2(Rect1Row,Rect1Col,RectPhi,RectLength1,RectLength2);
128 Rectangle2.GenRectangle2(Rect2Row,Rect2Col,RectPhi,RectLength1,RectLength2);
129 //------------创建模型 ShapeModel
130 ShapeModel.CreateShapeModel(ImgReduced,4,0,Tuple.TupleRad(360),
131 Tuple.TupleRad(1),none,use_polarity,30,10);
132 //----设置显示窗口参数
133 xwin.SetColor(green);xwin.SetDraw(fill);
134 ModelRegion.DispObj(xwin);
135 xwin.DumpWindow(png,tmp\xwin_Shape02);
136 xwin.SetColor(blue); xwin.SetDraw(margin);
137 Rectangle1.DispObj(xwin);Rectangle2.DispObj(xwin);
138 xwin.DumpWindow(png,tmp\xwin_Shape03);
139 end;
140 end;
141 
142 procedure DoMeasurement;
143 var
144 S1, S2: Double;
145 RowCheck, ColumnCheck, AngleCheck, Score: OleVariant;
146 Rect1RowCheck, Rect1ColCheck, Rect2RowCheck, Rect2ColCheck: OleVariant;
147 RowEdgeFirst1, ColumnEdgeFirst1: OleVariant;
148 AmplitudeFirst1, RowEdgeSecond1: OleVariant;
149 ColumnEdgeSecond1, AmplitudeSecond1: OleVariant;
150 IntraDistance1, InterDistance1: OleVariant;
151 RowEdgeFirst2, ColumnEdgeFirst2: OleVariant;
152 AmplitudeFirst2, RowEdgeSecond2: OleVariant;
153 ColumnEdgeSecond2, AmplitudeSecond2: OleVariant;
154 IntraDistance2, InterDistance2: OleVariant;
155 MinDistance: OleVariant;
156 NumLeads: Longint;
157 dss:string;
158 begin
159 // 本demo,演示的是如何测量IC针脚间的距离
160 with mwin do begin
161 Sys.SetSystem(flush_graphic,false);//曲线系统图像自动刷新
162 Img := Framegrabber.GrabImage();//设置新图像
163 Img.Dispobj(xwin);//显示加工图像
164 //----注意,Mea00sr.png与 ImgSr.png的不同,旋转了一个角度
165 //----Framegrabber图像源是系列图片,通过旋转模拟加工现场,
166 //----每次调用GrabImage,会自动更新
167 xwin.DumpWindow(png,tmp\Mea00sr);
168 // Find the IC in the current image. 匹配模型
169 S1 := Sys.CountSeconds;
170 RowCheck := ShapeModel.FindShapeModel(Img,0,Tuple.TupleRad(360),0.7,1,0.5,
171 least_squares,4,0.7,ColumnCheck,
172 AngleCheck,Score);
173 S2 := Sys.CountSeconds;
174 MatchingTimeLabel.Caption := Format(Time: %4.2f,[(S2-S1)*1000]);
175 MatchingScoreLabel.Caption := Score: ;
176 xwin.DumpWindow(png,tmp\Mea01);
177 //dss:=VarToStr(High(RowCheck));msg.lines.Add(dss);
178 //-----找到模型,开始测量
179 if not VarIsEmpty(RowCheck) and not VarIsArray(RowCheck) then begin
180 MatchingScoreLabel.Caption := Format(Score: %7.5f,[double(Score)]);
181 // Rotate the model for visualization purposes.旋转模型
182 Matrix.VectorAngleToRigid(Row,Column,0,RowCheck,ColumnCheck,AngleCheck);
183 ModelRegionTrans := ModelRegion.AffineTransRegion(Matrix,false);
184 xwin.SetColor(green);xwin.SetDraw(fill);
185 ModelRegionTrans.DispObj(xwin);
186 xwin.DumpWindow(png,tmp\Mea02);
187 // Compute the parameters of the measurement rectangles.
188 Rect1RowCheck := Matrix.AffineTransPixel(Rect1Row,Rect1Col,Rect1ColCheck);
189 Rect2RowCheck := Matrix.AffineTransPixel(Rect2Row,Rect2Col,Rect2ColCheck);
190 // For visualization purposes, generate the two rectangles as regions and
191 // display them.
192 Rectangle1.GenRectangle2(Rect1RowCheck,Rect1ColCheck,RectPhi+AngleCheck,
193 RectLength1,RectLength2);
194 Rectangle2.GenRectangle2(Rect2RowCheck,Rect2ColCheck,RectPhi+AngleCheck,
195 RectLength1,RectLength2);
196 xwin.SetColor(blue);xwin.SetDraw(margin);
197 Rectangle1.DispObj(xwin);Rectangle2.DispObj(xwin);
198 xwin.DumpWindow(png,tmp\Mea03);
199 // Do the actual measurements.开始测量
200 S1 := Sys.CountSeconds;
201 Measure1.GenMeasureRectangle2(Rect1RowCheck,Rect1ColCheck,
202 RectPhi+AngleCheck,RectLength1,RectLength2,
203 ImgWidth,ImgHeight,bilinear);
204 Measure2.GenMeasureRectangle2(Rect2RowCheck,Rect2ColCheck,
205 RectPhi+AngleCheck,RectLength1,RectLength2,
206 ImgWidth,ImgHeight,bilinear);
207 RowEdgeFirst1 := Measure1.MeasurePairs(Img,2,90,positive,all,
208 ColumnEdgeFirst1,AmplitudeFirst1,
209 RowEdgeSecond1,ColumnEdgeSecond1,
210 AmplitudeSecond1,IntraDistance1,
211 InterDistance1);
212 RowEdgeFirst2 := Measure2.MeasurePairs(Img,2,90,positive,all,
213 ColumnEdgeFirst2,AmplitudeFirst2,
214 RowEdgeSecond2,ColumnEdgeSecond2,
215 AmplitudeSecond2,IntraDistance2,
216 InterDistance2);
217 S2 := Sys.CountSeconds;
218 MeasureTimeLabel.Caption := Format(Time: %4.2f,[(S2-S1)*1000]);
219 xwin.SetColor(red);
220 xwin.DispLine(Tuple.TupleSub(RowEdgeFirst1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
221 Tuple.TupleSub(ColumnEdgeFirst1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
222 Tuple.TupleAdd(RowEdgeFirst1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
223 Tuple.TupleAdd(ColumnEdgeFirst1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
224 xwin.DumpWindow(png,tmp\Mea041);
225 xwin.DispLine(Tuple.TupleSub(RowEdgeSecond1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
226 Tuple.TupleSub(ColumnEdgeSecond1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
227 Tuple.TupleAdd(RowEdgeSecond1,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
228 Tuple.TupleAdd(ColumnEdgeSecond1,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
229 xwin.DumpWindow(png,tmp\Mea042);
230 xwin.DispLine(Tuple.TupleSub(RowEdgeFirst2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
231 Tuple.TupleSub(ColumnEdgeFirst2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
232 Tuple.TupleAdd(RowEdgeFirst2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
233 Tuple.TupleAdd(ColumnEdgeFirst2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
234 xwin.DumpWindow(png,tmp\Mea043);
235 xwin.DispLine(Tuple.TupleSub(RowEdgeSecond2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
236 Tuple.TupleSub(ColumnEdgeSecond2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))),
237 Tuple.TupleAdd(RowEdgeSecond2,Tuple.TupleMult(RectLength2,Cos(AngleCheck))),
238 Tuple.TupleAdd(ColumnEdgeSecond2,Tuple.TupleMult(RectLength2,Sin(AngleCheck))));
239 xwin.DumpWindow(png,tmp\Mea044);
240 NumLeads := (Tuple.TupleLength(IntraDistance1)+Tuple.TupleLength(IntraDistance2));
241 MeasureNumLabel.Caption := Format(Number of leads: %2d,[NumLeads]);
242 MinDistance := Tuple.TupleMin(Tuple.TupleConcat(InterDistance1,InterDistance2));
243 MeasureDistLabel.Caption := Format(Minimum lead distance: %6.3f,[double(MinDistance)]);
244 end;
245 xwin.DumpWindow(png,tmp\Mea09);
246 end;
247 Sys.SetSystem(flush_graphic,true);
248 // Force the graphics window to be updated by displaying an empty circle.
249 xwin.DispCircle(-1,-1,0);
250 end;
251 
252 //------------------
253 
254 procedure Tmwin.FormCreate(Sender: TObject);
255 begin
256 Set8087CW($27f);//设置FPU浮点计算参数,一般很少见
257 HXCtrl.ControlInterface.QueryInterface(IID_IHWindowX,xwin);//设置Halcon控件接口
258 Framegrabber := CoHFramegrabberX.Create;//设置图像输入,文件或相机
259 ShapeModel := CoHShapeModelX.Create;//创建匹配模型变量
260 //---------------
261 RectangleRegion := CoHRegionX.Create;//建立一个矩形,作为检测区
262 Rectangle1 := CoHRegionX.Create;//建立辅助测区
263 Rectangle2 := CoHRegionX.Create;//建立辅助测区
264 
265 Matrix := CoHHomMat2DX.Create;//建立 矩阵变量,用于传递模型参数
266 Tuple := CoHTupleX.Create;//建立 Tuple元组变量,用于传递模型参数
267 Sys := CoHSystemX.Create;//建立sys系统变量,修改系统设置
268 Measure1 := CoHMeasureX.Create;//建立检测变量
269 Measure2 := CoHMeasureX.Create;//建立检测变量
270 //--------------
271 msg.Clear;
272 //--------------
273 {
274 OpenFramegrabber函数声明
275 变量Device:输入图像列表文件名,其他变量,一般采用默认参数
276 procedure OpenFramegrabber(Name:OleVariant; HorizontalResolution:OleVariant;
277 VerticalResolution:OleVariant; ImageWidth:OleVariant; ImageHeight:OleVariant;
278 StartRow:OleVariant; StartColumn:OleVariant; Field:OleVariant;
279 BitsPerChannel:OleVariant; ColorSpace:OleVariant; Generic:OleVariant;
280 ExternalTrigger:OleVariant; CameraType:OleVariant;
281 Device:OleVariant;
282 Port:OleVariant; LineIn:OleVariant; out AcqHandle:OleVariant);
283 }
284 
285 Framegrabber.OpenFramegrabber(File,1,1,0,0,0,0,default,-1,default,
286 -1,default,pic/board.seq,default,1,-1);
287 msg.Lines.Add(pic from : +pic/board.seq);msg.Lines.Add(‘‘);
288 //-------------
289 
290 
291 end;
292 
293 procedure Tmwin.FormDestroy(Sender: TObject);
294 begin
295 FreeAndNil(HXCtrl);
296 // Framegrabber:=nil;ShapeModel:=nil; xwin:=nil;img:=nil;box:=nil;
297 end;
298 
299 procedure Tmwin.FormShow(Sender: TObject);
300 begin
301 initData;
302 //-----
303 CreateBtn.Enabled := True;
304 StartBtn.Enabled := False;
305 end;
306 
307 procedure Tmwin.InitBtnClick(Sender: TObject);
308 begin
309 initData;
310 end;
311 
312 
313 procedure Tmwin.StartBtnClick(Sender: TObject);
314 begin
315 DoMeasurement;
316 end;
317 
318 procedure Tmwin.CreateBtnClick(Sender: TObject);
319 begin
320 createShapeModel();
321 StartBtn.Enabled :=true;
322 end;
323 
324 end.

 

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

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

标签:

原文地址:http://www.cnblogs.com/ziwang/p/4854412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!