《zw版·Halcon-delphi系列原创教程》
水果自动分类脚本(机器学习、人工智能)
前面介绍了超市,流水线,酸奶的自动分类算法,下面再介绍一个水果的自动分类算法。
Halcon强大的图像处理能力,令人往往会忽视其更加彪悍的机器学习、人工智能。
分类,聚类分析,是机器学习、人工智能的核心算法之一,也是个典型的应用。
Halcon内置的聚类分析、机器学习模块,就有:knn邻近算法、向量机SVM、GMM高斯混合模型(Gaussian Mixture Model,或者混合高斯模型,也可以简写为MOG(Mixture of Gaussian)、MLP(多层神经网络)等等。
而且相关基本上都是汇编级的高度优化,直接调用就可以。
目前国内、海外机器学习、人工智能方面的学者,没有几位重视这块。
国外,可能是版权问题,毕竟,Halcon是售价高达数万欧元(不是人民币)的商业软件,而且主要用于自控、机器视觉等工业领域,而不是大学。
国内,可能是对于Halcon的了解不够。
其实,图像处理的核心,图像识别、分类,都离不开机器学习、人工智能
大家看看opencv的发展路线就可以清楚看到,从cv1.0的图像,到cv1.0的机器学习,以及目前cv3.0的GPU、cuda人工智能模块,AI在其中所占据的份额越来越大。
Halcon因为面向一线生产线,所以很多机器学习、人工智能,都是黑箱式的,无需编程,直接调用,录入内置的ocr模块,可以识别99%的标准工业字符:超市、海关、流水线
不过,Halcon也提供了大量的机器学习模块,毕竟各种应用场合复制,必须进行定制。
这个脚本,AI方面不算复杂,建模就是先拍摄几张产品的照片,直接匹配。
通常,Halcon建模,需要进行200次(默认参数)迭代。
脚本80多行,很简单。
虽然这个脚本和前面的酸奶分类脚本,都很简单,其实,应用领域很广
自动流水线、物流、智能仓库等,无论是元器件的自动识别、包裹自动分类,以及产品的QC等等,核心模块,就是这些代码、算法、
选这个脚本,其中一个原因,是因为前几天,有人在论坛询问,如何对企业生产线的产品(零食好像?)进行自动分类。
1 * This example program shows how to apply a general GMM 2 * classification to distinguish citrus fruits using the 3 * features ‘area‘ and ‘circularity‘. Additionally, the 4 * 2D feature space for the extracted fruits is visualized. 5 * 6 read_image (Image, ‘color/citrus_fruits_01‘) 7 get_image_pointer1 (Image, Pointer, Type, Width, Height) 8 dev_close_window () 9 dev_open_window (0, 0, Width, Height, ‘white‘, WindowHandle) 10 set_display_font (WindowHandle, 12, ‘courier‘, ‘true‘, ‘false‘) 11 dev_set_draw (‘margin‘) 12 dev_set_line_width (2) 13 dev_display (Image) 14 dev_update_window (‘off‘) 15 dev_update_pc (‘off‘) 16 dev_update_var (‘off‘) 17 * 18 FeaturesArea := [] 19 FeaturesCircularity := [] 20 ClassName := [‘orange‘,‘lemon‘] 21 * 22 * Create a GMM classifier 23 create_class_gmm (2, 2, 1, ‘spherical‘, ‘normalization‘, 10, 42, GMMHandle) 24 * 25 * Add training samples 26 for i := 1 to 4 by 1 27 read_image (Image, ‘color/citrus_fruits_‘ + i$‘.2d‘) 28 dev_display (Image) 29 * ‘Add Samples‘ 30 get_regions (Image, SelectedRegions) 31 dev_display (SelectedRegions) 32 count_obj (SelectedRegions, NumberObjects) 33 for j := 1 to NumberObjects by 1 34 select_obj (SelectedRegions, ObjectSelected, j) 35 get_features (ObjectSelected, WindowHandle, Circularity, Area, RowRegionCenter, ColumnRegionCenter) 36 FeaturesArea := [FeaturesArea,Area] 37 FeaturesCircularity := [FeaturesCircularity,Circularity] 38 FeatureVector := real([Circularity,Area]) 39 if (i <= 2) 40 add_sample_class_gmm (GMMHandle, FeatureVector, 0, 0) 41 disp_message (WindowHandle, ‘Add to Class:‘ + ClassName[0], ‘window‘, RowRegionCenter, ColumnRegionCenter - 100, ‘black‘, ‘true‘) 42 else 43 add_sample_class_gmm (GMMHandle, FeatureVector, 1, 0) 44 disp_message (WindowHandle, ‘Add to Class:‘ + ClassName[1], ‘window‘, RowRegionCenter, ColumnRegionCenter - 100, ‘black‘, ‘true‘) 45 endif 46 endfor 47 disp_continue_message (WindowHandle, ‘black‘, ‘true‘) 48 stop () 49 endfor 50 dev_clear_window () 51 * 52 * Visualize the feature space 53 visualize_2D_feature_space (Cross, Height, Width, WindowHandle, FeaturesArea[0:5], FeaturesCircularity[0:5], ‘dim gray‘, 18) 54 * ‘oranges‘, 40, 440 55 visualize_2D_feature_space (Cross, Height, Width, WindowHandle, FeaturesArea[6:11], FeaturesCircularity[6:11], ‘light gray‘, 18) 56 * ‘lemons‘, 70, 440 57 disp_continue_message (WindowHandle, ‘black‘, ‘true‘) 58 stop () 59 * 60 * Train the classifier 61 train_class_gmm (GMMHandle, 100, 0.001, ‘training‘, 0.0001, Centers, Iter) 62 * 63 * Classify 64 for i := 1 to 15 by 1 65 read_image (Image, ‘color/citrus_fruits_‘ + i$‘.2d‘) 66 dev_display (Image) 67 * ‘Classify Image‘, 10, 10 68 get_regions (Image, SelectedRegions) 69 dev_display (SelectedRegions) 70 count_obj (SelectedRegions, NumberObjects) 71 for j := 1 to NumberObjects by 1 72 select_obj (SelectedRegions, ObjectSelected, j) 73 get_features (ObjectSelected, WindowHandle, Circularity, Area, RowRegionCenter, ColumnRegionCenter) 74 FeaturesArea := [FeaturesArea,Area] 75 FeaturesCircularity := [FeaturesCircularity,Circularity] 76 FeatureVector := real([Circularity,Area]) 77 classify_class_gmm (GMMHandle, FeatureVector, 1, ClassID, ClassProb, Density, KSigmaProb) 78 disp_message (WindowHandle, ‘Class: ‘ + ClassName[ClassID], ‘window‘, RowRegionCenter, ColumnRegionCenter - 100, ‘black‘, ‘true‘) 79 disp_message (WindowHandle, ‘KSigmaProb: ‘ + KSigmaProb, ‘window‘, RowRegionCenter + 30, ColumnRegionCenter - 100, ‘black‘, ‘true‘) 80 endfor 81 if (i != 15) 82 disp_continue_message (WindowHandle, ‘black‘, ‘true‘) 83 endif 84 stop () 85 endfor 86 * 87 * Clear the classifier from memory 88 clear_class_gmm (GMMHandle)
【《zw版·Halcon-delphi系列原创教程》,网址,cnblogs.com/ziwang/】