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)
————————————————
版权声明:本文为CSDN博主「小邢同学」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https: