文章目录
- 算子
- 更多xld算子
- 更多区域算子
- Blob 分析案例
- 预处理图像增强降噪
- 图像降噪
- 图像增强
- Halcon 基于圆的拟合
- Halcon 共线联合案例
- Halcon 拟合动画案例
- Halcon 拟合椭圆
算子
二值化算子 (二值化后获取的都是区域)
二值化算子
clip_region_rel 剪切区域
clip_region_rel(Region : RegionClipped : Top, Bottom, Left, Right : )
Region是输入区域;
RegioonClipped是减掉后的输出区域;
Top, 顶端
Bottom,底端
Left,左边
Right右边
edges_sub_pix 提取亚像素边缘
edges_sub_pix(Image : Edges : Filter, Alpha, Low, High : )*提取亚像素精密边缘轮廓
Filter :过滤算法
Filter = 'canny'刚好相反,alpha值越大,轮廓细节就越少(可能有一部分轮廓消失)
alpha:指定值越小,平滑越强大,会减少边缘细节。
Low和High:
使用灰度直方图计算两个阀值,凡是像素灰度值大于高阀值的一定是轮廓; 凡是小于低阀值的一定不是轮廓;
如果检测像素灰度值大于低阀值但又小于高阀值,那就要看这个像素的邻接像素中有没有超过高阀值的边缘像素:如果有的话那么它就是轮廓边缘了,否则他就不是轮廓边缘;
lines_gauss 检测线条算子
lines_gauss(Image : Lines : Sigma, Low, High, LightDark, ExtractWidth, LineModel, CompleteJunctions : )
Image (input_object) :输入图像
Lines (output_object) :检测线条(XLD)
Sigma (input_control) :高斯滤波值
Low (input_control) :滞后阈值分割的低阈值
High (input_control) :滞后阈值分割的高阈值
LightDark (input_control) :提取线条类型( ‘dark’,‘light’)
ExtractWidth (input_control) :是否提取线宽(‘false’,‘true’)
LineModel (input_control) :用来调整线条位置和宽度的线模型(‘bar-shaped’, ‘gaussian’, ‘none’, ‘parabolic’)
CompleteJunctions (input_control) :在断连的部分是否添加节点使线条连续(‘false’, ‘true’)
clip_contours_xld 亚像素边缘分割
clip_contours_xld ( Contours : ClippedContours : Row1, Column1,Row2, Column2 : )
Contours :待分割的轮廓
ClippedContours :分割后的轮廓
Row1, Column1,Row2, Column2: 轮廓的左上角坐标和右下角坐标
segment_contours_xld 亚像素边缘分割
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
Edges:待分割的轮廓
ContoursSplit:分割后的轮廓
'lines_circles':分割模式,分割出线段、类圆
5:平滑轮廓系数
4:最大线距
2:最小线距
union_collinear_contours_xld 边缘联合共线
union_collinear_contours_xld(Contours : UnionContours : MaxDistAbs, MaxDistRel, MaxShift, MaxAngle, Mode : )
Contours (input_object) :输入XLD轮廓
UnionContours (output_object) :合并后输出轮廓
MaxDistAbs (input_control) :沿参考轮廓的回归线测量两个轮廓之间的最大间隙长度(间隙在参考轮廓拟合直线上的投影)。
MaxDistRel (input_control) :间隙在参考轮廓拟合直线上的投影长度与参考轮廓长度的比值上限(见下面详解)
MaxShift (input_control) :第二个轮廓与参考轮廓拟合直线的最大距离
MaxAngle (input_control) :两个轮廓拟合直线的角度( 0.0 ≤ MaxAngle ≤ 0.78539816339(45度))
Mode (input_control) :定义轮廓属性处理的模式,即,是否保留或丢弃轮廓属性
union_adjacent_contours_xld 临近联合,将端点距离很近的轮廓连接起来
union_adjacent_contours_xld(Contours : UnionContours : MaxDistAbs, MaxDistRel, Mode : )
Contours (input_object) :输入轮廓
UnionContours (output_object) :合并后输出轮廓
MaxDistAbs (input_control) :两个轮廓端点间的最小距离控制参数
MaxDistRel (input_control) :两个轮廓端点间的最小距离与最长轮廓长度的比值控制参数
Mode (input_control) :定义轮廓属性处理的模式,即,是否保留或丢弃轮廓属性
union_cocircular_contours_xld 联合圆
Contours:输入的共圆轮廓XLD对象。
CenterRow:共圆的圆心的行坐标。
CenterColumn:共圆的圆心的列坐标。
Radius:共圆的半径。
StartPhi:共圆曲线的起始角度。
EndPhi:共圆曲线的结束角度。
regress_contours_xld 求具有回归属性的轮廓
regress_contours_xld(Contours : RegressContours : Mode, Iterations : )
Contours:这是输入参数,表示将进行回归的 XLD 轮廓。
RegressContours:这是输出参数,将存储回归后的结果轮廓。
Mode:此参数确定要执行的回归类型。根据所需的回归方法可以采用不同的值,如“algebraic”(代数)、“directional”(方向)、“elliptic”(椭圆)等。
Iterations:此参数指定在回归过程中执行的迭代次数。它有助于改善回归结果。
sort_contours_xld 轮廓区域排序
sort_contours_xld(Contours : SortedContours : SortMode, Order, RowOrCol : )
示例:sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
ContoursSplit(输入对象):输入包含多个轮廓的对象
SortedContours(输出对象):输出排序轮廓对象
'upper_left'(输入控制参数1):排序方法
'true'(输入控制参数2):升序true或者是降序false
'column'(输入控制参数3):按照行或者列排序
get_contour_global_attrib_xld 亚像素拟合,自动判断拟合对象算子(直线,椭圆,圆)
get_contour_global_attrib_xld (ObjectSelected输入轮廓,
'cont_approx'判断算法,
Attrib输出结果)
fit_circle_contour_xld 拟合圆算子
fit_circle_contour_xld(Contours,输入轮廓
Algorithm,拟合算法,如最小二乘法
MaxNumPoints,拟合时采用轮廓最多的点数,-1表示全部参与
MaxClosureDist,是指Maximum distance between the end points of a contour to be considered as 'closed'用来区分拟合成圆还是圆弧 ,就是要拟合的最后一个点和被认为把轮廓封闭的点,这两个点距离
ClippingEndPoints,拟合时忽略轮廓开始和结束点的数量
Iterations,拟合迭代的最大次数
ClippingFactor,离群值的裁剪因子,越小离群值越多
Row, 输出圆的坐标
Column,
Radius,输出圆半径
StartPhi,输出圆开始角度
EndPhi,输出圆终止角度
PointOrder边界点正反向)
gen_circle_contour_xld 产生亚像素圆拟合图像
gen_circle_contour_xld(:ContCircle:Row,Column,Radius,StartPhi,EndPhi,PointOrder,Resolution)
ContCircle(output_object)xld_cont(-array)→对象
产生的轮廓。
Row(input_control)circle.center.y(-array)→(real)
圆心或圆弧的中心的行坐标。
默认值:200.0
Column (input_control)circle.center.x(-array)→(真实)
圆心或圆弧的圆柱坐标。
默认值:200.0
Radius (input_control)circle.radius(-array)→(真实)
圆弧或圆弧的半径。
默认值:100.0
限制:半径> 0
StartPhi(input_control)real(-array)→(真实)
圆的起点或圆弧[rad]的角度。
默认值:0.0
EndPhi(input_control)real(-array)→(真实)
圆弧或圆弧[rad]的终点角度。
默认值:6.28318
PointOrder(input_control)string(-array)→(string)
沿圆或圆弧的点顺序。
默认值:‘正面’
价值清单:‘负面’,‘正面’
Resolution(input_control)真实→(真实)
相邻轮廓点之间的距离。
默认值:1.0
限制:分辨率> = 0.00001
fit_line_contour_xld 拟合成直线
fit_line_contour_xld(Contours : : Algorithm, MaxNumPoints, ClippingEndPoints, Iterations, ClippingFactor : RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
Contours (input_object) :输入的XLD轮廓
Algorithm (input_control):直线拟合算法( ‘drop’, ‘gauss’, ‘huber’, ‘regression’, ‘tukey’)
MaxNumPoints (input_control):参与拟合直线的最大轮廓点数(设置-1代表所有点参与计算;最小值要大于等于2,因为至少两点才能确认一条直线)
ClippingEndPoints (input_control) :拟合直线时,需要忽略轮廓起始处点个数。(一个XLD轮廓会被线分割成很多个小轮廓)
Iterations (input_control) :最大迭代次数。(不适用于’regression’)
ClippingFactor (input_control):剔除异常值因子。(算法 ‘huber’ 、‘drop’ 设置1.0,'tukey’设置2.0)
RowBegin (output_control) :拟合后的线段起始点行坐标
ColBegin (output_control) :拟合后的线段起始点列坐标
RowEnd (output_control) :拟合后的线段终点点行坐标
ColEnd (output_control) :拟合后的线段终点点列坐标
Nr (output_control) :直线方程法线式x相乘系数
Nc (output_control) :直线方程法线式y相乘系数
Dist (output_control) :坐标系原点到线段的距离
gen_contour_polygon_xld 产生多边形亚像素轮廓
gen_contour_polygon_xld ()从一个多边形生成一个XLD轮廓(以元组的形式给出)。
参数:
Contour (output_object) :产生的轮廓。
Row(input_control):多边形的行坐标。
默认值:[0,1、2、2、2]
建议值:0、1、2、3、4、5、10、20、50、100、200、500
Col(input_control):多边形的列坐标。
默认值:[0,0,0,1,2)
建议值:0、1、2、3、4、5、10、20、50、100、200、500
XLD转 region (图像进行二值化的都是区域)
gen_region_contour_xld (ContoursUnion, Region, ‘filled’)
region 转 XLD
gen_contour_region_xld (SelectedRegions, Contours, ‘border’)
shape_trans 变换区域的形状
convex:凸包性
ellipse:与输入区域有相同的矩和区域的椭圆
outer_circle:最小外接圆
inner_circle:最大内接圆
rectangle1:平行于坐标轴的最小外接矩形
rectangle2:最小外接矩形
inner_rectangle1:平行于坐标轴的最大内接矩形
inner_rectangle2:输入区域的骨架点拥有同输入区域的重心点最小的距离
shape_trans 示例
更多xld算子
更多区域算子
Blob 分析案例
Blob 分析案例
预处理图像增强降噪
图像降噪
图像增强
Halcon 基于圆的拟合
*
* The edges in the image are segmented into lines and circles.
* For the edges that are part of a circle, the circle parameters
* are estimated and the resulting circle is displayed.
read_image (Image, 'double_circle')
*
* Init window
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
*
* Segment a region containing the edges
* 快速二值化 二值化得到的都是区域
fast_threshold (Image, Region, 0, 120, 7)
* 对区域求轮廓 内边界'inner'
boundary (Region, RegionBorder, 'inner')
* 裁剪区域
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5)
* 膨胀
dilation_circle (RegionClipped, RegionDilation, 2.5)
* 裁剪区域
reduce_domain (Image, RegionDilation, ImageReduced)
*
* In the subdomain of the image containing the edges,
* extract subpixel precise edges.
* 亚像素边缘提取
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)
* 亚像素分割
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
for I := 1 to Number by 1
* 选择第一个轮廓
select_obj (ContoursSplit, ObjectSelected, I)
* 拟合
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
* Fit a circle to the line segment that are arcs of a circle
if (Attrib > 0)
* 形成一个圆的拟合
fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
* 产生拟合图像
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
dev_display (ContCircle)
endif
endfor
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
Halcon 拟合成直线和圆计算距离
* measure_metal_part.hdev: inspects metal part by fitting lines and circles
*
dev_close_window ()
dev_update_window ('off')
* ****
* step: acquire image
* ****
read_image (Image, 'metal-parts/metal-parts-01')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* ****
* step: create contours
* ****
edges_sub_pix (Image, Edges, 'lanser2', 0.5, 40, 90)
dev_display (Edges)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* ****
* step: process contours
* ****
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 6, 4, 4)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
dev_clear_window ()
dev_set_colored (12)
dev_display (SortedContours)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* ****
* step: perform fitting
* ****
ROI := [115,225,395,535]
dev_open_window (0, round(Width / 2), (ROI[3] - ROI[1]) * 2, (ROI[2] - ROI[0]) * 2, 'black', WindowHandleZoom)
dev_set_part (round(ROI[0]), round(ROI[1]), round(ROI[2]), round(ROI[3]))
set_display_font (WindowHandleZoom, 14, 'mono', 'true', 'false')
count_obj (SortedContours, NumSegments)
dev_display (Image)
NumCircles := 0
NumLines := 0
for i := 1 to NumSegments by 1
select_obj (SortedContours, SingleSegment, i)
get_contour_global_attrib_xld (SingleSegment, 'cont_approx', Attrib)
if (Attrib == 1)
NumCircles := NumCircles + 1
fit_circle_contour_xld (SingleSegment, 'atukey', -1, 2, 0, 5, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_ellipse_contour_xld (ContEllipse, Row, Column, 0, Radius, Radius, 0, rad(360), 'positive', 1.0)
dev_set_color ('white')
dev_display (ContEllipse)
set_tposition (WindowHandleZoom, Row - Radius - 10, Column)
write_string (WindowHandleZoom, 'C' + NumCircles)
ResultText := 'C' + NumCircles + ': radius = ' + Radius
else
NumLines := NumLines + 1
fit_line_contour_xld (SingleSegment, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
gen_contour_polygon_xld (Line, [RowBegin,RowEnd], [ColBegin,ColEnd])
dev_set_color ('yellow')
dev_display (Line)
* 计算距离
distance_pp (RowBegin, ColBegin, RowEnd, ColEnd, Length)
* 设置显示位置
set_tposition (WindowHandleZoom, (RowBegin + RowEnd) / 2 - Nr * 10, (ColBegin + ColEnd) / 2)
write_string (WindowHandleZoom, 'L' + NumLines)
ResultText := 'L' + NumLines + ': length = ' + Length
endif
set_tposition (WindowHandleZoom, 275 + i * 10, 230)
write_string (WindowHandleZoom, ResultText)
endfor
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_set_window (WindowHandleZoom)
dev_close_window ()
dev_clear_window ()
Halcon 共线联合案例
* close_contour_gaps.hdev: closes gaps in extracted straight contours
*
dev_close_window ()
dev_update_window ('off')
* ****
* step: create synthetic image
* ****
gen_rectangle1 (Rectangle, 30, 20, 100, 100)
* 产生一个二值区域
region_to_bin (Rectangle, BinImage, 130, 100, 120, 130)
* 矩形区域裁剪
rectangle1_domain (BinImage, ImageReduced, 20, 48, 40, 52)
* 产生均值图像,让图像变得平滑
mean_image (ImageReduced, SmoothedImage, 15, 15)
* 绘制一个灰度图像
paint_gray (SmoothedImage, BinImage, Image)
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowID)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
stop ()
* ****
* step: create contours
* ****
rectangle1_domain (Image, ImageReduced, 5, 5, 125, 115)
* 边界提取
edges_sub_pix (ImageReduced, Edges, 'lanser2', 1.1, 22, 30)
dev_display (Image)
dev_display (Edges)
stop ()
* ****
* step: process contours
* ****
* 亚像素图像分割
segment_contours_xld (Edges, LineSegments, 'lines', 5, 4, 2)
* 计算亚像素轮廓的回归参数
regress_contours_xld (LineSegments, RegressContours, 'no', 1)
* 共线联合轮廓
union_collinear_contours_xld (RegressContours, UnionContours, 10, 1, 2, 0.1, 'attr_keep')
* 亚像素轮廓排序
sort_contours_xld (UnionContours, SortedContours, 'upper_left', 'true', 'column')
dev_display (Image)
colored_display (SortedContours, ['yellow','white','white','yellow'])
dev_update_window ('on')
Halcon 拟合动画案例
* This example demonstrates how to use the operator
* union_cotangential_contours_xld.
* Therefore, the operator is applied to a set of synthetic
* contours with different parameter sets.
*
* Part I
*
* Test the following parameters:
* - FitClippingLength
* - FitLength
* - MaxTangAngle
*
* Initialization
get_system ('flush_graphic', FlushGraphic)
*
* Default parameters
FitClippingLength := 0
FitLength := 30
MaxTangAngle := rad(45)
MaxDist := 25
MaxDistPerp := 10
MaxOverlap := 2
*
RowOffsets := [25,25,240,240]
ColumnOffsets := [0,256,0,256]
*
gen_test_contour (ContourConcat1, RowOffsets[0], ColumnOffsets[0])
gen_test_contour (ContourConcat2, RowOffsets[1], ColumnOffsets[1])
gen_test_contour (ContourConcat3, RowOffsets[2], ColumnOffsets[2])
gen_test_contour (ContourConcat4, RowOffsets[3], ColumnOffsets[3])
*
* Display original contours
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_part (0, 0, 511, 511)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_clear_window ()
dev_set_line_width (3)
dev_set_colored (12)
dev_display (ContourConcat1)
dev_display (ContourConcat2)
dev_display (ContourConcat3)
dev_display (ContourConcat4)
Message := 'This example shows the effect of'
Message[1] := 'union_cotangential_contours_xld'
Message[2] := 'with different parameter sets'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Before', 'window', 12, 420, 'white', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Test values
TestFitClippingLength := 10
TestFitLength := 5
TestMaxTangAngle := rad(135)
*
* Apply different parameter configurations
union_cotangential_contours_xld (ContourConcat1, UnionContours1, FitClippingLength, FitLength, MaxTangAngle, MaxDist, MaxDistPerp, MaxOverlap, 'attr_forget')
union_cotangential_contours_xld (ContourConcat2, UnionContours2, TestFitClippingLength, FitLength, MaxTangAngle, MaxDist, MaxDistPerp, MaxOverlap, 'attr_forget')
union_cotangential_contours_xld (ContourConcat3, UnionContours3, FitClippingLength, TestFitLength, MaxTangAngle, MaxDist, MaxDistPerp, MaxOverlap, 'attr_forget')
union_cotangential_contours_xld (ContourConcat4, UnionContours4, FitClippingLength, FitLength, TestMaxTangAngle, MaxDist, MaxDistPerp, MaxOverlap, 'attr_forget')
*
* Display results
dev_clear_window ()
dev_set_line_width (5)
dev_display (UnionContours1)
dev_display (UnionContours2)
dev_display (UnionContours3)
dev_display (UnionContours4)
dev_set_line_width (1)
dev_set_color ('white')
dev_display (ContourConcat1)
dev_display (ContourConcat2)
dev_display (ContourConcat3)
dev_display (ContourConcat4)
Message[0] := 'Results of'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'After', 'window', 12, 420, 'white', 'false')
Text := 'Default parameters:\nFitClippingLength = 0\nFitLength = 30\nMaxTangAngle = rad(45)'
Text[1] := 'Clip endpoints\nFitClippingLength = 10'
Text[2] := 'Fit small end pieces\nFitLength = 5'
Text[3] := 'Allow large angles\nMaxTangAngle = rad(135)'
disp_message (WindowHandle, Text[0], 'window', 200 + RowOffsets[0], 20 + ColumnOffsets[0], 'white', 'false')
disp_message (WindowHandle, Text[1], 'window', 200 + RowOffsets[1], 20 + ColumnOffsets[1], 'white', 'false')
disp_message (WindowHandle, Text[2], 'window', 200 + RowOffsets[2], 20 + ColumnOffsets[2], 'white', 'false')
disp_message (WindowHandle, Text[3], 'window', 200 + RowOffsets[3], 20 + ColumnOffsets[3], 'white', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Part II
*
* Test the following parameters:
* - MaxDist
* - MaxDistPerp
* - MaxOverlap
* - MaxTangAngle
*
* Test values
TestMaxDist := 10
TestMaxDistPerp := 10
TestMaxOverlap := 10
TestMaxTangAngle := rad(60)
*
* Create and display test contours
Row := [200,200,200]
Column := [100,250,400]
Delay := 0.1
gen_empty_obj (FixedContours)
for I := 0 to |Row| - 1 by 1
gen_ellipse_contour_xld (ContEllipse, Row[I], Column[I], rad(90), 100, 50, 0, rad(90), 'positive', 1.5)
concat_obj (FixedContours, ContEllipse, FixedContours)
endfor
gen_ellipse_contour_xld (Contour1, Row[0] - 125, Column[0] + 10, rad(270), 25, 35, 0, rad(90), 'positive', 1.5)
gen_ellipse_contour_xld (Contour2, Row[1] - 75, Column[1] + 10, rad(0), 35, 25, 0, rad(90), 'positive', 1.5)
gen_contour_nurbs_xld (Contour3, [100,100,100,100], Column[2] + [0,10,20,30] + 10, 'auto', 'auto', 3, 1, 5)
concat_obj (Contour1, Contour2, ContoursConcat)
concat_obj (ContoursConcat, Contour3, ContoursConcat)
concat_obj (ContoursConcat, FixedContours, ContoursConcat)
Title := 'Test different parameters of\nunion_cotangential_contours_xld'
Text := ''
dev_display_results (ContoursConcat, ContoursConcat, WindowHandle, Title, Text)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Test setting for MaxDist
Title := 'Test influence of MaxDist'
DistY := 0
for DistX := 20 to -10 by -1
* Modify test contours
gen_ellipse_contour_xld (Contour1, Row[0] - 125 + DistY, Column[0] + DistX, rad(270), 25, 35, 0, rad(90), 'positive', 1.5)
gen_ellipse_contour_xld (Contour2, Row[1] - 75 + DistY, Column[1] + DistX, rad(0), 35, 25, 0, rad(90), 'positive', 1.5)
gen_contour_nurbs_xld (Contour3, [100,100,100,100] + DistY, Column[2] + [0,10,20,30] + DistX, 'auto', 'auto', 3, 1, 5)
concat_obj (Contour1, Contour2, ContoursConcat)
concat_obj (ContoursConcat, Contour3, ContoursConcat)
concat_obj (ContoursConcat, FixedContours, ContoursConcat)
* Try to unify contours
union_cotangential_contours_xld (ContoursConcat, UnionContours, FitClippingLength, FitLength, MaxTangAngle, TestMaxDist, MaxDistPerp, MaxOverlap, 'attr_forget')
* Display results
Text := 'MaxDist = ' + TestMaxDist
dev_display_results (UnionContours, ContoursConcat, WindowHandle, Title, Text)
wait_seconds (Delay)
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Test setting for MaxDistPerp
Title := 'Test influence of MaxDistPerp'
DistX := 5
for DistY := -20 to 20 by 1
* Modify test contours
gen_ellipse_contour_xld (Contour1, Row[0] - 125 + DistY, Column[0] + DistX, rad(270), 25, 35, 0, rad(90), 'positive', 1.5)
gen_ellipse_contour_xld (Contour2, Row[1] - 75 + DistY, Column[1] + DistX, rad(0), 35, 25, 0, rad(90), 'positive', 1.5)
gen_contour_nurbs_xld (Contour3, [100,100,100,100] + DistY, Column[2] + [0,10,20,30] + DistX, 'auto', 'auto', 3, 1, 5)
concat_obj (Contour1, Contour2, ContoursConcat)
concat_obj (ContoursConcat, Contour3, ContoursConcat)
concat_obj (ContoursConcat, FixedContours, ContoursConcat)
* Try to unify contours
union_cotangential_contours_xld (ContoursConcat, UnionContours, FitClippingLength, FitLength, MaxTangAngle, MaxDist, TestMaxDistPerp, MaxOverlap, 'attr_forget')
* Display results
Text := 'MaxDistPerp = ' + TestMaxDistPerp
dev_display_results (UnionContours, ContoursConcat, WindowHandle, Title, Text)
wait_seconds (Delay)
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Test positive overlap
Title := 'Test influence of positive MaxOverlap'
DistY := -5
for DistX := -15 to 15 by 1
* Modify test contours
gen_ellipse_contour_xld (Contour1, Row[0] - 125 + DistY, Column[0] + DistX, rad(270), 25, 35, 0, rad(90), 'positive', 1.5)
gen_ellipse_contour_xld (Contour2, Row[1] - 75 + DistY, Column[1] + DistX, rad(0), 35, 25, 0, rad(90), 'positive', 1.5)
gen_contour_nurbs_xld (Contour3, [100,100,100,100] + DistY, Column[2] + [0,10,20,30] + DistX, 'auto', 'auto', 3, 1, 5)
concat_obj (Contour1, Contour2, ContoursConcat)
concat_obj (ContoursConcat, Contour3, ContoursConcat)
concat_obj (ContoursConcat, FixedContours, ContoursConcat)
* Try to unify contours
union_cotangential_contours_xld (ContoursConcat, UnionContours, FitClippingLength, FitLength, MaxTangAngle, MaxDist, MaxDistPerp, abs(TestMaxOverlap), 'attr_forget')
* Display results
Text := 'MaxOverlap = ' + abs(TestMaxOverlap)
dev_display_results (UnionContours, ContoursConcat, WindowHandle, Title, Text)
wait_seconds (Delay)
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Test for negative overlap
Title := 'Test influence of negative MaxOverlap'
DistY := -5
for DistX := -15 to 15 by 1
* Modify test contours
gen_ellipse_contour_xld (Contour1, Row[0] - 125 + DistY, Column[0] + DistX, rad(270), 25, 35, 0, rad(90), 'positive', 1.5)
gen_ellipse_contour_xld (Contour2, Row[1] - 75 + DistY, Column[1] + DistX, rad(0), 35, 25, 0, rad(90), 'positive', 1.5)
gen_contour_nurbs_xld (Contour3, [100,100,100,100] + DistY, Column[2] + [0,10,20,30] + DistX, 'auto', 'auto', 3, 1, 5)
concat_obj (Contour1, Contour2, ContoursConcat)
concat_obj (ContoursConcat, Contour3, ContoursConcat)
concat_obj (ContoursConcat, FixedContours, ContoursConcat)
* Try to unify contours
union_cotangential_contours_xld (ContoursConcat, UnionContours, FitClippingLength, FitLength, MaxTangAngle, MaxDist, MaxDistPerp, -abs(TestMaxOverlap), 'attr_forget')
* Display results
Text := 'MaxOverlap = -' + abs(TestMaxOverlap)
dev_display_results (UnionContours, ContoursConcat, WindowHandle, Title, Text)
wait_seconds (Delay)
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Test settings for the angle between two contours
Title := 'Test influence of MaxTangAngle'
DistX := 10
DistY := 0
hom_mat2d_identity (HomMat2DIdentity)
for Phi := 90 to -90 by -1
* Modify test contours
gen_ellipse_contour_xld (Contour1, Row[0] - 125 + DistY, Column[0] + DistX, rad(270), 25, 35, 0, rad(90), 'positive', 1.5)
hom_mat2d_rotate (HomMat2DIdentity, rad(Phi), Row[0] - 100 + DistY, Column[0] + DistX, HomMat2DRotate)
affine_trans_contour_xld (Contour1, Contour1, HomMat2DRotate)
gen_ellipse_contour_xld (Contour2, Row[1] - 75 + DistY, Column[1] + DistX, rad(0), 35, 25, 0, rad(90), 'positive', 1.5)
hom_mat2d_rotate (HomMat2DIdentity, rad(Phi), Row[1] - 100 + DistY, Column[1] + DistX, HomMat2DRotate)
affine_trans_contour_xld (Contour2, Contour2, HomMat2DRotate)
gen_contour_nurbs_xld (Contour3, [100,100,100,100] + DistY, Column[2] + [0,10,20,30] + DistX, 'auto', 'auto', 3, 1, 5)
hom_mat2d_rotate (HomMat2DIdentity, rad(Phi), Row[2] - 100 + DistY, Column[2] + DistX, HomMat2DRotate)
affine_trans_contour_xld (Contour3, Contour3, HomMat2DRotate)
concat_obj (Contour1, Contour2, ContoursConcat)
concat_obj (ContoursConcat, Contour3, ContoursConcat)
concat_obj (ContoursConcat, FixedContours, ContoursConcat)
* Try to unify contours
union_cotangential_contours_xld (ContoursConcat, UnionContours, FitClippingLength, FitLength, TestMaxTangAngle, MaxDist, MaxDistPerp, TestMaxOverlap, 'attr_forget')
* Display results
Text := 'MaxTangAngle = rad(' + deg(TestMaxTangAngle) + ')'
dev_display_results (UnionContours, ContoursConcat, WindowHandle, Title, Text)
wait_seconds (Delay / 2)
endfor
set_system ('flush_graphic', FlushGraphic)
Halcon 拟合椭圆
* Approximation of XLD contours by ellipses or elliptic arcs
* 产生一个空的图像
gen_image_const (Image, 'byte', 400, 400)
dev_set_color ('green')
* 形成一个椭圆
gen_ellipse (Ellipse, 200, 200, rad(30), 150, 120)
paint_region (Ellipse, Image, ImageEllipse, 255, 'fill')
* 亚像素边缘提取
edges_sub_pix (ImageEllipse, EdgesComplete, 'mderiche2', 0.5, 20, 40)
* First, the easy case:
* 拟合椭圆
fit_ellipse_contour_xld (EdgesComplete, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder)
* Now simulate a fragmented ellipse:
* 产生亚像素轮廓
gen_ellipse_contour_xld (ContEllipse, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder, 1.5)
gen_rectangle2 (Rectangle1, 200, 200, rad(30), 150, 2)
gen_rectangle2 (Rectangle2, 200, 200, rad(120), 150, 2)
union2 (Rectangle1, Rectangle2, Cross)
* 求区域的补给
complement (Cross, CrossComplement)
reduce_domain (ImageEllipse, CrossComplement, ImageEllipseReduced)
edges_sub_pix (ImageEllipseReduced, EdgesIncomplete, 'mderiche2', 0.5, 20, 40)
* Now the difficult case:
* First, make a fit to the individual arcs.
fit_ellipse_contour_xld (EdgesIncomplete, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder)
* Now we would have to determine which elliptic arcs should be merged.
* We assume that the customer is able to do this.
* If not, this could be done by clustering the elliptic arcs based on the centers
* and radii that result from the above fit.
*
* Now we have to determine in which order the arcs should be merged.
* This can be done by sorting them according to their StartPhi.
Order := sort_index(StartPhi)
* Now construct a new contour from the coordinates of the segments to be merged:
RowsMerged := []
ColsMerged := []
for I := 0 to |Order| - 1 by 1
select_obj (EdgesIncomplete, SelectedEdge, Order[I] + 1)
get_contour_xld (SelectedEdge, Rows, Cols)
if (PointOrder[Order[I]] == 'negative')
Rows := inverse(Rows)
Cols := inverse(Cols)
endif
RowsMerged := [RowsMerged,Rows]
ColsMerged := [ColsMerged,Cols]
endfor
dev_set_color ('blue')
gen_contour_polygon_xld (Contour, RowsMerged, ColsMerged)
fit_ellipse_contour_xld (Contour, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder)
dev_set_color ('red')
gen_ellipse_contour_xld (ContEllipseIncomplete, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder, 1.5)
clip_contours_xld 亚像素边缘分割案例
*
* This example program shows how to use clip_contours_xld.
*
*
dev_close_window ()
read_image (Angio, 'angio-part')
get_image_size (Angio, Width, Height)
dev_open_window (0, 0, 3 * Width / 2, 3 * Height / 2, 'black', WindowID)
dev_set_part (0, 0, Height - 1, Width - 1)
*
* Preparation: extract contours from an image.
* 检测线条
lines_gauss (Angio, Lines, 2.3, 0.0, 0.7, 'dark', 'true', 'parabolic', 'true')
dev_display (Angio)
dev_set_color ('yellow')
dev_display (Lines)
stop ()
*
* Now reduce the image domain. The previously extracted contours
* are clipped accordingly (the original contours are displayed in gray).
* Note, that the clipped contours end not at the pixel border,
* but at the last contour point that lies within the pixel.
Top := 200
Left := 100
Bottom := 300
Right := 200
gen_rectangle1 (Rectangle, Top, Left, Bottom, Right)
reduce_domain (Angio, Rectangle, AngioReduced)
* 亚像素边缘分割
clip_contours_xld (Lines, LinesClipped, Top, Left, Bottom, Right)
dev_clear_window ()
dev_display (AngioReduced)
dev_set_color ('dim gray')
dev_display (Lines)
dev_set_color ('yellow')
dev_display (LinesClipped)
stop ()
*
dev_set_color ('white')
disp_arrow (WindowID, 198, 130, 153, 175, 1)
dev_open_window (25, 275, 300, 300, 'black', ZoomWindow)
dev_set_part (198, 126, 202, 130)
dev_display (AngioReduced)
dev_set_color ('dim gray')
dev_display (Lines)
dev_set_color ('yellow')
dev_display (LinesClipped)
stop ()
*
dev_set_part (0, 0, Height - 1, Width - 1)
dev_close_window ()
dev_set_color ('black')
dev_set_line_width (2)
disp_arrow (WindowID, 198, 130, 153, 175, 1)