HALCON的shape-based匹配找到SMD电容,并显示出匹配长宽大小的变化
By
笑口常开
at 2019-03-25 • 0人收藏 • 2040人看过
aardio中代码如下;
import win.ui; /*DSG{{*/ var winform = win.form(text="建立灰度模版实现匹配 Htmlyaout.cn";right=759;bottom=469) winform.add( button={cls="button";text="检测";left=9;top=39;right=196;bottom=124;z=2}; picturebox={cls="picturebox";left=238;top=0;right=760;bottom=470;z=1} ) /*}}*/ import com //调用halcon控件 HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}") var Image = HOperatorSetX.ReadImage( "img/smd_capacitors_01.png" ); var Width , Height = HOperatorSetX.GetImageSize( Image ); var WindowHandle = HOperatorSetX.OpenWindow( 0 , 0 , winform.picturebox.width , winform.picturebox.height , winform.picturebox.hwnd , "transparent" , "" ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.SetColor( WindowHandle , "green" ); HOperatorSetX.SetLineWidth( WindowHandle , 3 ); var index = 1; winform.button.oncommand = function(id,event){ Image = HOperatorSetX.ReadImage( "img/smd_capacitors_0"++index++".png" ); HOperatorSetX.DispObj( Image , WindowHandle ); //首先绘制一个圆角矩形 var Contour = HOperatorSetX.GenContourPolygonRoundedXld( {50;100;100;50;50}, {50;50;150;150;50}, {6;6;6;6;6}, 1 ); //创建灰度图像模版 var Image1 = HOperatorSetX.GenImageConst( 'byte', 200, 150 ); //用灰度图填充矩形 var ImageModel = HOperatorSetX.PaintXld( Contour, Image1, 128 ); //创建灰度匹配模版 var ModelID = HOperatorSetX.CreateAnisoShapeModel( ImageModel, 'auto', -math.rad(10), math.rad(20), 'auto', 0.9, 1.7, 'auto', 0.9, 1.1, 'auto', 'none', 'use_polarity', 'auto', 20 ); //取得模版id var ModelContours = HOperatorSetX.GetShapeModelContours( ModelID, 1 ); //在图像中查找模版 var Row, Column, Angle, ScaleR, ScaleC, Score = HOperatorSetX.FindAnisoShapeModel( Image, ModelID, -math.rad(10), math.rad(20), 0.9, 1.7, 0.9, 1.1, 0.7, 0, 0.5, 'least_squares', 0, 0.8 ); // for(I=1;#Score;1){ //创建二维变换矩阵 var HomMat2D = HOperatorSetX.HomMat2dIdentity( ); //增加缩放 var HomMat2D = HOperatorSetX.HomMat2dScale( HomMat2D, ScaleR[I], ScaleC[I], 0, 0 ); //增加旋转 var HomMat2D = HOperatorSetX.HomMat2dRotate( HomMat2D, Angle[I], 0, 0 ); //增加变换 var HomMat2D = HOperatorSetX.HomMat2dTranslate( HomMat2D, Row[I], Column[I] ); //匹配轮廓 var ContoursTrans = HOperatorSetX.AffineTransContourXld( ModelContours, HomMat2D ); HOperatorSetX.DispObj( ContoursTrans , WindowHandle ); } //清除模版 HOperatorSetX.ClearShapeModel( ModelID ); index++; if(index>4){ index = 1; } } winform.show(); win.loopMessage(); return winform;
halcon源程序:
C:\Program Files\MVTec\HALCON-13.0\examples\hdevelop\Matching\Shape-Based\find_aniso_shape_model.hdev
登录后方可回帖