识别光学芯片中间的波导
By
笑口常开
at 2019-03-22 • 0人收藏 • 2354人看过
之前我把这个halcon代码和aar工程的代码发布在了halcon的论坛里
http://www.ihalcon.com/read-11823-1.html
这种办法, 通用性不强, 原来的代码如下:
import win.ui; /*DSGpw_*/ mainForm = win.form(text="aardio + halcon 矫正并找波导内芯 , Aardio.com.cn";right=883;bottom=561) mainForm.add( button={cls="button";text="检测";left=7;top=43;right=182;bottom=101;z=1}; picturebox={cls="picturebox";left=214;top=0;right=884;bottom=562;z=2} ) /**/ import com import com.activeX var HOperatorSetX; //检查并注册halcon控件 try{ HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}"); } catch(e){ com.activeX.regsvr32As("\res\halconx.dll"); HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}"); } //调用halcon控件 HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}") var Image = HOperatorSetX.ReadImage( "img/555.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 ); mainForm.button.oncommand = function(id,event){ var ImageScaled = HOperatorSetX.ScaleImage( Image , 6.07143, -164 ); var GrayImage = HOperatorSetX.Rgb1ToGray( ImageScaled ); var Region = HOperatorSetX.Threshold( GrayImage , 63 , 255 ); var RegionFillUp = HOperatorSetX.FillUp( Region ); var Row , Column , Phi , Length1 , Length2 = HOperatorSetX.SmallestRectangle2( RegionFillUp ); var Rad = math.rad(270) var HomMat2d = HOperatorSetX.VectorAngleToRigid( Row , Column , Phi , Row , Column , Rad ); var ImageAffineTrans = HOperatorSetX.AffineTransImage( GrayImage , HomMat2d , 'constant', 'false' ); HOperatorSetX.DispObj( ImageAffineTrans , WindowHandle ); var ImageGauss = HOperatorSetX.GaussImage( ImageAffineTrans , 5 ); var DiffOfGauss = HOperatorSetX.DiffOfGauss( ImageGauss , 3 , 1.6 ); var Region = HOperatorSetX.Threshold( DiffOfGauss , -73 , -3 ); var ConnectedRegions = HOperatorSetX.Connection( Region ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , 'width', 'and', 477.25, 585.06 ); var RegionClosing = HOperatorSetX.ClosingRectangle1( SelectedRegions , Width , Height ); var Row1 , Column1 , Row2 , Column2 = HOperatorSetX.InnerRectangle1( RegionClosing ); var Rectangle = HOperatorSetX.GenRectangle1( Row1 , Column1 , Row2 , Column2 ); var SortedRegions = HOperatorSetX.SortRegion( Rectangle , 'first_point', 'true', 'row' ); var Number = HOperatorSetX.CountObj( SortedRegions ); HOperatorSetX.SetColor( WindowHandle , "green" ); HOperatorSetX.SetDraw( WindowHandle , "margin" ); var columnPublic = 0; for(i=2;Number-1;1){ var ObjectSelected = HOperatorSetX.SelectObj( SortedRegions , i ); var Area , Row , Column = HOperatorSetX.AreaCenter( ObjectSelected ); if(i == 2){ columnPublic = Column; } var Rectangle = HOperatorSetX.GenRectangle1( Row-3 , columnPublic-270 , Row+3 , columnPublic+270 ); HOperatorSetX.DispObj( Rectangle , WindowHandle ); } } mainForm.show(); return win.loopMessage();
这两天看了看其他算子, 发现设置ROI后, 直接用 LinesGauss 更好
mainForm.button2.oncommand = function(id,event){ HOperatorSetX.SetDraw( WindowHandle , "margin" ); HOperatorSetX.SetColor( WindowHandle , "green" ); HOperatorSetX.SetLineWidth( WindowHandle , 3 ); var GrayImage = HOperatorSetX.Rgb1ToGray( Image ); var Region = HOperatorSetX.Threshold( GrayImage , 63 , 255 ); var RegionFillUp = HOperatorSetX.FillUp( Region ); var Row , Column , Phi , Length1 , Length2 = HOperatorSetX.SmallestRectangle2( RegionFillUp ); //旋转图像 var Rad = math.rad(0) var HomMat2d = HOperatorSetX.VectorAngleToRigid( Row , Column , Phi , Row , Column , Rad ); var ImageAffineTrans = HOperatorSetX.AffineTransImage( GrayImage , HomMat2d , 'constant', 'false' ); HOperatorSetX.DispObj( ImageAffineTrans , WindowHandle ); win.delay(2000) //高斯模糊 var ImageGauss = HOperatorSetX.GaussImage( ImageAffineTrans , 5 ); //绘制ROI区间 var Rectangle = HOperatorSetX.GenRectangle1( 274.757, 350.787, 645.919, 816.213 ); HOperatorSetX.DispObj( Rectangle , WindowHandle ); //win.delay(2000) HOperatorSetX.DispObj( ImageAffineTrans , WindowHandle ); var ImageReduced = HOperatorSetX.ReduceDomain( ImageGauss , Rectangle ); //黑白反转图像 var ImageInvert = HOperatorSetX.InvertImage( ImageReduced ); //对比度调大 var ImageScaled = HOperatorSetX.ScaleImage( ImageInvert , 2.42857, -364 ); //模糊图像 var ImageMean = HOperatorSetX.MeanImage( ImageScaled , Width , 1 ); //抽取高斯线性线段 var Lines = HOperatorSetX.LinesGauss( ImageMean , 3, 0.5, 1, 'dark', 'false', 'bar-shaped', 'true' ); //排序 var SortedContours = HOperatorSetX.SortContoursXld( Lines , 'upper_left', 'true', 'row' ); //显示线段 HOperatorSetX.DispObj( SortedContours , WindowHandle ); //绘制为实体线 var Polygons = HOperatorSetX.GenPolygonsXld( SortedContours , 'ramer', 2 ); //求出每根线的位置 var BeginRow , BeginCol , EndRow , EndCol , Length , Phi = HOperatorSetX.GetLinesXld( Polygons ); //显示位置信息 HOperatorSetX.DispText( WindowHandle , BeginRow , "image" , BeginRow, BeginCol , "black" , , ); }
1 个回复 | 最后更新于 2019-03-22
登录后方可回帖
Halcon代码如下: