一个halcon拟合直线的例子

read_image (hImage, 'E:/vs2012/halcon卡尺例程/白光碗光效果4.bmp')
get_image_pointer1(hImage, Pointer, Type, Width, Height)
*功能:获取一个通道的指针,得到HTuple  Pointer, Type, CurWidth, CurHeight
dev_set_draw('margin')
dev_set_color ('green')

****************************************
* 卡尺---X轴
****************************************
*找边参数:长度m_dMeasureHeight ,滤波m_dMeasureSmooth ,对比度m_dMeasureContrast
draw_rectangle1(3600, Row1, Column1, Row2, Column2)
gen_rectangle1(Rectangle, Row1, Column1, Row2, Column2)
XResultRows:=[]  
XResultCols:=[]
k:=0
*头文件---声明,定义变量
for Coli := Column1 to Column2 by 5									
	 dLen := (Row2-Row1)/2
     dCol := Coli
     dRow := (Row1+Row2)/2
     dPhi:=rad(90)
     m_dMeasureHeight:=5		
     m_dMeasureSmooth:=2
     m_dMeasureContrast:=60
     gen_rectangle2(Rect, dRow, dCol, dPhi, dLen, m_dMeasureHeight)
     gen_measure_rectangle2(dRow,dCol,dPhi,dLen,m_dMeasureHeight, Width, Height,'nearest_neighbor', tMeasureHandle)
     *measure_pos (hImage, tMeasureHandle, m_dMeasureSmooth, m_dMeasureContrast, 'positive', 'last', tTempRow, tTempCol, NULL, NULL)		
*功能:提取与矩形或环状弧垂直的直线边缘 ,last/positive和first/negative决定了找边的方向? 极性选择:暗到亮   
      measure_pos (hImage, tMeasureHandle, m_dMeasureSmooth, m_dMeasureContrast, 'negative', 'first', tTempRow, tTempCol, NULL, NULL)
*极性选择:亮到暗    
      if( |tTempRow|=1)	
         XResultRows:=[XResultRows,tTempRow]
         XResultCols:=[XResultCols,tTempCol]
         k:=k+1
     endif
     close_measure(tMeasureHandle)
endfor
stop ()			
if (k>=30)
    dev_display(hImage)
    gen_contour_polygon_xld(hHoleContour,XResultRows,XResultCols)	
*功能:根据区域创建 XLD轮廓(contour)   
    fit_line_contour_xld(hHoleContour, 'tukey', -1, 0, 5, 2, XRowBegin, XColBegin, XRowEnd, XColEnd, XNr, XNc, XDist)
*功能:根据分割线近似获取 XLD轮廓(contour)    
    XCStart := 0
    XRStart := XDist/XNr    
    XCEnd := Width
    XREnd := (XDist - (XCEnd * XNc))/XNr
    dev_display(hImage)
    gen_contour_polygon_xld (XContour, [XRStart,XREnd], [XCStart,XCEnd])
*     gen_region_line(XLine, XRStart, XCStart, XREnd, XCEnd)
 *********输出X轴角度 *********
    angle_ll(0, 0, 0, Width, XRStart, XCStart, XREnd, XCEnd, XAngle)
*功能:计算两条线的夹角
else
    set_tposition(3600, 100, 100)
    write_string(3600, '找边失败') 
endif

****************************************
* 卡尺---Y轴
****************************************
draw_rectangle1(3600, Row1, Column1, Row2, Column2)
gen_rectangle1(Rectangle, Row1, Column1, Row2, Column2)
YResultRows:=[]  
YResultCols:=[]
k:=0
for Rowi := Row1 to Row2 by 5										
	 dLen := (Column2-Column1)/2
     dRow := Rowi
     dCol := (Column1+Column2)/2
     dPhi:=0
     m_dMeasureHeight:=5		
     m_dMeasureSmooth:=2
     m_dMeasureContrast:=60
     gen_rectangle2(Rect, dRow, dCol, dPhi, dLen, m_dMeasureHeight)
     gen_measure_rectangle2(dRow,dCol,dPhi,dLen,m_dMeasureHeight, Width, Height,'nearest_neighbor', tMeasureHandle)
    measure_pos (hImage, tMeasureHandle, m_dMeasureSmooth, m_dMeasureContrast, 'positive', 'last', tTempRow, tTempCol, NULL, NULL)		
     if( |tTempRow|=1)	
         YResultRows:=[YResultRows,tTempRow]
         YResultCols:=[YResultCols,tTempCol]
         k:=k+1
     endif
     close_measure(tMeasureHandle)
endfor
stop ()			
if (k>=30)
    dev_display(hImage)
    gen_contour_polygon_xld(hHoleContour,YResultRows,YResultCols)
    fit_line_contour_xld(hHoleContour, 'tukey', -1, 0, 5, 2, YRowBegin, YColBegin, YRowEnd, YColEnd, YNr, YNc, YDist)
    YRStart := 0
    YCStart := YDist/YNc    
    YREnd := Height
    YCEnd := (YDist - (YREnd * YNr))/YNc
    dev_display(hImage)
    gen_contour_polygon_xld (YContour, [YRStart,YREnd], [YCStart,YCEnd])
*     gen_region_line(YLine, YRStart, YCStart, YREnd, YCEnd)
    ***输出Y轴角度
    angle_ll(0, 0, 0, Width, YRStart, YCStart, YREnd, YCEnd, YAngle)
else
    set_tposition(3600, 100, 100)
    write_string(3600, '找边失败') 
endif

phi:=XAngle-YAngle
angle:=deg(phi)
*求交点
dev_display(hImage)
dev_display (XContour)
dev_display (YContour)
dev_set_color('red')
intersection_ll (YRStart, YCStart, YREnd, YCEnd, XRStart, XCStart, XREnd, XCEnd, Row, Column, IsParallel)
*功能:计算两条线的交集点(相交点)
gen_circle_contour_xld(ContCircle, Row, Column, 20, 0, 6.28318, 'positive', 1)
* dev_set_color ('green')
* intersection (YLine, XLine, RegionIntersection)
* area_center (RegionIntersection, Area, Row0, Column0)
* gen_cross_contour_xld(Cross0, Row0, Column0, 60, 0)

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值