检测-网孔是否正常的检测
程序员文章站
2022-06-01 15:53:54
...
该示例,检查的是网格网孔是否存在破损。
示例代码如下:
dev_update_window ('off')
* 读图像
read_image (Image, 'plastic_mesh/plastic_mesh_01')
dev_close_window ()
* 获取图像宽高
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowHandle)
set_display_font (WindowHandle, 18, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
* Each of the images is read and smoothed. Subsequently
* dyn_threshold is performed and connected regions are
* looked for. The parameter 'area' of the operator select_shape
* makes it possible to find regions that differ in the area
* size. Found errors are finally counted and displayed.
* 每一张图像都先被读然后做平滑处理,通常dyn_threshold被执行,
*
for J := 1 to 14 by 1
* 读图像
read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02')
* 平滑图像
mean_image (Image, ImageMean, 49, 49)
* 二值化处理
* dark :选择出来的是小于等于ImageMean-5
dyn_threshold (Image, ImageMean, RegionDynThresh, 5, 'dark')
* 分开,获得单个联通域,
connection (RegionDynThresh, ConnectedRegions)
* 按照面积特征,特征直方图选择区域
select_shape (ConnectedRegions, ErrorRegions, 'area', 'and', 500, 99999)
* 计算选择出来的区域个数
count_obj (ErrorRegions, NumErrors)
dev_display (Image)
dev_set_color ('red')
dev_display (ErrorRegions)
* If the number of errors exceeds zero, the message 'Mesh not
* OK' is displayed. Otherwise the web is undamaged
* and 'Mesh OK' is displayed.
* 如果选择的区域大于0,则说明存在缺陷区域
* 选择出来的区域,面积都是大于正常区域的
if (NumErrors > 0)
disp_message (WindowHandle, 'Mesh not OK', 'window', 24, 12, 'black', 'true')
else
disp_message (WindowHandle, 'Mesh OK', 'window', 24, 12, 'black', 'true')
endif
* If the sequence number of the image to be inspected is
* lower than 14, the request to press 'Run' to continue appears.
* If the last image is read, pressing 'Run' will clear the SVM.
if (J < 14)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
重点说明:
1. 在进行二值化之前,先使用算子mean_image进行图像平滑处理,去除噪声,让后续的二值化处理更简单。
2. 二值化处理使用的是dyn_threshold算子,该算子通过指定offset,分段获得需要的区域。
3. 通常mean_image算子和dyn_threshold会联合使用。
更多最新文章,请关注公众号:
执行流程:
待处理的OK的图像:
预处理+Blob后的结果:
显示处理结果:
待处理的Not OK的图像:
预处理+Blob后的结果:
显示处理结果: