在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
《zw版·Halcon-delphi系列原创教程》 水果自动分类脚本(机器学习、人工智能) 前面介绍了超市,流水线,酸奶的自动分类算法,下面再介绍一个水果的自动分类算法。
Halcon强大的图像处理能力,令人往往会忽视其更加彪悍的机器学习、人工智能。 分类,聚类分析,是机器学习、人工智能的核心算法之一,也是个典型的应用。 Halcon内置的聚类分析、机器学习模块,就有:knn邻近算法、向量机SVM、GMM高斯混合模型(Gaussian Mixture Model,或者混合高斯模型,也可以简写为MOG(Mixture of Gaussian)、MLP(多层神经网络)等等。 目前国内、海外机器学习、人工智能方面的学者,没有几位重视这块。 Halcon因为面向一线生产线,所以很多机器学习、人工智能,都是黑箱式的,无需编程,直接调用,录入内置的ocr模块,可以识别99%的标准工业字符:超市、海关、流水线
这个脚本,AI方面不算复杂,建模就是先拍摄几张产品的照片,直接匹配。 脚本80多行,很简单。 选这个脚本,其中一个原因,是因为前几天,有人在论坛询问,如何对企业生产线的产品(零食好像?)进行自动分类。
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)
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论