标签:
《zw版·Halcon-delphi系列原创教程》
酸奶自动分类脚本(机器学习、人工智能)
Halcon强大的图像处理能力,令人往往会忽视其更加彪悍的机器学习、人工智能。
至少,目前国内、海外机器学习、人工智能方面的学者,没有几位重视这块。
国外,可能是版权问题,毕竟,Halcon是售价高达数万欧元(不是人民币)的商业软件,而且主要用于自控、机器视觉等工业领域,而不是大学。
国内,可能是对于Halcon的了解不够。
其实,图像处理的核心,图像识别、分类,都离不开机器学习、人工智能
大家看看opencv的发展路线,就可以清楚看到,从cv1.0的图像,到cv1.0的机器学习,以及目前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/】
《zw版·Halcon-delphi系列原创教程》 酸奶自动分类脚本(机器学习、人工智能)
标签:
原文地址:http://www.cnblogs.com/ziwang/p/4856523.html