halcon中检测模糊图像中的划痕缺陷
By
admin
at 2019-03-12 • 0人收藏 • 3163人看过
源程序位于:
C:\Program Files\MVTec\HALCON-13.0\examples\hdevelop\Applications\Surface-Inspection\detect_mura_defects_blur.hdev
使用此版本代码,请升级aardio到最新版本
import win.ui; /*DSG{{*/ mainForm = win.form(text="aardio工程35";right=1305;bottom=778) mainForm.add( button={cls="button";text="Button";left=13;top=235;right=225;bottom=306;z=2}; picturebox={cls="picturebox";left=373;top=0;right=1306;bottom=779;z=1} ) /*}}*/ import console console.open() import com var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}") var Image = HOperatorSetX.ReadImage( "img/mura_defects_blur_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 , 3 ); HOperatorSetX.SetColor( WindowHandle , "red" ); var ScaleFactor = 0.4 var calculate_lines_gauss_parameters = function(MaxLineWidth,ContrastHigh,ContrastLow){ if(ContrastLow == null){ ContrastLow = ContrastHigh / 3.0; } if(MaxLineWidth < math.sqrt(3.0)){ // Note that LineWidthMax < sqrt(3.0) would result in a Sigma < 0.5, // which does not make any sense, because the corresponding smoothing // filter mask would be of size 1x1. // To avoid this, LineWidthMax is restricted to values greater or equal // to sqrt(3.0) and the contrast values are adapted to reflect the fact // that lines that are thinner than sqrt(3.0) pixels have a lower contrast // in the smoothed image (compared to lines that are sqrt(3.0) pixels wide). ContrastLow = ContrastLow * MaxLineWidth / math.sqrt(3.0); ContrastHigh = ContrastHigh * MaxLineWidth / math.sqrt(3.0); MaxLineWidth = math.sqrt(3.0); } var HalfWidth = MaxLineWidth/2.0 Sigma = HalfWidth / math.sqrt(3.0) Help = -2.0*HalfWidth / (math.sqrt(6.283185307178)*(Sigma**3.0)) * math.exp(-0.5 * ((HalfWidth/Sigma)**2.0)) High = math.abs( ContrastHigh * Help ); Low = math.abs( ContrastLow * Help ); return Sigma,Low,High; } var index = 1; mainForm.button.oncommand = function(id,event){ var Sigma,Low,High = calculate_lines_gauss_parameters(17,25,3); var Image = HOperatorSetX.ReadImage( "img/mura_defects_blur_0"++index++".png" ); var ImageR , ImageG , ImageB = HOperatorSetX.Decompose3( Image ); //correct side illumination var ImageFFT = HOperatorSetX.RftGeneric( ImageB , 'to_freq', 'none', 'complex' , Width ); var ImageGauss = HOperatorSetX.GenGaussFilter( 100 , 100 , 0 , "n" , "rft" , Width , Height ); var ImageConvol = HOperatorSetX.ConvolFft( ImageFFT , ImageGauss ); var ImageFFT1 = HOperatorSetX.RftGeneric( ImageConvol , 'from_freq', 'none', 'byte' , Width ); var ImageSub = HOperatorSetX.SubImage( ImageB , ImageFFT1 , 2 , 100 ); var ImageZoomed = HOperatorSetX.ZoomImageFactor( ImageSub , ScaleFactor , ScaleFactor , "constant" ); var Domain = HOperatorSetX.GetDomain( ImageZoomed ); var RegionErosion = HOperatorSetX.ErosionRectangle1( Domain , 7 , 7 ); var ImageReduced = HOperatorSetX.ReduceDomain( ImageZoomed , RegionErosion ); var Lines = HOperatorSetX.LinesGauss( ImageReduced , Sigma , Low , High , 'dark', 'true', 'gaussian', 'true' ); var HomMat2dIdentity = HOperatorSetX.HomMat2dIdentity( ); var HomMat2dScale = HOperatorSetX.HomMat2dScaleLocal( HomMat2dIdentity , 1/ScaleFactor , 1/ScaleFactor ); var ContoursAffineTrans = HOperatorSetX.AffineTransContourXld( Lines , HomMat2dScale ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.DispObj( ContoursAffineTrans , WindowHandle ); index++; if(index>3){ index = 1; } } mainForm.show(); return win.loopMessage();
登录后方可回帖