网淘巴来吧,欢迎您!

返回首页 微信
微信
手机版
手机版

根据木纹不一样判断不同的木材

2022-12-29 新闻来源:网淘巴 围观:279
''

*本示例的目的是根据木材的表面纹理对不同种类的木材进行分类

file_exists ('classify_wood.gmc', FileExists)//存在FileExists返回

if (FileEx[文]ists)

    USE_STO[章]RED_CLA[来]SSIFIER[自] := 1

else

    USE_STO[网]RED_CLA[淘]SSIFIER[巴] := 0

endif

* First, the path to the images is set, the initial[文] image

* is read and the setting[章]s are specifi[来]ed.

get_sys[自]tem ('image_d[网]ir', HalconI[淘]mages)

get_sys[巴]tem ('operati[文]ng_syst[章]em', OS)

if (OS{0:2[来]} == 'Win')

    tuple_s[自]plit (Halcon[网]Images,[淘] ';', HalconI[巴]mages)

else

    tuple_s[文]plit (Halcon[章]Images,[来] ':', HalconI[自]mages)

endif

ImagePa[网]th := '/wood/'

ReadOK := false

dev_get[淘]_prefer[巴]ences ('suppres[文]s_handl[章]ed_exce[来]ptions_[自]dlg', SaveMod[网]e)

dev_set[淘]_prefer[巴]ences ('suppres[文]s_handl[章]ed_exce[来]ptions_[自]dlg', 'true')

for k := 0 to |Halcon[网]Images|[淘] - 1 by 1

    try

        read_im[巴]age (Image,[文] HalconI[章]mages[k[来]] + ImagePa[自]th + 'apple/a[网]pple_01[淘]')

        ReadPat[巴]h := HalconI[文]mages[k[章]] + ImagePa[来]th

        ReadOK := true

        break

    catch (Except[自]ion)

    endtry

endfor

if (not ReadOK)[网]

    disp_me[淘]ssage (Window[巴]ID, 'Could not find the images in $HALCON[文]IMAGES', 'window', 12, 12, 'black', 'true')

    stop ()

endif

dev_set[章]_prefer[来]ences ('suppres[自]s_handl[网]ed_exce[淘]ptions_[巴]dlg', SaveMod[文]e)

read_im[章]age (Image,[来] ImagePa[自]th + 'apple/a[网]pple_01[淘]')

get_ima[巴]ge_size[文] (Image,[章] Width, Height)[来]

dev_clo[自]se_wind[网]ow ()

dev_ope[淘]n_windo[巴]w_fit_i[文]mage (Image,[章] 0, 0, Width, Height,[来] WindowI[自]D)

set_dis[网]play_fo[淘]nt (Window[巴]ID, 16, 'mono', 'true', 'false')

dev_dis[文]play (Image)[章]

dev_upd[来]ate_off[自] ()

* Now the differe[网]nt wood classes[淘] are specifi[巴]ed.

Classes[文] := ['apple','beech','cherry','maple','oak','walnut']

* The program[章] uses by default[来] a stored classif[自]ier. If you, however[网],

* want to perform[淘] the trainin[巴]g, set USE_STO[文]RED_CLA[章]SSIFIER[来] to 0.

* If the classif[自]ier can not be found, USE_STO[网]RED_CLA[淘]SSIFIER[巴]

* is set to 0 automat[文]ically.[章]

if (USE_ST[来]ORED_CL[自]ASSIFIE[网]R == 1)

    read_cl[淘]ass_mlp[巴] ('classif[文]y_wood.[章]gmc', MLPHand[来]le)

    NumClas[自]ses := |Classe[网]s|

else

    gen_fea[淘]tures (Image,[巴] Feature[文]Vector)[章]

    NumFeat[来]ures := |Featur[自]eVector[网]|

    NumClas[淘]ses := |Classe[巴]s|

    NumHidd[文]en := 15

    create_[章]class_m[来]lp (NumFea[自]tures, NumHidd[网]en, NumClas[淘]ses, 'softmax[巴]', 'normali[文]zation', 10, 42, MLPHand[章]le)

    for Correct[来]ClassID[自] := 0 to NumClas[网]ses - 1 by 1

        list_fi[淘]les (ReadPa[巴]th + Classes[文][Correc[章]tClassI[来]D], 'files', Files)

        for k := 0 to |Files|[自] - 1 by 2

            read_im[网]age (Image,[淘] Files[k[巴]])

            dev_dis[文]play (Image)[章]

            gen_fea[来]tures (Image,[自] Feature[网]Vector)[淘]

            add_sam[巴]ple_cla[文]ss_mlp (MLPHan[章]dle, Feature[来]Vector,[自] Correct[网]ClassID[淘])

        endfor

    endfor

    train_c[巴]lass_ml[文]p (MLPHan[章]dle, 200, 1, 0.0001,[来] Error, ErrorLo[自]g)

    write_c[网]lass_ml[淘]p (MLPHan[巴]dle, 'classif[文]y_wood.[章]gmc')

    disp_me[来]ssage (Window[自]ID, 'Trainin[网]g of wood texture[淘]s complet[巴]ed\nPre[文]ss \'Run\' to continu[章]e', 'window', 12, 12, 'black', 'true')

    stop ()

endif

Errors := 0

Count := 0

for Correct[来]ClassID[自] := 0 to NumClas[网]ses - 1 by 1

    list_fi[淘]les (ReadPa[巴]th + Classes[文][Correc[章]tClassI[来]D], 'files', Files)

    for k := 0 to |Files|[自] - 1 by 1

        Count := Count + 1

        read_im[网]age (Image,[淘] Files[k[巴]])

        gen_fea[文]tures (Image,[章] Feature[来]Vector)[自]

        classif[网]y_class[淘]_mlp (MLPHan[巴]dle, Feature[文]Vector,[章] 2, FoundCl[来]assIDs,[自] Confide[网]nce)

        dev_dis[淘]play (Image)[巴]

        dev_set[文]_color ('blue')

        disp_me[章]ssage (Window[来]ID, 'correct[自] class: ' + Classes[网][Correc[淘]tClassI[巴]D], 'window', 12, 12, 'black', 'true')

        if (Correc[文]tClassI[章]D == FoundCl[来]assIDs[[自]0])

            disp_me[网]ssage (Window[淘]ID, 'found class:&[巴]nbsp;  ' + Classes[文][FoundC[章]lassIDs[来][0]], 'window', 42, 12, 'forest green', 'true')

        else

            Errors := Errors + 1

            disp_me[自]ssage (Window[网]ID, 'found class:&[淘]nbsp;  ' + Classes[巴][FoundC[文]lassIDs[章][0]], 'window', 42, 12, 'red', 'true')

            disp_co[来]ntinue_[自]message[网] (Window[淘]ID, 'black', 'true')

            stop ()

        endif

        wait_se[巴]conds (0.1)

    endfor

endfor

ErrorRa[文]te := real(Er[章]rors) / Count * 100.0

disp_me[来]ssage (Window[自]ID, 'ErrorRa[网]te = ' + ErrorRa[淘]te + '%', 'window', 72, 12, 'black', 'true')

disp_en[巴]d_of_pr[文]ogram_m[章]essage (Window[来]ID, 'black', 'true')

clear_c[自]lass_ml[网]p (MLPHan[淘]dle)

 

 

免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏

本文链接:https://www.wtao8.com/post/276.html 转载需授权!

分享到:

相关文章

龙年
大发