1.首先简单介绍一下Halcon开发:

        HALCON是德国MVtec公司开发的一套完善的标准的机器视觉算法包,拥有应用广泛的机器视觉集成开发环境。它节约了产品成本,缩短了软件开发周期——HALCON灵活的架构便于机器视觉,医学图像和图像分析应用的快速开发。在欧洲以及日本的工业界已经是公认具有最佳效能的Machine Vision软件。

Halcon语句的分类:

  1. 绿色:注释
  2. 褐色:控制和开发算子
  3. 蓝色:图像获取和处理算子
  4. 浅蓝色:外部函数

     如图所示: 


halcon深度学习的轮胎缺陷检测 基于halcon的元件缺陷检测_图像处理

Halcon语句分类示例

Halcon图像处理思想

    1.获取图像
    2.预处理
    3.处理
    4.显示结果和清除object

图像的特征

1.颜色物征:边缘、频谱、色彩,角点
2.形态学特征:轮廓,形状
3.纹理物征
4.空间关系

例程:detect_indent_fft.hdev

* This program demonstrates how to detect small texture
* defects on the surface of plastic items by using the fast
* fourier transform (FFT).
* First, we construct a suitable filter using Gaussian
* filters. Then, the images and the filter are convolved
* by using fast fourier transforms. Finally, the defects
* are detected in the filtered images by using
* morphology operators.
* 
* Initializations
*/
*例程:detect_indent_fft.hdev

* 说明:这个程序展示了如何利用快速傅里叶变换(FFT)对塑料制品的表面进行目标(缺陷)的检测,大致分为三步:

* 首先,我们用高斯滤波器构造一个合适的滤波器(将原图通过高斯滤波器滤波);

* 然后,将原图和构造的滤波器进行快速傅里叶变换;

* 最后,利用形态学算子将缺陷表示在滤波后的图片上(在缺陷上画圈)。
*
*/

* 在程序执行过程中选择将PC更新操作打开或关闭
dev_update_off ()
* 关闭激活的图形显示窗口
dev_close_window ()
read_image (Image, 'plastics/plastics_01')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
* dev_set_draw (’fill’)    填满选择的区域
* dev_set_draw (’margin’)    显示的对象只有边缘线,
dev_set_draw ('margin')
* 线宽用Line Width 指定
dev_set_line_width (3)
* 指定颜色
dev_set_color ('red')
* 
* Optimize the fft speed for the specific image size
* 对指定大小的图片的fft速度进行优化
optimize_rft_speed (Width, Height, 'standard')
* 
* Construct a suitable filter by combining two gaussian
* filters
* 定义两个常量
Sigma1 := 10.0
Sigma2 := 3.0
* 构造两个高斯滤波器
gen_gauss_filter (GaussFilter1, Sigma1, Sigma1, 0.0, 'none', 'rft', Width, Height)
gen_gauss_filter (GaussFilter2, Sigma2, Sigma2, 0.0, 'none', 'rft', Width, Height)

* 两图片相减(灰度)
sub_image (GaussFilter1, GaussFilter2, Filter, 1.025, 0)
* sub_image(ImageMinuend, ImageSubtrahend : ImageSub : Mult, Add : )
* g' := (g1 - g2) * Mult + Add
* 以上为函数原型以及运算公式
* 
* Process the images iteratively
NumImages := 16
for Index := 1 to NumImages by 1
    * 
    * Read an image and convert it to gray values
    read_image (Image, 'plastics/plastics_' + Index$'02')
    * 把一个RGB图像转变成一个灰度图像。
    rgb1_to_gray (Image, Image)
    * Perform the convolution in the frequency domain
    * 计算一个图像的实值快速傅里叶变换。
    rft_generic (Image, ImageFFT, 'to_freq', 'none', 'complex', Width)
    * 用在频域内的滤波器使一个图像卷积。
    convol_fft (ImageFFT, Filter, ImageConvol)
    rft_generic (ImageConvol, ImageFiltered, 'from_freq', 'n', 'real', Width)
    * 
    * Process the filtered image
    * 用一个矩形掩膜计算像素点的灰度范围
    gray_range_rect (ImageFiltered, ImageResult, 10, 10)

    * 决定区域内最小最大灰度值
    intensity(ImageResult, ImageResult, MeanValue, Deviation)
    min_max_gray (ImageResult, ImageResult, 0, Min, Max, Range)
    * 利用全局阈值对图像进行分割
    * value := 10
    * if (Max > 6.8)
    *     value := 6.8
    * else
    *     value := Max * 0.8
    * endif
    threshold (ImageResult, RegionDynThresh, max([5.55, MeanValue + 4.25]), 255)
    * threshold (ImageResult, RegionDynThresh, max([5.55, Max * 0.8]), 255)

    * 计算区域内的连通部分
    connection (RegionDynThresh, ConnectedRegions)
    * 根据指定的形态特征选择区域
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1, 99999)
    * 返回包含所有区域的集合
    union1 (SelectedRegions, RegionUnion)
    * 用一个圆圈来封闭一个区域
    * 参数说明:将要被封闭的区域(RegionUnion)
    *          被封闭的区域(RegionClosing)
    *          圆圈的半径(10)
    closing_circle (RegionUnion, RegionClosing, 5)
    * 合并像素相连区成为一个Element
    connection (RegionClosing, ConnectedRegions1)
    select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 10, 99999)
    * 计算区域的面积以及中心位置
    area_center (SelectedRegions1, Area, Row, Column)
    * 
    * Display the results
    * 显示原图
    dev_display (Image)
    * 将区域面积赋给Number用于后面检查是否存在缺陷
    Number := |Area|  
    if (Number)
        * 构造一个与设定的圆弧或圆相一致的边界
        gen_circle_contour_xld (ContCircle, Row, Column, gen_tuple_const(Number,30), gen_tuple_const(Number,0), gen_tuple_const(Number,rad(360)), 'positive', 1)
        ResultMessage := ['Not OK',Number + ' defect(s) found']
        Color := ['red','black']
        dev_display (ContCircle)
    else
        ResultMessage := 'OK'
        Color := 'forest green'
    endif
    * 显示信息
    disp_message (WindowHandle, ResultMessage, 'window', 12, 12, Color, 'true')
    if (Index != NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

运行效果图:

halcon深度学习的轮胎缺陷检测 基于halcon的元件缺陷检测_halcon_02

 

halcon深度学习的轮胎缺陷检测 基于halcon的元件缺陷检测_图像处理_03

 

halcon深度学习的轮胎缺陷检测 基于halcon的元件缺陷检测_图像处理_04

halcon深度学习的轮胎缺陷检测 基于halcon的元件缺陷检测_halcon深度学习的轮胎缺陷检测_05