Halcon 光度立体 缺陷检测

一、概述

halcon——缺陷检测常用方法总结(光度立体) - 唯有自己强大 - 博客园 (cnblogs.com)

        上周去了康耐视的新品发布会,我真的感觉压力山大,因为VM可以实现现在项目中的80% 的功能,感觉自己的不久就要失业了。同时康耐视开始布局工业的每个方面,生成一个完整的生态链,国内很喜欢搞某某一条龙服务。相机方面出了很多尤其是3D的,还有2.5D的相机,精度方面不好说,他们说Z方向的重复性精度可以达到0.15u,我们没有使用过,我对此保持怀疑,因为我用过的精度最高的是基恩士的的0.2u的,我尊重日本企业的严谨性和专业性,今天我看了一下那个halcon 的光度立体法,是2.5D 的。

光度立体法主要是表面检测的,在检测缺口、凹痕,适用于检测金属物料。在采集图片的时候最好是使用灰度图,至少是3张 最好是4张图像

光度立体的局限性:

  • 一方面假定相机是无畸变成像,也就是说必须使用远心镜头或者长焦镜头。
  • 另一方面假定每一个光源发射的光束都是平行且均匀的,也就是说必须使用具有均匀强度的远心照明光源,或者使用远距离的点光源代替。

此外,物体必须具有朗伯反射特性,即它必须以漫反射的方式反射入射光。有镜面反射的物体或者区域(镜子或者光滑的表面)不能使用此方法,会得到一个错误的结果

光照方向说明

安装说明:

    1、远心镜头必须与被测试的物体表面垂直按照,在采集多张图片的时候一定要保证相机和物体不动,对于采集至少三张的灰度图像,其每次取像的照明方向必须改变(相对于相机)。

    2、在采集的多张图像中的每付图像照明方向必定是制定Slants和Tilts两个参数角度

Slants:

表示光束方向与相机中轴线的夹角,一般是30-60度之间

Tilts:

Tilt角度通常都是均匀分布在被测物体周围,比如3个方向打光,Tilt角度应该是[0,120,240]OR[0,120,-120],4个方向打光是[0,90,180,-90]。需要注意的是,打光方向不能相同,否则重构的图像结果达不到预期效果

二、算子解释

  •  photometric_stereo (根据光度立体技术重建曲面)

*参数列表:
*Images(in)//输入灰度图像(4张)
*HeightField(out)//返回重建高度信息图
*Gradient(out)//返回表面的梯度信息图
*Albedo(out)//返回表面的反射率信息图
*Slant//光源光线与摄像机光轴的夹角
*Tilt//光源光线投影与被测物主轴的夹角
*ResultType//请求结果类型(高度场/梯度场/反射率)
*ReconstructionMethod//重建方法类型
*GenParamName//一般参数名称
*GenParamValue// 一般参数设置

                   *   重建高度信息图 表面的梯度信息图 表面的反射率信息图 光源光线与摄像机光轴的夹角  光源光线投影与被测物主轴的夹角  请求结果类型(高度场/梯度场/反射率)  重建方法类型(迫松重建)
photometric_stereo (Images, HeightField,    Gradient,      Albedo,          Slants,                  Tilts,                          ResultType,                     'poisson',          [],        [])
  • derivate_vector_field(处理photometric_stereo 函数输出的重建后的梯度、反射率、以及高度场信息图)

*derivate_vector_field    处理photometric_stereo 函数输出的重建后的梯度、反射率、以及高度场信息图  
*将向量场的分量与高斯函数的导数进行卷积,并计算由此得到的各种特征。
*在光度立体项目中,专门用于处理photometric_stereo 函数输出的重建后的梯度、反射率、以及高度场图像。
*参数列表:
*VectorField(in)// 梯度场图像
*Result(out)    // 返回平均曲率场图像
*Sigma(in)      // 高斯系数
                                 *如果在Sigma中传递一个值,那么在列和行方向上的平滑量是相同的。
                                 *如果在Sigma中传递两个值,第一个值指定列方向的平滑量,第二个值指定行方向的平滑量。
*Component(in)  //组件计算   
                                 *curl,向量场的旋度。旋度的一个应用是分析光流场。旋度是如果向量场是流体,小船会旋转多少。
                                 *divergence,向量场的散度。“divergence”的一个应用是分析光流场。打个比方,如果向量场是流体,散度就是源和汇的位置。
                                 *mean_curvature,当输入向量场 VectorField为梯度场时,下垫面的平均曲率H。用于处理photometric_stereo返回的向量场。
                                 *gauss_curvature,当输入向量场 VectorField 为梯度场时,下垫面的高斯曲率K。用于处理photometric_stereo返回的向量场。
                                 
                                 
                                    
derivate_vector_field (Gradient, Curl, 1, 'curl')

 

三、缺陷检测

反射率信息图

思路

1、不同角度采集4张灰度图,设置不同的角度参数 Slants   Tilts
2、photometric_stereo  ,返回不同的图 重建高度信息图 表面的梯度信息图 表面的反射率信息图
3、得到反射率信息图 然后利用blob 分析进行缺陷检测


*思路:
* 1、不同角度采集4张灰度图,设置不同的角度参数 Slants   Tilts
* 2、photometric_stereo  ,返回不同的图 重建高度信息图 表面的梯度信息图 表面的反射率信息图
* 3、得到反射率信息图 然后利用blob 分析进行缺陷检测


dev_close_window ()
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')


* Part 1利用反射率图像检测皮革表面缺陷
read_image (Images, 'photometric_stereo/leather_1_0' + [1:4])
write_image (Images, 'tiff', 0, 'D:/1.tiff')
** 展示不同方向光源成像图像
for I := 1 to 4 by 1
    Message := 'Sample 1: Acquire image ' + I + ' of 4'
    select_obj (Images, ObjectSelected, I)
    dev_display (ObjectSelected)
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
   * wait_seconds (0.5)
endfor
stop ()
* 应用光度立体法生成的反射率图进行缺陷检测
Tilts := [6.1,95.0,-176.1,-86.8]
Slants := [41.4,42.6,41.7,40.9]
              * 梯度场     反射率
ResultType := ['gradient','albedo']

*参数列表:
*Images(in)//输入灰度图像(4张)
*HeightField(out)//返回重建高度信息图
*Gradient(out)//返回表面的梯度信息图
*Albedo(out)//返回表面的反射率信息图
*Slant//光源光线与摄像机光轴的夹角
*Tilt//光源光线投影与被测物主轴的夹角
*ResultType//请求结果类型(高度场/梯度场/反射率)
*ReconstructionMethod//重建方法类型
*GenParamName//一般参数名称
*GenParamValue// 一般参数设置

                   *   重建高度信息图 表面的梯度信息图 表面的反射率信息图 光源光线与摄像机光轴的夹角  光源光线投影与被测物主轴的夹角  请求结果类型(高度场/梯度场/反射率)  重建方法类型(迫松重建)
photometric_stereo (Images, HeightField,    Gradient,      Albedo,          Slants,                  Tilts,                          ResultType,                     'poisson',          [],        [])

* 显示反射率图
dev_display (Albedo)

*检测缺陷   反射率的基础上进行缺陷检测
var_threshold (Albedo, Region, 15, 15, 0.4, 0.4, 'light')
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 99999)
union1 (SelectedRegions, RegionUnion)
closing_circle (RegionUnion, RegionClosing, 3.5)
connection (RegionClosing, Defects)
area_center (Defects, Area, Row, Column)
gen_circle (Circle, Row, Column, gen_tuple_const(|Row|,sqrt(Area) + 30))
*显示缺陷
dev_display (Albedo)
dev_set_color ('red')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Circle)




梯度信息图

思路

1、不同角度采集4张灰度图,设置不同的角度参数 Slants   Tilts
2、photometric_stereo  ,返回不同的图 重建高度信息图 表面的梯度信息图 表面的反射率信息图
3、得到梯度信息图然后利用blob 分析进行缺陷检测


*思路:
* 1、不同角度采集4张灰度图,设置不同的角度参数 Slants   Tilts
* 2、photometric_stereo  ,返回不同的图 重建高度信息图 表面的梯度信息图 表面的反射率信息图
* 3、得到梯度信息图然后利用blob 分析进行缺陷检测

* Part 2 利用梯度图像检测皮革表面缺陷
read_image (Images, 'photometric_stereo/leather_2_0' + [1:4])
for I := 1 to 4 by 1
    Message := 'Sample 2: Acquire image ' + I + ' of 4'
    select_obj (Images, ObjectSelected, I)
    dev_display (ObjectSelected)
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    *wait_seconds (0.5)
     stop()
endfor

* 应用光度立体法生成的反射率图
photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])
*对反射率图二值化(发现无法二值化)
threshold (Albedo, Region1, 128, 255)
* 显示反射率图
dev_display (Albedo)






*derivate_vector_field    处理photometric_stereo 函数输出的重建后的梯度、反射率、以及高度场信息图  
*将向量场的分量与高斯函数的导数进行卷积,并计算由此得到的各种特征。
*在光度立体项目中,专门用于处理photometric_stereo 函数输出的重建后的梯度、反射率、以及高度场图像。
*参数列表:
*VectorField(in)// 梯度场图像
*Result(out)    // 返回平均曲率场图像
*Sigma(in)      // 高斯系数
                                 *如果在Sigma中传递一个值,那么在列和行方向上的平滑量是相同的。
                                 *如果在Sigma中传递两个值,第一个值指定列方向的平滑量,第二个值指定行方向的平滑量。
*Component(in)  //组件计算   
                                 *curl,向量场的旋度。旋度的一个应用是分析光流场。旋度是如果向量场是流体,小船会旋转多少。
                                 *divergence,向量场的散度。“divergence”的一个应用是分析光流场。打个比方,如果向量场是流体,散度就是源和汇的位置。
                                 *mean_curvature,当输入向量场 VectorField为梯度场时,下垫面的平均曲率H。用于处理photometric_stereo返回的向量场。
                                 *gauss_curvature,当输入向量场 VectorField 为梯度场时,下垫面的高斯曲率K。用于处理photometric_stereo返回的向量场。
                                 
                                 
                                    
derivate_vector_field (Gradient, Curl, 1, 'curl')



*derivate_gauss.hdev
*derivate_gauss (Image, ImageGauss, 3, 'none')
* 将一个图像与高斯函数的导数进行卷积。  效果与FFT进行高斯滤波差不多。
*主要的区别是边界处理:FFT的定义假设信号是周期性的,因此边界处理是循环的延续。与此相反,derivate_gauss在图像边界使用灰度值的镜像。
*通过FFT进行过滤的速度已经快于在Sigma=3(排除创建过滤器的时间)中使用derivate_gauss。这种优势随着Simag的增大而变得更加明显。    
*'none' 仅使用平滑
*'x' 沿X的一阶导数
derivate_gauss (Curl, CurlGradient, 1, 'gradient')// 将一个图像与高斯函数的导数进行卷积。  效果与FFT进行高斯滤波差不多。
* 显示梯度图
dev_display (CurlGradient)
Message := 'Changes in the gradient curl'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

* 用梯度图寻找缺陷
threshold (CurlGradient, Region, 0, 0.01)
*
rank_region (Region, RegionCount, 10, 10, 30)//归类区域
connection (RegionCount, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 2000, 99999)
union1 (SelectedRegions, RegionUnion)
rank_region (RegionUnion, RegionCount1, 25, 25, 170)
connection (RegionCount1, NoTextured)

* 显示
dev_display (Albedo)
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (3)
dev_display (NoTextured)
disp_message (WindowHandle, 'Non-textured areas on leather', 'window', 12, 12, 'black', 'true')
stop ()

四、洗发水表面检测 

思路:

梯度信息图来检测

 inspect_shampoo_label_photometric_stereo.hdev



*思路 :
* inspect_shampoo_label_photometric_stereo.hdev
dev_close_window ()
dev_update_off ()
dev_open_window (0, 0, 640, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
Message := 'Inspect the label of a shampoo bottle'
Message[1] := 'using photometric stereo. In this case four'
Message[2] := 'different light orientations were used.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Show input images with different illumination
read_image (Images, 'photometric_stereo/shampoo_label_0' + [1:4])
for I := 1 to 4 by 1
    Message := 'Acquire image ' + I + ' of 4'
    select_obj (Images, ObjectSelected, I)
    dev_display (ObjectSelected)
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    wait_seconds (0.5)
endfor
* 
* Apply photometric stereo to determine the albedo
* and the surface gradient.
Tilts := [6.1,95.0,-176.1,-86.8]
Slants := [41.4,42.6,41.7,40.9]
ResultType := ['gradient','albedo']
photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])
* 
* Display the albedo image
dev_display (Albedo)
disp_message (WindowHandle, 'Albedo image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Calculate the gaussian curvature of the surface
* using the gradient field as input for the operator
* derivate_vector_field.
* Defects are usually easy to detect in the curvature image.
derivate_vector_field (Gradient, MeanCurvature, 1.0, 'mean_curvature')
* 
* Detect defects
* 
* Segment the tablet areas in the curvature image
threshold (MeanCurvature, Region, -10, -0.07)
opening_circle (Region, RegionOpening, 1)
connection (RegionOpening, ConnectedRegions)
select_shape (ConnectedRegions, Defects, 'area', 'and', 50, 99999)
shape_trans (Defects, Circle, 'outer_circle')
* Display the defects in curvature image
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (2)
dev_display (MeanCurvature)
dev_display (Circle)
Message := 'The defect can easily be detected'
Message[1] := 'in the surface curvature image'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
stop ()
* Display the defects in the albedo image
dev_set_draw ('margin')
dev_set_color ('red')
dev_display (Albedo)
dev_display (Circle)
disp_message (WindowHandle, 'Defect in albedo image', 'window', 12, 12, 'black', 'true')

我用反射率的图做了,是达不到这个效果的

五、药片缺陷检测

* Initialization
dev_close_window ()
dev_update_off ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
Message := 'Inspect the backside of a blister'
Message[1] := 'using photometric stereo. In this case four'
Message[2] := 'different light orientations were used.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Show input images with different illumination
read_image (Images, 'photometric_stereo/blister_back_0' + [1:4])
for I := 1 to 4 by 1
    Message := 'Acquire image ' + I + ' of 4'
    select_obj (Images, ObjectSelected, I)
    dev_display (ObjectSelected)
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    wait_seconds (0.5)
endfor
stop ()
* 
* Apply photometric stereo to determine the albedo
* and the surface gradient.
Tilts := [6.1,95.0,-176.1,-86.8]
Slants := [41.4,42.6,41.7,40.9]
ResultType := ['gradient','albedo']
photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])
* 
* Display the albedo image
dev_display (Albedo)
disp_message (WindowHandle, 'Albedo image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Calculate the gaussian curvature of the surface
* using the gradient field as input for the operator
* derivate_vector_field.
* Defects are usually easy to detect in the curvature image.
derivate_vector_field (Gradient, GaussCurvature, 1, 'gauss_curvature')
* 
* Detect defects
* 
* Segment the tablet areas in the curvature image
regiongrowing (GaussCurvature, Regions, 1, 1, 0.001, 250)
select_shape (Regions, TabletRegions, ['width','height'], 'and', [150,150], [200,200])
shape_trans (TabletRegions, TabletRegions, 'convex')
union1 (TabletRegions, TabletRegions)
erosion_circle (TabletRegions, TabletRegions, 3.5)
* Search for defects inside the tablet areas
reduce_domain (GaussCurvature, TabletRegions, ImageReduced)
abs_image (ImageReduced, ImageAbs)
threshold (ImageAbs, Region, 0.03, 255)
closing_circle (Region, RegionClosing, 10.5)
connection (RegionClosing, ConnectedRegions)
select_shape (ConnectedRegions, Defects, 'area', 'and', 10, 99999)
area_center (Defects, Area, Row, Column)
gen_circle (Circle, Row, Column, gen_tuple_const(|Row|,20.5))
* Display the defects in curvature image
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (2)
dev_display (GaussCurvature)
dev_display (Circle)
Message := 'The defect can easily be detected'
Message[1] := 'in the surface curvature image'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
stop ()
* Display the defects in the albedo image
dev_set_draw ('margin')
dev_set_color ('red')
dev_display (Albedo)
dev_display (Circle)
disp_message (WindowHandle, 'Defect in albedo image', 'window', 12, 12, 'black', 'true')

  • 18
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值