Halcon 学习笔记九:mlp训练学习实战颜色识别
* This example demonstrates a completeness check of colored game * pieces using MLP classification. The training and application * of the classifier is first shown on colored images and then on * gray images. * dev_update_off () dev_close_window () dev_open_window (0, 0, 557, 416, 'black', WindowHandle) set_display_font (WindowHandle, 14, 'mono', 'true', 'false') dev_set_draw ('margin') * * Initialization ImageRootName := 'color/color_pieces_0' Regions := ['yellow','pink','blue','background'] Highlight := ['goldenrod','magenta','cyan'] gen_empty_obj (Classes) * * Train and apply the MLP classifier for Mode := 0 to 1 by 1 dev_set_color ('black') read_image (Image, ImageRootName + '0') * * Simulate gray image if (Mode == 1) rgb1_to_gray (Image, GrayImage) compose3 (GrayImage, GrayImage, GrayImage, Image) dev_display (Image) disp_message (WindowHandle, 'Train and apply the classes again on gray images', 'window', 12, 12, 'black', 'false') disp_continue_message (WindowHandle, 'black', 'true') stop () endif * * Colored images if (Mode == 0) * * Specify color classes for I := 1 to 4 by 1 dev_display (Image) dev_display (Classes) disp_message (WindowHandle, ['Drag rectangle inside ' + Regions[I - 1] + ' color','Click right mouse button to confirm'], 'window', 24, 12, 'black', 'false') draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2) gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) concat_obj (Classes, Rectangle, Classes) endfor endif * * Train the specified color classes create_class_mlp (3, 7, 4, 'softmax', 'normalization', 3, 42, MLPHandle) add_samples_image_class_mlp (Image, Classes, MLPHandle) disp_message (WindowHandle, 'Training...', 'window', 100, 12, 'black', 'false') train_class_mlp (MLPHandle, 400, 0.5, 0.01, Error, ErrorLog) * * Use the trained MLP classifier to test if each image * contains four game pieces of each color for J := 0 to 3 by 1 read_image (Image, ImageRootName + J) if (Mode == 1) rgb1_to_gray (Image, GrayImage) compose3 (GrayImage, GrayImage, GrayImage, Image) endif * * Apply the trained classes classify_image_class_mlp (Image, ClassRegions, MLPHandle, 0.5) dev_display (Image) disp_message (WindowHandle, 'Looking for 4 game pieces of each color ...', 'window', 24, 12, 'black', 'false') dev_set_line_width (2) * * Count the number of game pieces for each color class for Figure := 1 to 3 by 1 copy_obj (ClassRegions, ObjectsSelected, Figure, 1) connection (ObjectsSelected, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 400, 99999) count_obj (SelectedRegions, Number) dev_set_color (Highlight[Figure - 1]) dev_display (SelectedRegions) OutString := Regions[Figure - 1] + ': ' + Number + ' ' dev_set_color ('green') disp_message (WindowHandle, OutString, 'window', 24 + 30 * Figure, 12, 'black', 'false') if (Number != 4) disp_message (WindowHandle, 'Not OK', 'window', 24 + 30 * Figure, 120, 'red', 'false') else disp_message (WindowHandle, 'OK', 'window', 24 + 30 * Figure, 120, 'green', 'false') endif endfor if (J < 3 or Mode == 0) disp_continue_message (WindowHandle, 'black', 'true') stop () endif endfor clear_class_mlp (MLPHandle) endfor dev_clear_window () dev_display (Image) Message := 'The game pieces cannot be classified reliable on' Message[1] := 'gray images because the gray values of the' Message[2] := 'game pieces cannot always be distinguished from' Message[3] := 'the gray values of the background.' disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
后续可以尝试对下图进行训练和预测