- read_image (Image, './1.png')
- *//获得区域
- decompose3 (Image, R, G, B)
- threshold (G, RegionO, 123, 255)
- fill_up (RegionO, RegionO)
- opening_circle (RegionO, RegionO, 10.5)
- *//Mode1:1代表思路一,2代表思路2
- Mode1:=2
- if(Mode1==1)
- *//思路一
- *//获得基准角度
- smallest_rectangle2 (RegionO, Row, Column, Phi, Length1, Length2)
- *//仿射变换
- hom_mat2d_identity (HomMat2D)
- hom_mat2d_rotate (HomMat2D, -Phi-rad(90), Row, Column, HomMat2DR)
- hom_mat2d_identity (HomMat2DI)
- hom_mat2d_rotate (HomMat2DI, Phi+rad(90), Row, Column, HomMat2DRI)
- affine_trans_region (RegionO, RegionOAF, HomMat2DR, 'nearest_neighbor')
- *//Mode2获得转正后的区域每行的起点和终点的两种方法
- *//个人推荐第二种,用纯形态学计算边缘性能会高很多
- Mode2:=1
- if(Mode2==1)
- *1.使用Halcon自带的算子实现
- get_region_runs (RegionOAF, Row1, ColB, ColE)
- elseif(Mode2==2)
- *2.使用自定义算子实现
- *//get_region_ud_edge:目的:获取区域指定方向的边缘,输入区域,输出指定方向的边缘点
- get_region_ud_edge (RegionOAF, 'Left', Row1, ColB)
- get_region_ud_edge (RegionOAF, 'Right', Row1, ColE)
- endif
- *//获得每行的宽度并找到最大的值和对应的下标
- tuple_sub (ColE, ColB, Width)
- tuple_max (Width, WidthMax)
- tuple_find_first (Width, WidthMax, WidthMaxIdx)
- gen_arrow_contour_xld (ArrowS, Row1[WidthMaxIdx], ColB[WidthMaxIdx], Row1[WidthMaxIdx], ColE[WidthMaxIdx], 5, 5)
- affine_trans_contour_xld (ArrowS, ContS, HomMat2DRI)
- elseif(Mode1==2)
- *//思路二
- *//这一步直接就得到结果了,下面的步骤都是用于可视化的
- smallest_rectangle2 (RegionO, Row, Column, Phi, Length1, WidthMax)
- *//get_rect2_vertex:目的:获得旋转矩形的四个拐点,输入旋转矩形的五个参数
- get_rect2_vertex (Row, Column, Phi, Length1-1, WidthMax-1,\
- RowTL, ColTL, RowTR, ColTR, RowBL, ColBL, RowBR, ColBR)
- distance_pp (RowTL, ColTL, RowTR, ColTR, Dis1)
- distance_pp (RowTL, ColTL, RowBL, ColBL, Dis2)
- if(Dis1>=Dis2)
- *//上下边缘为长边
- gen_contour_polygon_xld (ContL1, [RowTL,RowTR], [ColTL,ColTR])
- gen_contour_polygon_xld (ContL2, [RowBL,RowBR], [ColBL,ColBR])
- concat_obj (ContL1, ContL2, ContS)
- gen_arrow_contour_xld (ArrowS1, (RowTL+RowTR)/2, (ColTL+ColTR)/2, (RowBL+RowBR)/2, (ColBL+ColBR)/2, 5, 5)
- gen_arrow_contour_xld (ArrowS2, (RowBL+RowBR)/2, (ColBL+ColBR)/2, (RowTL+RowTR)/2, (ColTL+ColTR)/2, 5, 5)
- concat_obj (ArrowS1, ArrowS2, ArrowS)
- else
- *//左右边缘为长边
- gen_contour_polygon_xld (ContL1, [RowTL,RowBL], [ColTL,ColBL])
- gen_contour_polygon_xld (ContL2, [RowTR,RowBR], [ColTR,ColBR])
- concat_obj (ContL1, ContL2, ContS)
- gen_arrow_contour_xld (ArrowS1, (RowTL+RowBL)/2, (ColTL+ColBL)/2, (RowTR+RowBR)/2, (ColTR+ColBR)/2, 5, 5)
- gen_arrow_contour_xld (ArrowS2, (RowTR+RowBR)/2, (ColTR+ColBR)/2, (RowTL+RowBL)/2, (ColTL+ColBL)/2, 5, 5)
- concat_obj (ArrowS1, ArrowS2, ArrowS)
- endif
- concat_obj (ArrowS, ContS, ContS)
- WidthMax:=WidthMax*2
- endif
- *//可视化
- dev_clear_window ()
- dev_display (Image)
- dev_set_color ('red')
- dev_display (ContS)
- disp_message (200000, 'MaxWidth: '+WidthMax+' pix', 'window', 10, 10, 'green', 'false')
复制代码
|