halcon基于局部阈值和区域后处理的六边形晶体提取
By
admin
at 2019-10-03 • 0人收藏 • 1559人看过
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio form";right=759;bottom=469) winform.add( button={cls="button";text="Button";left=20;top=38;right=185;bottom=119;z=2}; picturebox={cls="picturebox";left=217;top=0;right=760;bottom=470;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\crystal"); //读取图片原始大小 var Width, Height = HOperatorSetX.GetImageSize(Image); //将picturebox绑定到halcon窗口 var WindowHandle = HOperatorSetX.OpenWindow(0,0,winform.picturebox.width, winform.picturebox.height,winform.picturebox.hwnd,"transparent",""); winform.button.oncommand = function(id,event){ //crystal.hdev: extraction of hexagonally shaped crystals via local thresholding and region post-processing //线宽 HOperatorSetX.SetLineWidth( WindowHandle , 2 ); //空心 HOperatorSetX.SetDraw( WindowHandle , 'margin' ); //蓝色 HOperatorSetX.SetColor( WindowHandle , 'blue' ); //-> using a local threshold //模糊 var ImageMean = HOperatorSetX.MeanImage( Image, 21, 21 ); //动态自动阈值处理 var RegionDynThresh = HOperatorSetX.DynThreshold( Image, ImageMean, 8, 'dark' ); //-> extract connected components //断开 var ConnectedRegions = HOperatorSetX.Connection( RegionDynThresh ); //变换区域的形状(凸包性) var ConvexRegions = HOperatorSetX.ShapeTrans( ConnectedRegions, 'convex' ); //选择面积在600-2000之间区域 var LargeRegions = HOperatorSetX.SelectShape( ConvexRegions, 'area', 'and', 600, 2000 ); //根据灰度值的熵选择区域 var Crystals = HOperatorSetX.SelectGray( LargeRegions, Image, 'entropy', 'and', 1, 5.6 ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.DispObj( Crystals , WindowHandle ); } winform.show(); win.loopMessage(); return winform;
shape_trans(Region : RegionTrans : Type : )
函数作用:
变换区域的形状
参数Type的可选项解释如下:
convex:凸包性
ellipse:与输入区域有相同的矩和区域的椭圆
outer_circle:最小外接圆
inner_circle:最大内接圆
rectangle1:平行于坐标轴的最小外接矩形
rectangle2:最小外接矩形
inner_rectangle1:平行于坐标轴的最大内接矩形
inner_rectangle2:输入区域的骨架点拥有同输入区域的重心点最小的距离
注意:如果Type是outer_circle,最终的圆形区域不是完全覆盖输入区域,因为内部算法原理是由算子smallest_circle和gen_circle完成的。由smallest_circle计算的半径能够小到1/sqrt(2)-0.5个像素。同时,由gen_circle生成的圆在双方向上由0.5个像素转换。因此,当加入双方的影响,原因区域可能要超出返回圆形区域至多1个像素
登录后方可回帖