图像缺口问题
By
admin
at 2019-04-05 • 0人收藏 • 1443人看过
学习下缺口检测方法:
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( 'C:/Users/popdes/Desktop/f083.png'); var Width , Height = HOperatorSetX.GetImageSize( Image ); //设置展示框大小 var WindowHandle = HOperatorSetX.OpenWindow( 0 , 0 , winform.picturebox.width , winform.picturebox.height , winform.picturebox.hwnd , "transparent" , "" ); //设置需要显示图片的哪些部分(-1即全部显示) HOperatorSetX.SetPart( WindowHandle , 0 , 0 , Height-1 , Width-1 ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.SetColor( WindowHandle , "green" ); HOperatorSetX.SetLineWidth( WindowHandle , 3 ); //var index = 1; winform.button.oncommand = function(id,event){ var F083 = HOperatorSetX.ReadImage( 'C:/Users/popdes/Desktop/f083.png' ); //通过一个自动确定的全局阈值返回区域中的分段区域来分割单通道图像,选择暗的部分 //例如在同质照明的背景下分割字符很有效 var Region, UsedThreshold = HOperatorSetX.BinaryThreshold( F083, 'max_separability', 'dark' ); //以矩形结构为基础扩大一个选中区域 var RegionDilation = HOperatorSetX.DilationRectangle1( Region, 15, 10 ); //计算一个区域中的联通部分 var ConnectedRegions = HOperatorSetX.Connection( RegionDilation ); //将给定区域的形状与默认形状进行比较,如果该区域具有相似的形状,则将其返回 var SelectedRegions = HOperatorSetX.SelectShapeStd( ConnectedRegions, 'max_area', 70 ); //关闭半径小于3.5的孔 var RegionClosing = HOperatorSetX.ClosingCircle( SelectedRegions, 3.5 ); //缩小给定图像的区域 var ImageReduced = HOperatorSetX.ReduceDomain( F083, RegionClosing ); //关闭一个图像的一个灰度值 var ImageClosing = HOperatorSetX.GrayClosingRect( ImageReduced, 1, 20 ); //两个图像做减法 var ImageSub = HOperatorSetX.SubImage( ImageClosing, ImageReduced, 1, 0 ); //自动分割单通道图像, 选择亮的部分 var Region1, UsedThreshold1 = HOperatorSetX.BinaryThreshold( ImageSub, 'max_separability', 'light' ); //腐蚀圆(膨胀圆) var RegionDilation1 = HOperatorSetX.DilationCircle( Region1, 1 ); //计算联通区域 var ConnectedRegions1 = HOperatorSetX.Connection( RegionDilation1 ); //选择面积在50-1099区间的部分 var SelectedRegions1 = HOperatorSetX.SelectShape( ConnectedRegions1, 'area', 'and', 50, 1099 ); //转化图像区间 var RegionTrans = HOperatorSetX.ShapeTrans( SelectedRegions1, 'convex' ); HOperatorSetX.SetDraw( WindowHandle, 'margin' ); HOperatorSetX.DispObj( F083, WindowHandle ); HOperatorSetX.DispObj( RegionTrans, WindowHandle ); } winform.show(); win.loopMessage(); return winform;
原帖地址:http://www.ihalcon.com/read-11971.html
登录后方可回帖