网淘巴来吧,欢迎您!

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

模型纹理检查

2022-11-03 新闻来源:网淘巴 围观:247
''

这个例子利用纹理检测模型检测纹理缺陷。

这个示例程序演示了如何进行微调

模型纹理检查

*纹理检测模型[文]

*首先,创建和[章]训练纹理检测模[来]

*使用一组无错[自]误的纹理图像。[网]

*接下来,对其[淘]中一个有缺陷的[巴]参数进行微调

*测试图像。

*在最后一步,[文]收集有缺陷的和[章]没有缺陷的

*图像被测试。[来]

*初始化

dev_update_off ()
dev_close_window ()
read_image (Image, 'carpet/carpet_01')
get_image_size (Image, Width, Height)
NumImages := 7

*定义训练图像[自]集。

Trainin[网]gImageI[淘]ndices := [1,2]

*训练图像指数

Texture[巴]ModelFi[文]lename := 'texture[章]_model_[来]carpet'

*纹理模型文件

* 初始可视化.
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle1)
get_window_extents (WindowHandle1, Row, Column, WindowWidth, WindowHeight)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (2)

Message := '这个示例程序演示了如何微调'
Message[1] := '用于缺陷检测的纹理检测模型:'
Message[2] := ' '
Message[3] := '(1)创建并训练纹理检查模型
Message[4] := '使用无错误纹理的图像'
Message[5] := ' '
Message[6] := ''(2)对其中一个测试的参数进行了微调'
Message[7] := '图像。'
Message[8] := ' '
Message[9] := '(3)检测缺陷图像和非缺陷图像'
Message[10] := ' '
Message[11] := '要完全理解此程序,请阅读'
Message[12] := '源代码中的注释。'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()

*首先我们尝试[自]读取纹理检测模[网]

*(仅当示例之[淘]前运行过时有效[巴])

dev_clear_window ()

file_exists (TextureModelFilename + '.htim', FileExists)

if (FileEx[文]ists)

    read_texture_inspection_model (TextureModelFilename, TextureInspectionModel)

    get_texture_inspection_model_param (TextureInspectionModel, 'levels', Levels)

    Message[章] := '读取之前训练的[来]纹理检测模型.[自]'

    Message[网][1] := ' '

else

    * 训练一个新的纹[淘]理检查模型.

    * [巴]

    * 创建内部数据结[文]构.

    create_texture_inspection_model ('basic', TextureInspectionModel)

    * [章]

    * 阅读并添加样本[来]图像进行训练.[自]

    for Index := 0 to |Traini[网]ngImage[淘]Indices[巴]| - 1 by 1

        read_image (Image, 'carpet/carpet_' + TrainingImageIndices[Index]$'02')

        dev_display (Image)

        Message[文] := '添加图片 ' + (Index + 1) + ' of ' + |Traini[章]ngImage[来]Indices[自]| + '到训练集.'

        dev_disp_text (Message, 'window', 12, 12, 'black', [], [])

        * 使用整个图像作[网]为纹理样本

        *在不同的应用[淘]程序中,你可能[巴]想先缩小图像域[文]。.

        add_texture_inspection_model_image (Image, TextureInspectionModel, Indices)

        dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])

        stop ()

    endfor

    * [章]

    * 训练可能需要一[来]段时间,特别是[自]当使用大图像时[网].

    dev_display (Image)

    Message[淘][0] := '训练纹理检测模[巴]型.'

    Message[文][1] := ' '

    Message[章][2] := '这可能需要一分[来]钟 (看'

    Message[自][3] := '进度的状态栏)[网].'

    dev_disp_text (Message, 'window', 12, 12, 'black', [], [])

    * [淘]

    * 定义训练参数.[巴]

    * 将归一化设置为[文][章]weber&r[来]dquo;,以[自]允许处理不同亮[网]度的图像.

    set_texture_inspection_model_param (TextureInspectionModel, 'patch_normalization', 'weber')

    * [淘]

    * 高分辨率的图像[巴]会导致较长的训[文]练和执行时间.[章]

    * 在许多情况下,[来]在较低的分辨率[自]下可以检测到缺[网]

    * (对应于更高的[淘]金字塔层次),[巴]也.

    Levels := [2,3,4][文]

    set_texture_inspection_model_param (TextureInspectionModel, 'levels', Levels)

    * [章]

    * 开始训练.

    train_texture_inspection_model (TextureInspectionModel)

    * [来]

    * 将纹理模型写入[自]磁盘

    write_texture_inspection_model (TextureInspectionModel, TextureModelFilename)

    * [网]

    Message[淘] := '训练很成功.'

    Message[巴][1] := ' '

endif

[文]

Message[章][2] := '我们现在可以通[来]过评估测试图像[自]上的'

Message[网][3] := ' 新颖性检测结果[淘]来微调参数'

dev_disp_text (Message, 'window', 12, 12, 'black', [], [])

dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])

stop()

[巴]

*我们得到了新[文]颖性阈值,以便[章]在测试阶段显示[来]它们.

* 新阈值定义了每[自]一个金字塔级别[网],在这一点上区[淘]分有缺陷和无缺[巴]陷的纹理

* 较低的阈值会导[文]致更多的错误检[章]测,但也可能导[来]致更多的误报([自]无缺陷的纹理区[网]域作为缺陷返回[淘])

get_texture_inspection_model_param (TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)

[巴]

*调试产生的信[文]息在apply[章]_textur[来]e_inspe[自]ction_m[网]odel执行期[淘]间存储了。

* 注意,必须使用[巴]clear_t[文]exture_[章]inspect[来]ion_res[自]ult释放re[网]sult句柄。[淘]

 

set_texture_inspection_model_param (TextureInspectionModel, 'gen_result_handle', 'true')

[巴]

dev_close_window ()

WindowW[文]idth := 320

WindowH[章]eight := 280

dev_open_window (0, 0, WindowWidth, WindowHeight, 'black', WindowHandle1)

set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')

dev_open_window (0, WindowWidth + 8, WindowWidth, WindowHeight, 'black', WindowHandle2)

set_display_font(WindowHandle2, 16, 'mono', 'true', 'false')

dev_open_window (0, 2 * WindowWidth + 16, WindowWidth, WindowHeight, 'black', WindowHandle3)

set_display_font(WindowHandle3, 16, 'mono', 'true', 'false')

dev_open_window (WindowHeight + 50, WindowWidth / 2 + 8, 2 * WindowWidth, 2 * WindowHeight, 'black', WindowHandle4)

set_display_font(WindowHandle4, 16, 'mono', 'true', 'false')

WindowH[来]andles := [Window[自]Handle1[网],Window[淘]Handle2[巴],Window[文]Handle3[章]]

[来]

* 检测第一个训练[自]图像的纹理缺陷[网],以微调参数.[淘]

for Index := 1 to 3 by 1

    ImageIn[巴]dex := 5

    read_image (TestImage, 'carpet/carpet_' + ImageIndex$'02')

    * [文]

    * 检查当前图像.[章]

    apply_texture_inspection_model (TestImage, NoveltyRegion, TextureInspectionModel, TextureInspectionResultID)

    * 检查调试信息.[来]

    * 新颖性得分图像[自]可用于单独微调[网]新颖性阈值。.[淘]

    get_texture_inspection_result_object (NovScoreImage, TextureInspectionResultID, 'novelty_score_image')

    get_texture_inspection_result_object (NovRegion, TextureInspectionResultID, 'novelty_region')

    * 清理结果句柄

    clear_texture_inspection_result (TextureInspectionResultID)

    * [巴]

    * 显示单个尺度的[文]结果

    count_o[章]bj (NovSco[来]reImage[自], Number)[网]

    for Level := 1 to Number by 1

        Current[淘]Window := WindowH[巴]andles[[文]Level - 1]

        dev_set_window (CurrentWindow)

        dev_clear_window ()

        select_obj (NovScoreImage, NovScoreImageL, Level)

        select_obj (NovRegion, NovRegionL, Level)

        get_image_size (NovScoreImageL, Width, Height)

        dev_set_part (0, 0, Height - 1, Width - 1)

        dev_display (NovScoreImageL)

        Legend := 'Novelty[章] region (level ' + Levels[[来]Level - 1] + ')'

        dev_set_color ('red')

        dev_set_line_width (2)

        * [自]

        dev_display (NovRegionL)

        dev_disp_text (['Novelty score image (level ' + Levels[Level - 1] + ')','Novelty threshold: ' + NoveltyThreshold[Level - 1]$'.1f'], 'window', 12, 12, 'black', [], [])

        dev_disp_text (Legend, 'window', WindowHeight - 30, 12, 'white', ['box_color','shadow'], ['black','false'])

        * 注意,appl[网]y_textu[淘]re_insp[巴]ection_[文]model返回[章]的Novelt[来]yRegion[自]是通过组合各个[网]金字塔层的各个[淘]新奇区域而派生[巴]的.

    endfor

    * [文]

    * 显示结果

    dev_set_window (WindowHandle4)

    dev_display (TestImage)

    dev_set_line_width (2)

    dev_set_color ('red')

    dev_display (NoveltyRegion)

    area_center (NoveltyRegion, Area, Row, Column)

    if (Index < 3)

        dev_disp_text ('Result', 'window', 12, 12, 'black', [], [])

    else

        dev_disp_text ('Final result', 'window', 12, 12, 'black', [], [])

    endif

    * [章]

    * 对于第一个图像[来],我们微调参数[自],以确保最佳参[网]数设置

    if (Index == 1)

        Message[淘][0] := 'There are many small errors in the image.'

        Message[巴][1] := 'It is possibl[文]e to adapt the sensiti[章]vity by alterin[来]g'

        Message[自][2] := 'the novelty[网] thresho[淘]lds.'

        Message[巴][3] := 'For example[文], this can be done by decreas[章]ing the'

        Message[来][4] := 'value of the \'sensiti[自]vity\' paramet[网]er'

        dev_disp_text (Message, 'window', 12, 12, 'black', [], [])

        * 微调新颖性阈值[淘]

        * [巴]

        * 设置阈值计算的[文]灵敏度。负值导[章]致更高的阈值,[来]因此检测到的缺[自]陷更少。

        set_texture_inspection_model_param  (TextureInspectionModel, 'sensitivity', -10)

        get_texture_inspection_model_param  (TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)

    endif

    * [网]

    if (Index == 2)

        Message[淘] := 'It is also possibl[巴]e to adapt the sensiti[文]vity'

        Message[章][1] := 'of the single levels individ[来]ually by manipul[自]ating'

        Message[网][2] := 'the novelty[淘] boundar[巴]ies directl[文]y.'

        dev_disp_text (Message, 'window', 12, 12, 'black', [], [])

        * 微调新颖性阈值[章].

        * [来]

        * 从纹理检测模型[自]中获取(自动确[网]定的)新颖性阈[淘]值,并设置适当[巴]修改值作为新颖[文]性阈值。

        * [章]

        * 如果我们明确地[来]设定了新颖性的[自]界限,敏感性就[网]被忽略了。

        * 为了避免混淆,[淘]我们把它设回0[巴]

        set_texture_inspection_model_param  (TextureInspectionModel, 'sensitivity', 0)

        * [文]

        Offset := [25,10,[章]30]

        get_texture_inspection_model_param(TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)

        set_texture_inspection_model_param  (TextureInspectionModel, 'novelty_threshold', Offset + NoveltyThreshold)

        get_texture_inspection_model_param(TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)

    endif

    * [来]

    dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])

    stop ()

endfor

[自]

for Level := 1 to |Window[网]Handles[淘]| by 1

    dev_set_window (WindowHandles[Level - 1])

    dev_clear_window ()

endfor

dev_set_window (WindowHandle4)

dev_clear_window  ()

Message[巴] := 'With the fine-tu[文]ned paramet[章]ers we can now'

Message[来][1] := 'detect defects[自] in all trainin[网]g and test images.[淘]..'

dev_disp_text (Message, 'window', 12, 12, 'black', [], [])

dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])

stop  ()

[巴]

* 在所有测试图像[文]上检测纹理缺陷[章]

for Index := 1 to NumImag[来]es by 1

    read_image (TestImage, 'carpet/carpet_' + Index$'02')

    * [自]

    * 检查当前图像

    apply_t[网]exture_[淘]inspect[巴]ion_mod[文]el (TestIm[章]age, Novelty[来]Region,[自] Texture[网]Inspect[淘]ionMode[巴]l, Texture[文]Inspect[章]ionResu[来]ltID)

    * 检查调试信息

    get_tex[自]ture_in[网]spectio[淘]n_resul[巴]t_objec[文]t (NovSco[章]reImage[来], Texture[自]Inspect[网]ionResu[淘]ltID, 'novelty[巴]_score_[文]image')

    get_tex[章]ture_in[来]spectio[自]n_resul[网]t_objec[淘]t (NovReg[巴]ion, Texture[文]Inspect[章]ionResu[来]ltID, 'novelty[自]_region[网]')

    * 清理结果句柄

    clear_t[淘]exture_[巴]inspect[文]ion_res[章]ult (Textur[来]eInspec[自]tionRes[网]ultID)

    * [淘]

    * 显示单个尺度的[巴]结果

    count_obj (NovScoreImage, Number)

    for Level := 1 to Number by 1

        Current[文]Window := WindowH[章]andles[[来]Level - 1]

        dev_set_window (CurrentWindow)

        dev_clear_window()

        select_obj (NovScoreImage, NovScoreImageL, Level)

        select_obj (NovRegion, NovRegionL, Level)

        get_image_size (NovScoreImageL, Width, Height)

        dev_set_part (0, 0, Height - 1, Width - 1)

        dev_display (NovScoreImageL)

        Legend := 'Novelty[自] region (level ' + Levels[[网]Level - 1] + ')'

        dev_set_color ('red')

        dev_set_line_width (2)

        * [淘]

        dev_display (NovRegionL)

        dev_disp_text (['Novelty score image (level ' + Levels[Level - 1] + ')','Novelty threshold: ' + NoveltyThreshold[Level - 1]$'.1f'], 'window', 12, 12, 'black', [], [])

        dev_disp_text (Legend, 'window', WindowHeight - 50, 12, ['red','white'], ['box_color','shadow'], ['black','false'])

        * 请注意,app[巴]ly_text[文]ure_ins[章]pection[来]_model返[自]回的Novel[网]tyRegio[淘]n是通过组合各[巴]个金字塔层次的[文]各个新奇区域派[章]生出来的。

    endfor

    * [来]

    * 显示结果

    dev_set_window (WindowHandle4)

    dev_display (TestImage)

    dev_set_line_width (2)

    dev_set_color ('red')

    dev_display (NoveltyRegion)

    area_center (NoveltyRegion, Area, Row, Column)

    if (Area > 100)

        dev_disp_text ('Not OK', 'window', 12, 12, 'white', 'box_color', 'red')

    else

        dev_disp_text ('OK', 'window', 12, 12, 'white', 'box_color', 'forest green')

    endif

    * [自]

    if (Index < NumImag[网]es)

        dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])

        stop()

    endif

endfor

stop()

[淘]

* 清理内存

clear_texture_inspection_model (TextureInspectionModel)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

分享到:

相关文章

龙年
大发