检查医药行业药瓶里液位高低,用到了shape-based matching和1D measuring测量
By
admin
at 2019-03-13 • 1人收藏 • 2381人看过
在本例中检查水平液体高度(制药行业)。
任务是检查每一个瓶子的水平高度。为此,我们首先确定每个瓶子头通过应用shape-based匹配,然后我们通过测量使用1 d测量灰度变化来测量.
import win.ui; /*DSG{{*/ mainForm = win.form(text="aardio + Halcon 识别瓶中液体高度示例, 来自 Aardio.com.cn";right=886;bottom=573) mainForm.add( button={cls="button";text="识别缺陷";left=13;top=20;right=200;bottom=88;z=2}; picturebox={cls="picturebox";left=226;top=0;right=887;bottom=574;z=1} ) /*}}*/ //import console //console.open() //import HOperatorSetX; import com; HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}") Image = HOperatorSetX.ReadImage( "img/ampoules_01.png" ); var Width , Height = HOperatorSetX.GetImageSize( Image ); var WindowHandle = HOperatorSetX.OpenWindow( 0 , 0 , mainForm.picturebox.width , mainForm.picturebox.height , mainForm.picturebox.hwnd , "transparent", "" ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.SetDraw( WindowHandle , "margin" ); HOperatorSetX.SetLineWidth( WindowHandle , 2 ); //HOperatorSetX.SetColor( WindowHandle , "red" ); var Rectangle = HOperatorSetX.GenRectangle1( 230 , 280 , 317 , 330 ); var ImageReduced = HOperatorSetX.ReduceDomain( Image , Rectangle ); var ModelID = HOperatorSetX.CreateShapeModel( ImageReduced , 'auto', 0, 0, 'auto', 'auto', 'use_polarity', 'auto', 'auto' ); var MeasureHandle = HOperatorSetX.GenMeasureRectangle2( 0 , 0 , HOperatorSetX.tupleRad(90) , 75 , 20 , Width , Height , 'bilinear' ); var Tolerance = 15 var index = 1; mainForm.button.oncommand = function(id,event){ Image = HOperatorSetX.ReadImage( "img/ampoules_0"++index++".png" ); var ColumnEdges,FillLevelHeight = {},{}; var Row , Column , Angle , Score = HOperatorSetX.FindShapeModel( Image , ModelID , 0, 0, 0.7, 0, 0.1, 'least_squares', 0, 0.9 ); var he = 0; for(i=1;#Row;1){ he = he+Row[i] } var MeanRow = he/(#Row); var RefLevel = MeanRow - 160; he = 0; var cmax,cmin; for(i=1;#Column;1){ he = he+Column[i] if(i==1){ cmax = Column[i] cmin = Column[i] }else { if(cmax < Column[i]){ cmax = Column[i]; } if(cmin > Column[i]){ cmin = Column[i]; } } } var MeanColumn = he/(#Column); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.SetLineWidth( WindowHandle , 1 ); HOperatorSetX.SetColor( WindowHandle , "white" ); var Rectangle = HOperatorSetX.GenRectangle2( RefLevel , MeanColumn , 0 , 30+( cmax - cmin )/2 , Tolerance ); HOperatorSetX.DispObj( Rectangle , WindowHandle ); HOperatorSetX.SetLineWidth( WindowHandle , 2 ); var Errors = 0; for(Idx=1;#Score;1){ HOperatorSetX.TranslateMeasure( MeasureHandle , MeanRow-135 , Column[Idx] ); var RowEdge , ColumnEdge , Amplitude , Distance = HOperatorSetX.MeasurePos( Image , MeasureHandle , 2 , 7 , "all" , "first" ); table.push(FillLevelHeight,RowEdge); table.push(ColumnEdges,ColumnEdge); var Cross = HOperatorSetX.GenCrossContourXld( RowEdge , ColumnEdge , 15 , 0 ); var FillLevel = HOperatorSetX.GenRectangle2( RowEdge, ColumnEdge, 0, 28, 20 ); if(math.abs( FillLevelHeight[Idx] - RefLevel ) >= Tolerance ){ var ChamberSingle = HOperatorSetX.GenRectangle2( MeanRow - 133, Column[Idx], 0, 35, 90 ); var Cross = HOperatorSetX.GenCrossContourXld( FillLevelHeight[Idx], ColumnEdges[Idx], 15, 0 ); var FillLevel = HOperatorSetX.GenRectangle2( FillLevelHeight[Idx], ColumnEdges[Idx], 0, 28, 20 ); Errors = Errors+1; HOperatorSetX.SetColor( WindowHandle , "red" ); HOperatorSetX.DispObj( ChamberSingle , WindowHandle ); }else { HOperatorSetX.SetColor( WindowHandle , "green" ); } HOperatorSetX.DispObj( FillLevel , WindowHandle ); HOperatorSetX.DispObj( Cross , WindowHandle ); win.delay(200) } index++; if(index>8){ index = 1; } } mainForm.show(); return win.loopMessage();
请更新aardio到最新版使用.
halcon源代码位于:
C:\Program Files\MVTec\HALCON-13.0\examples\hdevelop\Applications\Measuring-2D\measure_fill_level.hdev
登录后方可回帖