* 这个例子检查瓶口缺陷,
* 首先找到瓶口,找瓶口的方法就通过自动阈值,然后进行圆拟合
*边缘检测,找到边缘之后缩小圆,找到瓶口这一个圆带状图像
*把圆带拉抻变换成长方形,这样其实是为了使用平均值滤波,把窗口的设置为【500,3】大小,这样就可以把垂直方向的差异提取出来
*然后再通过dyn_threshold找出差异位置,计算连通域,然后再找出连通域高度大于9的,如果有,就认为包含缺陷,如果没有就不包含缺陷
*然后给定一个动态的阈值来检测缺陷,最后显示缺陷
*总结,这个例子主要是看瓶口径向一致性的检查,正常的瓶口径向一致性很好,但是有破损的则不然,所以这是一个很好的思路
* tuning parameters
*均值滤波窗口宽度
SmoothX := 501
*动态阈值操作的容差值,但是下面直接使用的55这个值,没使用变量
ThresholdOffset := 55
*找到的瑕疵高度最小值
MinDefectSize := 9
*
* initialization
PolarResolution := 640
RingSize := 70
get_system ('store_empty_region', StoreEmptyRegion)
set_system ('store_empty_region', 'false')
read_image (Image, 'bottles/bottle_mouth_01')
dev_update_off ()
dev_close_window ()
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 512, WindowHandle1)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
dev_display (Image)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_open_window_fit_size (0, 648, RingSize, PolarResolution, 150, 512, WindowHandle)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')
*
* Main loop
*
* Detect defects in bottle necks
for Index := 1 to 16 by 1
*读取图像到Image
read_image (Image, 'bottles/bottle_mouth_' + Index$'.02')
*
* Part 1: 检测瓶子口
auto_threshold (Image, Regions, 2)
*通过自动阈值,选择最黑的区域
select_obj (Regions, DarkRegion, 1)
opening_circle (DarkRegion, RegionOpening, 3.5)
closing_circle (RegionOpening, RegionClosing, 25.5)
*填充区域
fill_up (RegionClosing, RegionFillUp)
*找到边界
boundary (RegionFillUp, RegionBorder, 'outer')
*膨胀边界
dilation_circle (RegionBorder, RegionDilation, 3.5)
*取图像的边缘,其实取的是Image 和RegionDilation的相同的部分
reduce_domain (Image, RegionDilation, ImageReduced)
*
*通过为提取的边缘拟合一个圆来查找瓶中心
*canny边缘检测
edges_sub_pix (ImageReduced, Edges, 'canny', 0.5, 20, 40)
*打散成圆弧和直线
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
*找出一个共同圆的部分
union_cocircular_contours_xld (ContoursSplit, UnionContours, 0.9, 0.5, 0.5, 200, 50, 50, 'true', 1)
*计算轮廓或者多边形的长度
length_xld (UnionContours, Length)
*选择长度最长的那一个
*sort_index(Length)[|Length| - 1] 是取最大的长度的index,因为select_obj的index参数是从1开始的,所以需要加1
select_obj (UnionContours, LongestContour, sort_index(Length)[|Length| - 1] + 1)
*圆形拟合
fit_circle_contour_xld (LongestContour, 'ahuber', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
*
* Part 2:圆形带变换成长方形 Transform the ring-shaped bottle neck region to a rectangle
*创建一个圆
gen_circle (Circle, Row, Column, Radius)
*膨胀圆
dilation_circle (Circle, RegionDilation, 5)
*腐蚀圆
erosion_circle (Circle, RegionErosion, RingSize - 5)
*做减法
difference (RegionDilation, RegionErosion, RegionDifference)
*把带状区域的图像找出来
reduce_domain (Image, RegionDifference, ImageReduced)
*极坐标到笛卡尔坐标转换
polar_trans_image_ext (ImageReduced, ImagePolar, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, 'nearest_neighbor')
*
* Part 3: 通过动态阈值打到缺陷位置
* Note the strong smoothing in x-direction in the transformed image.
*scale_image_max应该是指正则化,找到图像最亮和最暗,然后把图像像素拉成0-255
scale_image_max (ImagePolar, ImageScaleMax)
*使用平均平滑图像,后面两个参数应该是平均窗口的大小,但是为什么长度设置为501高为3呢,可能是因为垂直方向上差异大,想要找出垂直方向差异
mean_image (ImageScaleMax, ImageMean, SmoothX, 3)
*使用本地的图像动态阈值,然后分割imageSacleMax,Offset越大,找到的区域越小,因为容差大了,大于这个容差的就少了
dyn_threshold (ImageScaleMax, ImageMean, Regions1, ThresholdOffset, 'not_equal')
*计算连通区域
connection (Regions1, Connection)
*选择高度在9~9999之间的连通区域
select_shape (Connection, SelectedRegions, 'height', 'and', MinDefectSize, 99999)
* ignore noise regions
closing_rectangle1 (SelectedRegions, RegionClosing1, 10, 20)
union1 (RegionClosing1, RegionUnion)
* re-transform defect regions for visualization
*把带状反变换为极坐标图像
polar_trans_region_inv (RegionUnion, XYTransRegion, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, 1280, 1024, 'nearest_neighbor')
*
* Part 4: Display results
* display original image with results
dev_set_window (WindowHandle1)
dev_display (Image)
dev_set_color ('blue')
dev_display (RegionDifference)
dev_set_color ('red')
*XYTransRegion保存的是图像中的瑕疵
dev_display (XYTransRegion)
* display polar transformed inspected region with results
* The image and resulting region are rotated by 90 degrees
* only for visualization purposes! (I.e. to fit better on the screen)
* The rotation is NOT necessary for the detection algorithm.
dev_set_window (WindowHandle)
rotate_image (ImagePolar, ImageRotate, 90, 'constant')
dev_display (ImageRotate)
count_obj (RegionUnion, Number)
if (Number > 0)
mirror_region (RegionUnion, RegionMirror, 'diagonal', PolarResolution)
mirror_region (RegionMirror, RegionMirror, 'row', PolarResolution)
dev_display (RegionMirror)
disp_message (WindowHandle1, 'Not OK', 'window', -1, -1, 'red', 'false')
else
disp_message (WindowHandle1, 'OK', 'window', -1, -1, 'forest green', 'false')
endif
if (Index < 16)
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
endif
endfor
Halcon例程学习之瓶口缺陷检测
猜你喜欢
转载自blog.csdn.net/chongzi865458/article/details/79675345
今日推荐
周排行