在网上看到有几张个瓶口缺陷的图片拿来练练手,先上原图
这几张应该是都有缺陷的,特别是有一张缺口不明显只有口边有黑色区域
下面是我的检测程序:
dev_close_window () * read_image (Image, 'C:/Users/liuhang/Desktop/瓶口缺陷检测/爆口.bmp') * Image Acquisition 01: Code generated by Image Acquisition 01 list_files ('C:/Users/liuhang/Desktop/瓶口缺陷检测', ['files','follow_links'], ImageFiles) tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles) for Index := 0 to |ImageFiles| - 1 by 1 read_image (Image, ImageFiles[Index]) * Image Acquisition 01: Do something get_image_size (Image, Width, Height) dev_open_window (0, 0, Width/2.5, Height/2.5, 'black', WindowHandle) dev_display (Image) * threshold_sub_pix (Image, Border, 128) edges_sub_pix (Image, Edges, 'canny', 0.5, 20, 40) segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2) select_shape_xld (ContoursSplit, SelectedXLD, 'contlength', 'and', 100, 5000) union_cocircular_contours_xld (SelectedXLD, UnionContours, 0.25, 0, 0.2, 50, 20, 20, 'true', 2) select_shape_xld (UnionContours, SelectedXLD1, 'circularity', 'and', 0.9, 1) dev_clear_window () dev_display (Image) dev_display (SelectedXLD1) fit_circle_contour_xld (SelectedXLD1, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder) gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1) sort_contours_xld (ContCircle, SortedContours, 'upper_left', 'true', 'row') * select_shape_xld (SelectedXLD1, SelectedXLD2, 'contlength', 'and', 1200, 99999) * select_contours_xld (Edges, SelectedContours, 'contour_length', 20, 500, 0, 0 * Number:=|Row| select_obj (SortedContours, ObjectSelected, 1) gen_region_contour_xld (ObjectSelected, Region, 'filled') reduce_domain (Image, Region, ImageReduced) scale_image (ImageReduced, ImageScaled, 0.2, 2) threshold (ImageScaled, Region1, 40, 80) connection (Region1, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 15500, 40000) select_shape (SelectedRegions, SelectedRegions1, 'max_diameter', 'and', 500, 700) dev_clear_window () dev_set_color ('green') dev_display (Image) dev_display (SelectedRegions1) stop () endfor
运行效果如下:
开始我是打算找黑色的缺陷区域作为判断条件,后面发现干扰比较多不好提,转而找白色区域,如结果图。但是发现精准度不好,面积判断条件不好把控,如原图1中就是有缺陷的,口边有黑色区域但是白色部分跟正常的瓶口区别不大, 而且小的缺陷也不好剔除。各位有什么好的方法,指导一下吧,谢谢大家!!