halcon缺陷检测例程
时间: 2023-09-02 16:08:59 浏览: 111
使用halcon进行瑕疵检测的一个例程.7z
以下是一个简单的Halcon缺陷检测例程:
1. 导入图像并进行预处理
```
read_image(Image, 'path/to/image')
gray_image(Image, ImageGray)
gauss_image(ImageGray, ImageGauss, 3)
```
2. 创建模板
```
read_image(Template, 'path/to/template')
gray_image(Template, TemplateGray)
threshold(TemplateGray, TemplateBinary, 128, 255)
```
3. 执行模板匹配
```
match_template(ImageGauss, TemplateBinary, 'use_polarity', 'true',
'metric', 'use_polarity', 'result', MatchResult)
```
4. 显示匹配结果
```
get_shape_model_contours(Template, TemplateContours, 1)
gen_cross_contour_xld(Cross, Row, Column, 10, 0)
tuple_length(MatchResult, NumMatches)
for (Index := 0 to NumMatches-1 by 1)
get_shape_model_instance_contours(ModelContours, MatchResult[Index],
Angle, Row, Column)
disp_image(Image, WindowHandle)
disp_xld(TemplateContours, WindowHandle, 'green')
disp_xld(ModelContours, WindowHandle, 'blue')
disp_xld(Cross, WindowHandle, 'red')
endfor
```
5. 检测缺陷
```
gen_region_contour(MatchResult, TemplateWidth, TemplateHeight,
Row, Column, ModelContours)
reduce_domain(Image, ModelContours, ImageReduced)
subtract_image(ImageGauss, ImageReduced, ImageDiff)
threshold(ImageDiff, DefectsBinary, 10, 255)
```
6. 显示缺陷
```
area_center(DefectsBinary, Area, Row, Column)
disp_image(Image, WindowHandle)
disp_region(Area, WindowHandle, 'red')
```
阅读全文