当前这个项目主要实现上下两电路板间,对应两金手指相对左右偏移量计算。
计算出偏移量后通过执行机构,使上下板金手指一一对齐。
实现结果如图,主要思路是
1.先提取上板白色区域最右侧金手指中心线。
2.再提取下板白色区域最右侧金手指中心线。计算其中心线上端点与上侧中心线投影点坐标。
3.最终计算投影坐标点到下板最右侧金手指中心线的距离,就为要计算的结果。
至于上板,下板最右侧金手指的提取,中间过程进行了一系列的blob分析与处理。此处理方法也是blob分析中最简单,最常用的。具体可以参阅代码
- dev_close_window ()
- read_image (Image, '1.bmp')
- dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
- dev_display (Image)
- rgb1_to_gray (Image, Image)
- get_image_size (Image, ImageWidth, ImageHeight)
- threshold (Image, IconTemp, 120, 255)
- connection (IconTemp, Icon)
- *筛选全部白色区域
- select_shape (Icon, IconTemp, ['area','rectangularity'], 'and', [2000,0.9], [10000,1])
- *筛选上,下白色区
- select_shape(IconTemp, Icon, 'row1', 'and', 0, 100)
- difference (IconTemp, Icon, IconTemp2)
- *处理上白色
- union1 (Icon, IconTemp)
- smallest_rectangle1 (IconTemp, Row, Column, Row1, Column1)
- gen_rectangle1 (Rectangle, Row, Column, Row1/2, ImageWidth)
- reduce_domain(Image, Rectangle, IconTemp)
- threshold (IconTemp, Icon, 0, 120)
- connection (Icon, IconTemp)
- select_shape (IconTemp, Icon, ['area','rectangularity'], 'and', [500,0.9], [999999,1])
- sort_region (Icon, IconTemp, 'upper_right', 'false', 'column')
- select_obj(IconTemp, Icon, 1)
- gen_contour_region_xld (Icon, IconTemp, 'border')
- fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
- *提取矩形中心线为上参考线
- get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
- if( Phi<=0)
- XValue := LineCenterX[3]
- YValue := LineCenterY[3]
- XValue1:= LineCenterX[1]
- YValue1:= LineCenterY[1]
- else
- XValue := LineCenterX[1]
- YValue := LineCenterY[1]
- XValue1:= LineCenterX[3]
- YValue1:= LineCenterY[3]
- endif
- *处理下白色区
- union1 (IconTemp2, IconTemp)
- dilation_rectangle1 (IconTemp, IconTemp2, 50, 50)
- smallest_rectangle1 (IconTemp2, Row, Column, Row1, Column1)
- gen_rectangle1 (Rectangle, Row, Column, Row1, ImageWidth)
- reduce_domain(Image, Rectangle, IconTemp)
- threshold (IconTemp, Icon, 100, 255)
- connection (Icon, IconTemp)
- select_shape (IconTemp, Icon, ['area','rectangularity','ratio'], 'and', [500,0.8,2], [999999,1,20])
- sort_region (Icon, IconTemp, 'upper_right', 'false', 'column')
- select_obj(IconTemp, Icon, 1)
- gen_contour_region_xld (Icon, IconTemp, 'border')
- fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
- get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
- if( Phi<=0)
- XValue2 := LineCenterX[3]
- YValue2 := LineCenterY[3]
- XValue3 := LineCenterX[1]
- YValue3 := LineCenterY[1]
- else
- XValue2 := LineCenterX[1]
- YValue2 := LineCenterY[1]
- XValue3 := LineCenterX[3]
- YValue3 := LineCenterY[3]
- endif
- *下金手指中心线顶点到上金手指中心线的投影。
- projection_pl(YValue2,XValue2,YValue,XValue,YValue1,XValue1,YValue1,XValue1)
- *投影点到下金手指中心线距离
- distance_pl(YValue1,XValue1,YValue,XValue,YValue2,XValue2,Distance)
- *显示结果
- gen_contour_polygon_xld(LineXLD,[YValue,YValue1],[XValue,XValue1])
- gen_contour_polygon_xld(LineXLD1,[YValue2,YValue3],[XValue2,XValue3])
- gen_arrow_contour_xld(Arrow, YValue1, XValue1, YValue2, XValue2, 10, 10)
- gen_arrow_contour_xld(Arrow1, YValue2, XValue2, YValue1, XValue1, 10, 10)
- dev_display(Image)
- dev_display(LineXLD)
- dev_display(LineXLD1)
- dev_set_color('green')
- dev_display(Arrow)
- dev_display(Arrow1)
- message := Distance
- .2f'+'mm'
- get_string_extents(WindowHandle, message, Ascent, Descent, Width1, Height1)
- dev_disp_text(message, 'image', YValue2-Height1, XValue2, 'green', ['box'], ['false'])
复制代码
|