aardio调用halcon控件实现:内螺纹压痕缺陷检测
By
admin
at 2019-03-07 • 0人收藏 • 3311人看过
本示例halcon代码和测试图片来源于:http://www.ihalcon.com/read-11736.html
目前halcon代码自动转换为aar代码工具还在编写,
所以下面的代码是我自己参照halcon代码调用aar中HOperatorSetX库改写来的
注: 这个库请去这个工程目录lib文件夹里查看,对应halcon13.0.2版本
import win.ui; /*DSG{{*/ mainForm = win.form(text="内螺纹压痕缺陷检测 - aardio版本";right=1177;bottom=674;border="dialog frame") mainForm.add( button={cls="button";text='\u2460加载示例图片';left=25;top=16;right=231;bottom=78;z=2}; button2={cls="button";text='\u2461中心区域圆';left=26;top=100;right=232;bottom=162;z=3}; button3={cls="button";text='\u2462查找缺陷';left=25;top=189;right=231;bottom=251;z=4}; button4={cls="button";text="循环识别图像缺陷";left=26;top=319;right=232;bottom=381;font=LOGFONT(h=-20);z=5}; picturebox={cls="picturebox";left=357;top=0;right=1178;bottom=675;border=1;z=1} ) /*}}*/ import com; var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}"); var WindowHandle; var Image; Image = HOperatorSetX.ReadImage( "img/iir- (3).bmp" ); var Width , Height = HOperatorSetX.GetImageSize( Image ); WindowHandle = HOperatorSetX.OpenWindow( 0 , 0 , mainForm.picturebox.width , mainForm.picturebox.height , mainForm.picturebox.hwnd,"transparent",""); mainForm.button.oncommand = function(id,event){ Image = HOperatorSetX.ReadImage( "img/iir- (3).bmp" ); HOperatorSetX.DispObj( Image , WindowHandle ); } var RegionFillUp; var ContCircle; var PolarTransImage; var Row , Column , Radius , StartPhi , EndPhi , PointOrder; mainForm.button2.oncommand = function(id,event){ var Region = HOperatorSetX.Threshold( Image , 170 , 255 ); var ConnectedRegions = HOperatorSetX.Connection( Region ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , "area" , "and" , 50000 , 999999 ); RegionFillUp = HOperatorSetX.FillUp( SelectedRegions ); HOperatorSetX.SetColor( WindowHandle , "green" ); HOperatorSetX.DispObj( RegionFillUp , WindowHandle ); var Contours = HOperatorSetX.GenContourRegionXld( RegionFillUp , "border_holes" ); Row , Column , Radius , StartPhi , EndPhi , PointOrder = HOperatorSetX.FitCircleContourXld( Contours , "algebraic", -1, 0, 0, 3, 2 ); ContCircle = HOperatorSetX.GenCircleContourXld( Row , Column , Radius , 0, 6.28318, "positive", 1 ); HOperatorSetX.SetColor( WindowHandle , "red" ); HOperatorSetX.SetLineWidth(WindowHandle,3); HOperatorSetX.DispObj( ContCircle , WindowHandle ); } mainForm.button3.oncommand = function(id,event){ HOperatorSetX.ClearWindow( WindowHandle ); PolarTransImage = HOperatorSetX.PolarTransImageExt( Image , Row , Column , 0, 6.28319, Radius-20, Radius+220, 2500, 300, 'nearest_neighbor' ); var ImageMax = HOperatorSetX.GrayDilationRect( PolarTransImage , 11 , 31 ); var Region = HOperatorSetX.Threshold( ImageMax , 180 , 255 ); var ImageResult = HOperatorSetX.PaintRegion( Region , ImageMax , 170 , "fill" ); var ImageMean = HOperatorSetX.MeanImage( ImageResult , 21 , 3 ); var ImageMean1 = HOperatorSetX.MeanImage( ImageMean , 211 , 3 ); var ImageSub = HOperatorSetX.SubImage( ImageMean , ImageMean1 , 1 , 128 ); var Region = HOperatorSetX.Threshold( ImageSub , 0 , 88 ); var Region1 = HOperatorSetX.Threshold( ImageSub , 168 , 255 ); var XYTransRegion = HOperatorSetX.PolarTransRegionInv( Region , Row , Column , 0, 6.28319, Radius-20, Radius+220, 2500, 300, 1600, 1200, 'nearest_neighbor' ); var XYTransRegion1 = HOperatorSetX.PolarTransRegionInv( Region1 , Row , Column , 0, 6.28319, Radius-20, Radius+220, 2500, 300, 1600, 1200, 'nearest_neighbor' ); var RegionUnion = HOperatorSetX.Union2( XYTransRegion , XYTransRegion1 ); var RegionOpening = HOperatorSetX.OpeningCircle( RegionUnion , 3.5 ); var RegionClosing = HOperatorSetX.ClosingCircle( RegionOpening , 15.5 ); var ConnectedRegions = HOperatorSetX.Connection( RegionClosing ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , 'area', 'and', 1200, 99999 ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.SetColor( WindowHandle , "red" ); HOperatorSetX.DispObj( SelectedRegions , WindowHandle ); } var indexImg = 1; mainForm.button4.oncommand = function(id,event){ Image = HOperatorSetX.ReadImage( "img/iir- ("++indexImg++").bmp" ); var Region = HOperatorSetX.Threshold( Image , 170 , 255 ); var ConnectedRegions = HOperatorSetX.Connection( Region ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , "area" , "and" , 50000 , 999999 ); RegionFillUp = HOperatorSetX.FillUp( SelectedRegions ); var Contours = HOperatorSetX.GenContourRegionXld( RegionFillUp , "border_holes" ); Row , Column , Radius , StartPhi , EndPhi , PointOrder = HOperatorSetX.FitCircleContourXld( Contours , "algebraic", -1, 0, 0, 3, 2 ); ContCircle = HOperatorSetX.GenCircleContourXld( Row , Column , Radius , 0, 6.28318, "positive", 1 ); PolarTransImage = HOperatorSetX.PolarTransImageExt( Image , Row , Column , 0, 6.28319, Radius-20, Radius+220, 2500, 300, 'nearest_neighbor' ); var ImageMax = HOperatorSetX.GrayDilationRect( PolarTransImage , 11 , 31 ); var Region = HOperatorSetX.Threshold( ImageMax , 180 , 255 ); var ImageResult = HOperatorSetX.PaintRegion( Region , ImageMax , 170 , "fill" ); var ImageMean = HOperatorSetX.MeanImage( ImageResult , 21 , 3 ); var ImageMean1 = HOperatorSetX.MeanImage( ImageMean , 211 , 3 ); var ImageSub = HOperatorSetX.SubImage( ImageMean , ImageMean1 , 1 , 128 ); var Region = HOperatorSetX.Threshold( ImageSub , 0 , 88 ); var Region1 = HOperatorSetX.Threshold( ImageSub , 168 , 255 ); var XYTransRegion = HOperatorSetX.PolarTransRegionInv( Region , Row , Column , 0, 6.28319, Radius-20, Radius+220, 2500, 300, 1600, 1200, 'nearest_neighbor' ); var XYTransRegion1 = HOperatorSetX.PolarTransRegionInv( Region1 , Row , Column , 0, 6.28319, Radius-20, Radius+220, 2500, 300, 1600, 1200, 'nearest_neighbor' ); var RegionUnion = HOperatorSetX.Union2( XYTransRegion , XYTransRegion1 ); var RegionOpening = HOperatorSetX.OpeningCircle( RegionUnion , 3.5 ); var RegionClosing = HOperatorSetX.ClosingCircle( RegionOpening , 15.5 ); var ConnectedRegions = HOperatorSetX.Connection( RegionClosing ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , 'area', 'and', 1200, 99999 ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.SetColor( WindowHandle , "red" ); HOperatorSetX.DispObj( SelectedRegions , WindowHandle ); indexImg++; if(indexImg>8){ indexImg = 1; } } mainForm.enableDpiScaling(); mainForm.show(); return win.loopMessage();
不要看上面代码好多啊, 函数的参数啊,好多啊,
库里面的智能提示会自动帮你写好参数位置, 不需要去记忆.
完整工程代码:
链接:https://pan.baidu.com/s/1iNoiSsiDZpOw-dLXKJKNug
提取码:uk3g
2 个回复 | 最后更新于 2019-03-10
登录后方可回帖
回复#1 @cuiqbo :
本来示例里就写了①②③,意思就是依次点下去, 这个是分步查看效果