halcon基本的图像分割(腐蚀,膨胀,选择)
By
admin
at 2019-10-02 • 1人收藏 • 2981人看过
import win.ui; /*DSG{{*/ mainForm = win.form(text="aardio工程2";right=957;bottom=667) mainForm.add( button={cls="button";text="Button";left=787;top=78;right=944;bottom=149;z=2}; picturebox={cls="picturebox";left=0;top=0;right=748;bottom=668;z=1} ) /*}}*/ import HOperatorSetX; //halcon var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}") //读取图片 var Image = HOperatorSetX.ReadImage("C:\Users\Public\Documents\MVTec\HALCON-13.0\examples\images\metal-parts\work_sheet_01"); //读取图片原始大小 var width,height = HOperatorSetX.GetImageSize(Image); //将picturebox绑定到halcon窗口,并设置窗口显示图片原始大小 var WindowHandle = HOperatorSetX.OpenWindow(0,0,width,height,mainForm.picturebox.hwnd,"transparent",""); var i =1; mainForm.button.oncommand = function(id,event){ //循环加载图片 if(i>=5){ i=1; } //清空之前窗口的显示 HOperatorSetX.ClearWindow( WindowHandle ); //读取图片 Image = HOperatorSetX.ReadImage("C:\Users\Public\Documents\MVTec\HALCON-13.0\examples\images\metal-parts\work_sheet_"++string.format("%02d", i)); //WindowHandle串口中显示image图片 HOperatorSetX.DispObj( Image , WindowHandle ); //模糊图像 var ImageMean = HOperatorSetX.MeanImage( Image, 201, 201 ); //动态阈值处理(亮色) var Region = HOperatorSetX.DynThreshold( Image, ImageMean, 0, 'light' ); //填充上面获取的那些区间 var RegionFillUp = HOperatorSetX.FillUp( Region ); //断开区块 var ConnectedRegions = HOperatorSetX.Connection( RegionFillUp ); //腐蚀上面圆形结构基础的区域,将小圆形区域腐蚀掉 var RegionErosion = HOperatorSetX.ErosionCircle( ConnectedRegions, 3.5 ); //膨胀上面圆形结构基础的区域,还原之前剩下的区域大小 var RegionDilation = HOperatorSetX.DilationCircle( RegionErosion, 3.5 ); //选择需要的区域(面积在2000到4000000之间,凸性在0.9到1,方形特性0.8-1之间的区域) var Objects = HOperatorSetX.SelectShape( RegionDilation, {'area';'convexity';'rectangularity'}, 'and', {2000;0.9;0.8}, {4000000;1;1} ); //设置显示区域颜色为绿色 HOperatorSetX.SetColor( WindowHandle , "green" ); //不填充样式 HOperatorSetX.SetDraw(WindowHandle,"margin"); //显示区域框 HOperatorSetX.DispObj( Objects , WindowHandle ); //下一个图片 i++; } mainForm.show(); return win.loopMessage();
登录后方可回帖