检测-纹理表面凸起、凹痕、划痕缺陷的检测(光度立体算子)

此示例是一个综合的示例,检测的是皮革纹理表面上出现的凸起、凹痕、划痕上的缺陷。使用的依然是光度立体法,只是不同的缺陷,需要使用的是不同参数所生成的图像。


示例代码如下:

* 使用光度立体的方法检测皮革样品
 
* Initialization
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
Message := 'Inspect leather samples using photometric stereo'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Part 1
* 
* Show input images with different illumination
* 先显示并且输入同一图像在不同光照下的图像
* 此缺陷检测,检测的是纹理凸起缺陷
 
* 1. 一次性读入多个图像
read_image (Images, 'photometric_stereo/leather_1_0' + [1:4])
* 循环,一次选择一张图像
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
* 
* 
* Apply photometric stereo to determine the albedo
Tilts := [6.1,95.0,-176.1,-86.8]
Slants := [41.4,42.6,41.7,40.9]
ResultType := ['gradient','albedo']
* 2. 用该算子得到反照率图像和表面梯度图像
photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])
* 
* Display the albedo image
* 显示反照率图像
dev_display (Albedo)
disp_message (WindowHandle, 'The defect is clearly visible in the albedo image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Detect defects
* 3. 缺陷检测
* 
* 该二值化处理有几个处理:均值处理->使用标准差进行二值化处理
var_threshold (Albedo, Region, 15, 15, 0.2, 0.05, '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))
* Display the defects
dev_display (Albedo)
dev_set_color ('red')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Circle)
disp_message (WindowHandle, 'Albedo image with defect', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Part 2
* 
* Show input images with different illumination
* 读入图像,一次读入多张在不同光照下的图像
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)
endfor
* 
* 
* Apply photometric stereo to determine the albedo
* 用该算子得到反照率图像和表面梯度图像
photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])
 
* 二值化
threshold (Albedo, Region1, 128, 255)
* 
* Display the albedo image
* 仅仅只使用二值化的方法处理反照率图像,并不能检测到缺陷区域
dev_display (Albedo)
Message := 'These defects are difficult to detect in the albedo image.'
Message[1] := 'Therefore, we use the gradient information to detect them.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Detect texture defects using gradient information.
* We are looking for areas with little surface changes.
* 使用梯度信息检测纹理缺陷,寻找表面变化很小的区域
* 
* 获得高斯曲率图像
derivate_vector_field (Gradient, Curl, 1, 'curl')
derivate_gauss (Curl, CurlGradient, 1, 'gradient')
* 
* Display changes of the curl of the gradient field
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 ()
* 
* Detect texture defects
* 如下代码检测纹理缺陷
* 此处缺陷检测,检测的是纹理的凹痕
 
* 对高斯曲率图像进行二值化处理
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)
* 
* Display texture defects
* 在反照率图像上显示选择的区域
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 ()
* 
* Detect scratches using curvature information.
* We are looking for areas with high curvature
* 
* 使用曲率信息检测划痕,寻找高曲率区域
* 
* 获得高斯曲率图像
derivate_vector_field (Gradient, MeanCurvature, 1, 'mean_curvature')
* 
* Display the mean curvature of the surface
* 显示表面的平均曲率
dev_display (MeanCurvature)
Message := 'Mean curvature of the surface'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Press F5', 'image', 720, 850, 'black', 'true')
stop ()
* 
* Detect scratches
* 划痕检测
* 此处缺陷检测,检测的是划痕
 
* 计算绝对值图像  因为高斯曲率图像中曲率会有负数
abs_image (MeanCurvature, ImageAbs)
* 二值化处理
threshold (ImageAbs, Region2, 0.15, 255)
* 计算连通域,得到分隔开的连通域
connection (Region2, ConnectedRegions1)
* 以面积特征,特征直方图选择区域
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 10, 99999)
* 联合,联合成一个区域
union1 (SelectedRegions1, RegionUnion1)
* 闭运算
closing_circle (RegionUnion1, RegionClosing, 1.5)
* 计算连通域,得到分隔开的连通域
connection (RegionClosing, ConnectedRegions2)
* 以最大直径特征,特征直方图选择区域
select_shape (ConnectedRegions2, SelectedRegions2, 'max_diameter', 'and', 50, 99999)
* 以灰度值选择区域
select_gray (SelectedRegions2, MeanCurvature, SelectedRegions3, 'deviation', 'and', 0.2, 255)
* 
* Display scratches
* 在反照率图像显示抓痕
dev_display (Albedo)
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (3)
dev_display (SelectedRegions3)
disp_message (WindowHandle, 'Deep scratch', 'window', 12, 12, 'black', 'true')


重点说明:

1. 第一部分的检测,待检测的是皮革表面的凸起,该检测,直接使用的是反照率图像,进行预处理和Blob后,即可得到皮革凸起的缺陷。

2. 待检测的是皮革表面的凹痕和划痕,该检测,示例中先直接对反照率图像进行二值化之后,确认该方法无法完成对皮革表面缺陷的检测。

3. 第二部分的检测,待检测的是皮革表面的凹痕,先使用如下算子得到高斯曲率图像:

derivate_vector_field (Gradient, Curl, 1, 'curl')

derivate_gauss (Curl, CurlGradient, 1, 'gradient')

然后使用该高斯曲率图像Blob分析,即可得到皮革凹痕的缺陷。

4. 第三部分的检测,待检测的是皮革表面的划痕,先使用如下算子得到高斯曲率图像:

derivate_vector_field (Gradient, MeanCurvature, 1, 'mean_curvature')

然后使用该高斯曲率图像Blob分析,即可得到皮革划痕的缺陷。

5. 算子rank_region,使用一个Mask依次对待处理的整个区域过滤,选择需要的区域。

6. 算子select_gray,按照灰度值来选择区域。



执行流程:

凸起反照率图像如下:

image.png

在反照率图像中标记处凸起缺陷:

image.png


凹痕反照率图像:

image.png


凹痕高斯曲率图像:

image.png


在反照率图像中标记处凹痕:


image.png

划痕的高斯曲率图像:


image.png

在反照率图像中标记处划痕:

image.png

--------------------- 

作者:hackpig
来源:
www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!



本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864