文章目录
- 处理要求
- 处理方法1
- 方法一思路
- 方法一halcon源码
- 处理效果
- 处理方法2
- 方法二思路
- 方法二halcon源码
- 处理效果
|
处理要求
椭圆/圆环(产品易变形,为椭圆)内外圆毛刺(凸起)缺口(凹陷)检测。
处理方法1
方法一思路
1、这是一个圆环产品检测,我们可以通过产品区域与标准圆环进行比较得出不良区域。
2、为了避免误检、误判,我们可以通过区域筛选阈值偏移的方法滤除干扰区域,可以将标准圆环放大消除一些圆度导致干扰。
3、根据不同用户的精度要求,可以通过调节缺陷面积进行筛选。
4、方法1的代码量有点多,但是更贴近工业现场使用。
方法一halcon源码
dev_close_window ()
read_image (Image, 'C:/Users/22967/Desktop/圆环缺陷检测/处理1.jpg')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_display (Image)*********************方法一**************************
*****变量定义
* 卡尺测量参数
CenterRow:=0
CenterColumn:=0
CenterRadius:=0
* 灰度分割阈值偏移
ThresholdOffest:=80
* 缺陷区域面积阈值
NGArea:=50
*圆环内外偏移阈值
RadiusOffest:=5
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('C:/Users/22967/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 1read_image (Image, ImageFiles[Index])rgb1_to_gray (Image, GrayImage)*****圆环灰度筛选binary_threshold (GrayImage, Region, 'max_separability', 'dark', UsedThreshold)threshold (GrayImage, Region1, 0, UsedThreshold+ThresholdOffest)*分割连通域connection (Region1, ConnectedRegions)*选取圆环区域select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)*滤除圆环边缘毛刺opening_circle (SelectedRegions, RegionOpening, 1.5)*****求圆环内外圆*求圆环外圆smallest_circle (RegionOpening, Row, Column, Radius)CenterRow[0]:=RowCenterColumn[0]:=ColumnCenterRadius[0]:=Radius*求圆环内圆fill_up (RegionOpening, RegionFillUp)difference (RegionFillUp, RegionOpening, RegionDifference)connection (RegionDifference, ConnectedRegions2)select_shape_std (ConnectedRegions2, SelectedRegions1, 'max_area', 70)smallest_circle (SelectedRegions1, Row1, Column1, Radius1)CenterRow[1]:=Row1CenterColumn[1]:=Column1CenterRadius[1]:=Radius1*****对内外圆进行卡尺测量*创建测量句柄create_metrology_model (MetrologyHandle)*设置卡尺测量参数add_metrology_object_circle_measure (MetrologyHandle, CenterRow, CenterColumn, CenterRadius, CenterRadius[0]/10, CenterRadius[0]/60, 1, 4, ['measure_distance','min_score'], [CenterRadius[0]/30,0.2], Indexnumb)*进行测量apply_metrology_model (Image, MetrologyHandle)*得到测量结果get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'all_param', Parameter)get_metrology_object_result_contour (Contour, MetrologyHandle, 'all', 'all', 1.5)get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)*****求出标准圆环,进行缺陷检测*突出部分gen_circle (Circle, Parameter[0], Parameter[1], Parameter[2]+RadiusOffest)gen_circle (Circle1, Parameter[3], Parameter[4], Parameter[5]-RadiusOffest)difference (Circle, Circle1, RegionDifference1)difference (SelectedRegions, RegionDifference1, RegionDifference2)*内凹部分gen_circle (Circle2, Parameter[0], Parameter[1], Parameter[2]-RadiusOffest)gen_circle (Circle3,Parameter[3], Parameter[4], Parameter[5]+RadiusOffest)difference (Circle2, Circle3, RegionDifference4)difference (RegionDifference4, SelectedRegions, RegionDifference3)*滤除噪点opening_circle (RegionDifference2, RegionOpening1, 1.5)opening_circle (RegionDifference3, RegionOpening2, 1.5)*合并缺陷区域union2 (RegionOpening1, RegionOpening2, RegionUnion)closing_circle (RegionUnion, RegionClosing, 3.5)connection (RegionClosing, ConnectedRegions1)*结果判断area_center (ConnectedRegions1, Area, Row2, Column2)count_obj (ConnectedRegions1, Number)gen_empty_obj (EmptyObject)for Index1 := 1 to Number by 1if (Area[Index1-1] > NGArea)select_obj (ConnectedRegions1, ObjectSelected, Index1)smallest_circle (ObjectSelected, Row3, Column3, Radius2)gen_circle (Circle4, Row3, Column3, Radius2)concat_obj (EmptyObject, Circle4, EmptyObject)endifendfordev_set_draw ('margin')dev_set_line_width (3)dev_display (Image)dev_display (EmptyObject)
* stop()
endfor
clear_metrology_model (MetrologyHandle)
处理效果
处理方法2
方法二思路
1、利用形态学方法进行缺陷检测。
2、缺点就是对圆度不敏感。
方法二halcon源码
dev_close_window ()
read_image (Image, 'C:/Users/22967/Desktop/圆环缺陷检测/处理1.jpg')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_display (Image)
*********************方法二**************************
* 灰度分割阈值偏移
ThresholdOffest:=80
*外圆缺陷查找阈值
OutCircleTh:=200.5
*内圆缺陷查找阈值
InCircleTh:=100.5
*缺陷区域面积阈值
NGArea:=50
*噪点过滤阈值
DelNoise:=1.5* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('C:/Users/22967/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])rgb1_to_gray (Image, GrayImage)*二值化选取垫片区域binary_threshold (GrayImage, Region, 'max_separability', 'dark', UsedThreshold)threshold (GrayImage, Region1, 0, UsedThreshold+ThresholdOffest)connection (Region1, ConnectedRegions)select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)*外圆缺陷查找fill_up (SelectedRegions, RegionFillUp1)opening_circle (RegionFillUp1, RegionOpening, OutCircleTh)difference (RegionFillUp1, RegionOpening, RegionDifference5)*内圆缺陷查找difference (RegionFillUp1, SelectedRegions, RegionDifference6)connection (RegionDifference6, ConnectedRegions3)select_shape_std (ConnectedRegions3, SelectedRegions2, 'max_area', 70)opening_circle (SelectedRegions2, RegionOpening3, InCircleTh)difference (SelectedRegions2, RegionOpening3, RegionDifference7)*合并缺陷区域union2 (RegionDifference5, RegionDifference7, RegionUnion1)opening_circle (RegionUnion1, RegionOpening4, DelNoise)connection (RegionOpening4, ConnectedRegions4)*结果判断area_center (ConnectedRegions4, Area1, Row4, Column4)gen_empty_obj (EmptyObject1)for Index1 := 1 to |Area1| by 1if (Area1[Index1-1] > NGArea)select_obj (ConnectedRegions4, ObjectSelected, Index1)smallest_circle (ObjectSelected, Row3, Column3, Radius2)gen_circle (Circle4, Row3, Column3, Radius2)concat_obj (EmptyObject1, Circle4, EmptyObject1)endifendfor *显示结果dev_set_draw ('margin')dev_set_line_width (3)dev_display (Image)dev_display (EmptyObject1)stop()
endfor
处理效果
------------------------------------------------------------------------------------------分割线
------------------------------------------------------------------------------------------
|
|