快速傅立叶变换(FFT)检测塑料制品表面的小纹理缺陷
By
admin
at 2019-03-11 • 0人收藏 • 2391人看过
该程序演示了如何使用快速傅立叶变换(FFT)检测塑料制品表面的小纹理缺陷。
首先,我们使用高斯滤波器构造一个合适的滤波器。然后,利用快速傅立叶变换对图像和滤波器进行卷积。最后,利用形态学算子对滤波后的图像进行缺陷检测
该程序位于:
C:\Program Files\MVTec\HALCON-13.0\examples\hdevelop\Applications\Surface-Inspection\detect_indent_fft.hdev
import win.ui; /*DSG{{*/ mainForm = win.form(text="aardio工程35";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 HOperatorSetX; import com; HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}") Image = HOperatorSetX.ReadImage( "img/plastics_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" ); HOperatorSetX.OptimizeRftSpeed( Width , Height , "standard" ); var Sigma1 = 10.0 var Sigma2 = 3.0 var ImageGauss1 = HOperatorSetX.GenGaussFilter( Sigma1 , Sigma1 , 0.0 , 'none', 'rft' , Width , Height ); var ImageGauss2 = HOperatorSetX.GenGaussFilter( Sigma2 , Sigma2 , 0.0 , 'none', 'rft' , Width , Height ); var ImageSub = HOperatorSetX.SubImage( ImageGauss1 , ImageGauss2 , 1 , 0 ); var index = 1; mainForm.button.oncommand = function(id,event){ //for(i=1;11;1){ var Image = HOperatorSetX.ReadImage( "img/plastics_"++string.format("%02s", index)++".png" ); var ImageGray = HOperatorSetX.Rgb1ToGray( Image ); var ImageFFT = HOperatorSetX.RftGeneric( ImageGray , 'to_freq', 'none', 'complex' , Width ); var ImageConvol = HOperatorSetX.ConvolFft( ImageFFT , ImageSub ); var ImageFFT = HOperatorSetX.RftGeneric( ImageConvol , 'from_freq', 'n', 'real' , Width ); var ImageResult = HOperatorSetX.GrayRangeRect( ImageFFT , 10 , 10 ); var Min , Max , Range = HOperatorSetX.MinMaxGray( ImageResult , ImageResult , 0 ); var maxdd = math.max(5.55,Max*0.8 ) var Region = HOperatorSetX.Threshold( ImageResult , maxdd , 255 ); var ConnectedRegions = HOperatorSetX.Connection( Region ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , 'area', 'and', 4, 99999 ); var RegionUnion = HOperatorSetX.Union1( SelectedRegions ); var RegionClosing = HOperatorSetX.ClosingCircle( RegionUnion , 10 ); var ConnectedRegions = HOperatorSetX.Connection( RegionClosing ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , 'area', 'and', 10, 99999 ); var Area , Row , Column = HOperatorSetX.AreaCenter( SelectedRegions ); HOperatorSetX.DispObj( Image , WindowHandle ); if(type(Area) == type.table){ var number = #Area; if(number){ for(i=1;number;1){ var ContCircle = HOperatorSetX.GenCircleContourXld( Row[i] , Column[i] , 30 , 0 , math.rad(360) , "positive" , 1 ); HOperatorSetX.DispObj( ContCircle , WindowHandle ); } } }else if(type(Area)==type.number) { var ContCircle = HOperatorSetX.GenCircleContourXld( Row , Column , 30 , 0 , math.rad(360) , "positive" , 1 ); HOperatorSetX.DispObj( ContCircle , WindowHandle ); } // } index++; if(index>11){ index = 1; } } mainForm.show(); return win.loopMessage();
上面代码最后,是和halcon代码不同的地方
由于aardio没有halcom中的tupe这个形式, 所以只能拆分开来用 , 首先判断是不是数据表table, 如果不是那么有可能是只检测到一个缺陷区域, 那么就是数值number , 如果没有缺陷, 那么就是返回的null
登录后方可回帖