《zw版·Halcon-delphi系列原創教程》酸奶自動分類腳本(機器學習、人工智能)
Halcon強大的圖像處理能力,令人往往會忽視其內核,是更加彪悍的機器學習、人工智能。
分類,聚類分析,是機器學習、人工智能的核心算法之一,也是個典型的應用。
Halcon內置的聚類分析、機器學習模塊,就有:knn鄰近算法、向量機SVM、GMM高斯混合模型(Gaussian Mixture Model,或者混合高斯模型,也可以簡寫為MOG(Mixture of Gaussian)、MLP(多層神經網絡)等等。
相關模塊,基本上都是匯編級的高度優化,直接調用就可以。
目前國內、海外機器學習、人工智能方面的學者,沒有幾位重視這塊。
國外,可能是版權問題,畢竟,Halcon是售價高達數萬歐元(不是人民幣)的商業軟件,而且主要用於自控、機器視覺等工業領域,而不是大學。
國內,可能是對於Halcon的了解不夠,halcon,雖然在自控領域一家獨大(70%份額),本身非常低調,很少在行業外宣傳自己,也許是國人的逆向工程、D版,把德國人,也嚇壞了。
其實,圖像處理的核心,圖像識別、分類,都離不開機器學習、人工智能
大家看看opencv的發展路線就可以清楚看到,從cv1.0的圖像,到cv2.0、2.4的機器學習,以及目前cv3.0的GPU、cuda人工智能模塊,AI在其中所占據的份額越來越大。
Halcon因為面向一線生產線,所以很多機器學習、人工智能,都是黑箱式的,無需編程,直接調用,例如內置的ocr模塊,可以識別99%的標准工業字符:超市、海關、流水線、零配件
不過,Halcon也提供了大量的機器學習模塊,畢竟各種應用場合復雜,許多庫,必須進行定制。
Halcon自帶demo腳本:matching_multi_channel_yogurl.hdev
是一個簡單的機器學習、人工智能分類應用,也是個典型的應用場景
效果還是蠻好的,大家可以看到,圖2、圖4,圖像的角度不同,有旋轉,Halcon能夠輕輕松松識別。
這個腳本,AI方面不算復雜,建模就是先拍攝幾張產品的照片,直接匹配。
通常,Halcon建模,需要進行200次(默認參數)迭代。
選這個腳本,其中一個原因,是因為前幾天,有人在論壇詢問,如何對企業生產線的產品(零食好像?)進行自動分類。
腳本80多行,很簡單。
1 * This example demonstrates shape based matching 2 * with multi channel images 3 * 4 * Init display 5 dev_update_off () 6 Mode := 'multi channel' 7 ModelColor := 'green' 8 CircleColor := 'white' 9 Names := ['Pear Apple Hazelnut','Cherry Currant','Strawberry'] 10 read_image (Image, 'color/yogurt_model_01') 11 get_image_size (Image, Width, Height) 12 dev_close_window () 13 dev_open_window (0, 0, Width, Height, 'black', WindowHandle) 14 set_display_font (WindowHandle, 14, 'mono', 'true', 'false') 15 * 16 * Part 1: create shape models 17 ModelIDs := [] 18 for Index := 1 to 3 by 1 19 read_image (Image, 'color/yogurt_model_' + Index$'02') 20 dev_display (Image) 21 * 22 * Create ROI automatically 23 access_channel (Image, Channel1, 1) 24 fast_threshold (Channel1, Region, 75, 255, 20) 25 fill_up (Region, RegionFillUp) 26 opening_circle (RegionFillUp, RegionOpening, 170.5) 27 gen_contour_region_xld (RegionOpening, Contours, 'border') 28 fit_circle_contour_xld (Contours, 'geotukey', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder) 29 gen_circle (Circle, Row, Column, Radius / 2) 30 reduce_domain (Image, Circle, ImageReduced) 31 * 32 * Create model 33 create_shape_model (ImageReduced, 6, rad(0), rad(360), 'auto', 'auto', 'ignore_color_polarity', [35,50,15], 11, ModelID) 34 ModelIDs := [ModelIDs,ModelID] 35 * 36 * Display model 37 dev_set_color (CircleColor) 38 dev_set_draw ('margin') 39 dev_set_line_width (5) 40 dev_display (Circle) 41 get_shape_model_contours (Model1Contours, ModelID, 1) 42 dev_set_color (ModelColor) 43 dev_set_line_width (2) 44 dev_display_shape_matching_results (ModelIDs, ModelColor, Row, Column, 0.0, 1, 1, ModelID) 45 disp_message (WindowHandle, 'Create shape model ' + Names[Index - 1], 'window', 12, 12, 'black', 'true') 46 disp_message (WindowHandle, 'Press \'Run\' to continue', 'window', 450, 12, 'black', 'true') 47 stop () 48 endfor 49 * Main loop: Find yogurt 50 for Index := 1 to 10 by 1 51 read_image (Image, 'color/yogurt_' + Index$'02') 52 * Preprocessing: Reduce search domain to speed up matching 53 access_channel (Image, Channel1, 1) 54 fast_threshold (Channel1, Region, 50, 255, 20) 55 fill_up (Region, RegionFillUp) 56 erosion_rectangle1 (RegionFillUp, RegionErosion, 210, 210) 57 reduce_domain (Image, RegionErosion, ImageReduced) 58 * Find yogurt 59 find_shape_models (ImageReduced, ModelIDs, rad(0), rad(360), 0.80, 1, 0.5, 'least_squares', 0, 0.95, Row, Column, Angle, Score, Model) 60 * 61 * Display results 62 dev_display (Image) 63 gen_circle (Circle, Row, Column, Radius / 2) 64 dev_set_color (CircleColor) 65 dev_set_line_width (5) 66 dev_display (Circle) 67 get_shape_model_contours (ModelContours, Model, 1) 68 dev_set_color (ModelColor) 69 dev_set_line_width (2) 70 dev_display_shape_matching_results (ModelIDs, ModelColor, Row, Column, Angle, 1, 1, Model) 71 disp_message (WindowHandle, Names[find(ModelIDs,Model)] + ' found', 'window', 12, 12, 'black', 'true') 72 disp_message (WindowHandle, 'Score ' + Score, 'window', 50, 12, 'black', 'true') 73 if (Index < 10) 74 disp_continue_message (WindowHandle, 'black', 'true') 75 stop () 76 endif 77 endfor 78 * 79 * Cleanup memory 80 clear_shape_model (ModelIDs[0]) 81 clear_shape_model (ModelIDs[1]) 82 clear_shape_model (ModelIDs[2])
【《zw版·Halcon-delphi系列原創教程》,網址,cnblogs.com/ziwang/】