Halcon——mlp分类

read_image (Image, 'E:     2018.12.26正面/1.bmp')
gen_rectangle1 (Rectangle, 1200, 150, 2400, 1900)
reduce_domain (Image, Rectangle, ImageReduced)
classes:=['正面','反面']
gen_features (ImageReduced, FeatureVector)
NumFeatures := |FeatureVector|
NumClasses := |classes|
NumHidden := 15
create_class_mlp (NumFeatures, NumHidden, NumClasses, 'softmax', 'normalization', 10, 42, MLPHandle)

*********训练分类正面
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('E:    2018.12.26正面', ['files','follow_links'], ImageFiles_front)
tuple_regexp_select (ImageFiles_front, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image_front, ImageFiles_front[Index])
    reduce_domain (Image_front, Rectangle, ImageReduced_front)
    gen_features (ImageReduced_front, FeatureVector_front)
    add_sample_class_mlp (MLPHandle, FeatureVector_front, 0)
    * Image Acquisition 01: Do something
    * dev_set_window_extents (0, 0, 256, 306)
endfor

* Image Acquisition 02: Code generated by Image Acquisition 02
list_files ('E:  反面', ['files','follow_links'], ImageFiles_reverse)
tuple_regexp_select (ImageFiles_reverse, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image_reverse, ImageFiles_reverse[Index])
    reduce_domain (Image_reverse, Rectangle, ImageReduced_reverse)
    gen_features (Image_reverse, FeatureVector_reverse)
    add_sample_class_mlp (MLPHandle, FeatureVector_reverse, 1)
*     dev_set_window_extents (0, 0, 340, 406)
    * Image Acquisition 02: Do something
endfor
stop ()
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)

* Image Acquisition 03: Code generated by Image Acquisition 03
list_files ('C:/Users/Desktop/正反面', ['files','follow_links'], ImageFiles_test)
tuple_regexp_select (ImageFiles_test, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image_test, ImageFiles_test[Index])
    reduce_domain (Image_test, Rectangle, ImageReduced_test)
    gen_features (ImageReduced_test, FeatureVector_test)
    classify_class_mlp (MLPHandle, FeatureVector_test, 2, FoundClassIDs, Confidence)
*     dev_set_window_extents (0, 0, 341, 406)
    if (FoundClassIDs[0]==0)
        dev_display(Image_test)
        disp_message (200000, '正面', 'window', 50, 50, 'green', 'false')   
    elseif (FoundClassIDs[0]==1)
        dev_display(Image_test)
        disp_message (200000, '反面', 'window', 50, 50, 'yellow', 'false')   
    endif
    stop ()
    
    * Image Acquisition 03: Do something
endfor


你可能感兴趣的:(Halcon)