halcon的这种纹理检测模型使用起来相当方便。只需要下面几个步骤:
创建纹理检测模型create_texture_inspection_model
读多张图,选择图片中的一片没纹理正常的ROI传给检测模型
设置训练参数 set_texture_inspection_model_param
开始训练 train_texture_inspection_model
读取要检测的图片,apply_texture_inspection_model进行检测,直接就能输出缺陷的region。
不亏是halcon,又整出一个傻瓜功能。
C#
dev_update_off ()
dev_close_window ()
read_image (Image, 'isolation_foam/isolation_foam_01')
get_image_size (Image, Width, Height)
NumImages := 6
* Define the set of training images.
TrainingImageIndices := [1,2]
TrainingROIs := {[20,70],[300,30],[500,250],[600,600]}
*
* Initialize the visualization.
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle)
get_window_extents (WindowHandle, Row, Column, WindowWidth, WindowHeight)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
*
Message := 'This example program demonstrates how to detect texture'
Message[1] := 'defects with the texture inspection model.'
Message[2] := ' '
Message[3] := 'First, a texture inspection model is created and trained'
Message[4] := 'using images with regions of error-free texture.'
Message[5] := 'Then, texture defects are detected in a set of'
Message[6] := 'test images (novelty detection).'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
* Create the internal data structures.
create_texture_inspection_model ('basic', TextureInspectionModel)
*
* Read and add sample images for training.
for Index := 0 to |TrainingImageIndices| - 1 by 1
read_image (Image, 'isolation_foam/isolation_foam_' + TrainingImageIndices[Index]$'02')
* Use only good parts of the image for training
gen_rectangle1 (Rectangle, TrainingROIs.at(0)[Index], TrainingROIs.at(1)[Index], TrainingROIs.at(2)[Index], TrainingROIs.at(3)[Index])
reduce_domain (Image, Rectangle, ImageReduced)
* Add the training image to the texture inspection model.
add_texture_inspection_model_image (ImageReduced, TextureInspectionModel, Indices)
dev_clear_window ()
dev_display (Image)
dev_set_line_width (4)
dev_set_draw ('margin')
dev_set_color ('green')
dev_display (Rectangle)
Message := 'Add region of error free texture (green)'
Message[1] := 'to the texture model'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
Message := 'Image ' + (Index + 1) + ' of ' + |TrainingImageIndices|
dev_disp_text (Message, 'window', 478, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endfor
*
* The training may take a while, especially when using large images.
dev_clear_window ()
Message[0] := 'Train the texture inspection model.'
Message[1] := ' '
Message[2] := 'This may take a minute (see the'
Message[3] := 'status bar for the progress).'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
*参数weber允许处理具有不同亮度的图像
*看来这个傻瓜功能的主要变化在这些参数上面,目前勇哥尚不清楚这些参数。
set_texture_inspection_model_param (TextureInspectionModel, 'patch_normalization', 'weber')
*这里如果使用高分辨图像会速度很慢。
*在许多情况下,可以在较低的分辨率上检测到缺陷(对应于更高的金字塔级别)
*因此这里设置金字塔层级2,3,4,5进行后面的训练和执行,就是为了提速
set_texture_inspection_model_param (TextureInspectionModel, 'levels', [2,3,4,5])
*
* Start the training.
train_texture_inspection_model (TextureInspectionModel)
*
* Detect texture defects on all test images.
dev_clear_window ()
Message := 'Next, the trained texture model can be applied'
Message[1] := 'to detect texture defects...'
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 NumImages by 1
read_image (TestImage, 'isolation_foam/isolation_foam_' + Index$'02')
*
* Inspect the current image.
apply_texture_inspection_model (TestImage, DefectCandidates, TextureInspectionModel, TextureInspectionResultID)
* Postprocessing
connection (DefectCandidates, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 600, Width * Height)
area_center (SelectedRegions, Area, Row, Column)
*
* Display results.
dev_display (TestImage)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (DefectCandidates)
dev_set_color ('red')
dev_set_line_width (4)
dev_display (SelectedRegions)
if (Area > 0)
disp_message (WindowHandle, 'Not OK', 'window', 12, 12, 'white', 'red')
disp_message (WindowHandle, ['Defect regions','Defect candidate regions'], 'window', WindowHeight - 80, 12, ['red','yellow'], ['black','false'])
else
disp_message (WindowHandle, 'OK', 'window', 12, 12, 'white', 'forest green')
endif
if (Index < NumImages)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


