Halcon 光度立体法应用——皮革表面缺陷检测
如果想深刻、系列的了解光度立体法,建议根据博客顺序观看。在这个例程中将会介绍通过光度立体法生成的图像适用场景。
总体代码注释说明
* 此例程介绍的是利用光度立体法对皮革样品表面进行缺陷检测。
*
* 程序初始化
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 ()
*
* 第一部分,利用反射率图像检测皮革表面缺陷
*
* 展示不同方向光源成像图像
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
*
*
* 应用光度立体法生成的反射率图进行缺陷检测
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', [], [])
*
* 显示反射率图
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 ()
*
* 检测缺陷
*
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)
disp_message (WindowHandle, 'Albedo image with defect', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* 第二部分区别第一部分缺陷类型,使用不同图像检测。
*
* 展示不同方向光源成像图像
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
*
*
* 根据光度立体法生成反射率图(反射率图无法检测)
photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])
threshold (Albedo, Region1, 128, 255)
*
* 显示反射率图
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 ()
*
* 利用梯度信息检测纹理表面上小的突变区域缺陷。
*
derivate_vector_field (Gradient, Curl, 1, 'curl')
derivate_gauss (Curl, CurlGradient, 1, 'gradient')
*
* 以上对梯度信息图进行高斯导数卷积操作并显示处理后图像
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 ()
*
* 利用曲率信息检测划痕,根据划痕表征的高曲率特性检测划痕。
*
derivate_vector_field (Gradient, MeanCurvature, 1, 'mean_curvature')
*
* 显示表面平均曲率图
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 ()
*
* 检测划痕
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)
*
* 显示划痕
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')
代码解析:
Part1利用反射率图检测皮革表面缺陷,而Part2却利用梯度信息图检测缺陷,Why?
仔细观察不难发现,Part1中的缺陷区域除了第一幅图(右边打光)之外其他展现的都是高亮特性。缺陷特征较背景区域具有较高的反光特性,所以反射率图能很好的凸显缺陷特征,所以用反射率图检测缺陷。
好奇的同志却在想,为啥不用梯度图呢?有想法是好的,那么假设我们用梯度信息图检测,效果会怎样?梯度信息图如下:
可以看出缺陷和纹理对比度很差,所以不能用梯度信息图检测Part1中的缺陷
Part2中的缺陷
从图中可以看出,缺陷表面的皮革纹理几乎不存在了,所以这种缺陷很可能是比较钝的物体造成。对于比较平缓的缺陷区域,梯度几乎没有变化,但对于背景纹理它们的梯度会始终存在,因此利用梯度信息图检测这种缺陷是一种不错的选择。划痕缺陷正好与其相反,划痕表征的是细长状,尖锐物体划伤,所以边缘梯度较锐利,同样用梯度信息提取。
总结:如果在实际应用中,有些产品缺陷对光源角度有要求,且方向不固定(比如:带方向的缺陷,需要多角度打光才能凸显缺陷的产品)那么就可以考虑光度立体法。