* This example checks bottle necks for defects.
* First, the bottle is detected with basic morphology,
* edge detection and circle fitting.
* Then, the neck area is transformed with a polar transformation.
* After that, in the transformed image a dynamic threshold is used
* to detect defects. Finally, the results are displayed.
*
*
* tuning parameters
SmoothX := 501
ThresholdOffset := 25
MinDefectSize := 50
*
* 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
read_image (Image, 'bottles/bottle_mouth_' + Index$'.02')
*
* Part 1: Use basic morphology to detect bottle
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)
reduce_domain (Image, RegionDilation, ImageReduced)
*
* Find the bottle center by fitting a circle to extracted edges
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)
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: Find defects with a dynamic threshold
* Note the strong smoothing in x-direction in the transformed image.
scale_image_max (ImagePolar, ImageScaleMax)
mean_image (ImageScaleMax, ImageMean, SmoothX, 3)
dyn_threshold (ImageScaleMax, ImageMean, Regions1, 55, 'not_equal')
connection (Regions1, Connection)
select_shape (Connection, SelectedRegions, 'height', 'and', 9, 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')
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', 12, 12, 'red', 'false')
else
disp_message (WindowHandle1, 'OK', 'window', 12, 12, 'forest green', 'false')
endif
if (Index < 16)
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
endif
endfor
* Reset system parameters
set_system ('store_empty_region', StoreEmptyRegion)
halcon瓶口缺陷检测
最新推荐文章于 2024-01-03 14:18:02 发布