跟我学机器视觉-HALCON学习例程中文
详解-测量圆环脚宽间距
* This example program demonstrates the basic usage of a circular measure object.
* Here, the task is to determine the width of the cogs.
*首先读取图像,获取图像的宽度和高度
* First, read in the image and initialize the program.
read_image (Image, 'rings_and_nuts')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
get_image_size (Image, Width, Height)
*读到的图像如下:
* Extract the rings.
*******************自动阈值分割,此时为一个区域,如图**************************** bin_threshold (Image, Region)
connection (Region, ConnectedRegions)
***************************填充区域内的孔洞,效果如图************************** fill_up (ConnectedRegions, RegionFillUp)
*******************计算区域的紧性,机器视觉算法与应用第168页***************** compactness (RegionFillUp, Compactness)
************通过紧性特征进行区域筛选,在1.5到2.5之间的为需要测量的圆环******** for i := 0 to |Compactness|-1 by 1
if (Compactness[i] > 1.5 and Compactness[i] < 2.5)
select_obj (RegionFillUp, ObjectSelected, i+1)
*
* Determine the size and position of the rings.
**************计算选择区域的最小外接圆和最大内接圆,存储圆心和半径************** *两个半径的均值作为测量圆弧半径,AnnulusRadius 为测量圆弧宽度******************* smallest_circle (ObjectSelected, Row, Column, RadiusMax)
inner_circle (ObjectSelected, CenterRow, CenterCol, RadiusMin)
Radius := (RadiusMax+RadiusMin)/2.0
AnnulusRadius := (RadiusMax-RadiusMin)/4.0
*
* Determine the position between two cogs.
* This position is then used as the start angle for the circular ROI.
********多边形近似逼近区域,存储多边形角点的坐标值,机器视觉算法与应用第252页get_region_polygon (ObjectSelected, AnnulusRadius, RowsBorder, ColumnsBorder)
********计算每个角点到最大内接圆圆心的距离,然后进行排序********************* SqrDistanceBorder := (RowsBorder-CenterRow)*(RowsBorder-CenterRow) + (ColumnsBorder-CenterCol)*(ColumnsBorder-CenterCol)
****************************************从小到大排序*************************** tuple_sort_index (SqrDistanceBorder, Indices)
****************计算直线的角度,圆心和到圆心最近的多边形角点所确定的直线******** ********************************将该角度作为测量圆弧的起始角度***************** line_orientation (CenterRow, CenterCol, RowsBorder[Indices[0]], ColumnsBorder[Indices[0]], AngleStart)
*************我认为检测圆开始角度直接设为0度也可以*************************** * AngleStart := rad(0)
AngleExtent := rad(360)
* Create the measure for a circular ROI.
Interpolation := 'bilinear'
**********************生成测量圆弧,为完整圆环(360度)************************* gen_measure_arc (CenterRow, CenterCol, Radius, AngleStart, AngleExtent, AnnulusRadius, Width, Height, Interpolation, MeasureHandle)
*
* Determine all edge pairs that have a negative transition, i.e., edge pairs
* that enclose dark regions.
* Note that the output parameters IntraDistance and InterDistance are given as arc lengths.
Sigma := 1.0
Threshold := 30
Transition := 'negative'
Select := 'all'
********进行边缘对测量,RowEdgeFirst, ColumnEdgeFirst,RowEdgeSecond, ********ColumnEdgeSecond为检测圆弧检测到的边界的中心点的坐标,IntraDistance, ********InterDistance分别为边缘对间的距离和边缘对的弧线距离********************* ********如有疑问,请咨询qiuqiu:1613985351****************************** measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
*
* Determine the number of cogs.
NumCogs := |RowEdgeFirst|
*
* Determine the linear distance between the two edges of each edge pair ('Linear cog size')
* as well as the angular distance of the edge pairs ('Angular cog size').
**************************计算边缘对的直线距离和弧度*************************** distance_pp (RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond, LinearDistance)
AngularDistance := deg(IntraDistance/Radius)
Visualize the determined edges.
******************将边缘对的起始终止点画出,并显示测量结果**********************
gen_empty_obj (Crosses)
for i := 0 to |RowEdgeFirst|-1 by 1
gen_cross_contour_xld (Cross, RowEdgeFirst[i], ColumnEdgeFirst[i], AnnulusRadius*2.0, atan2(RowEdgeFirst[i]-CenterRow,ColumnEdgeFirst[i]-CenterCol)) concat_obj (Crosses, Cross, Crosses)
gen_cross_contour_xld (Cross, RowEdgeSecond[i], ColumnEdgeSecond[i], AnnulusRadius*2.0, atan2(RowEdgeSecond[i]-CenterRow,ColumnEdgeSecond[i]-CenterCol)) concat_obj (Crosses, Cross, Crosses)
endfor
dev_display (Image)
dev_set_line_width (4)
dev_set_color ('black')
dev_display (Crosses)
dev_set_line_width (1)
dev_set_color ('white')
dev_display (Crosses)
*
* Display the measured size of the cogs.
disp_message (WindowHandle, 'Number of cogs: '+|RowEdgeFirst|+' ', 'window', 260, 10, 'black', 'true')
disp_message (WindowHandle, 'Mean cog size: ', 'window', 286, 10, 'black', 'true')
disp_message (WindowHandle, '- Arc length: '+mean(IntraDistance)$'.2f'+' +/- '+deviation(IntraDistance)$'.2f'+' pixel', 'window', 310, 10, 'black', 'true')
disp_message (WindowHandle, '- Linear: '+mean(LinearDistance)$'.2f'+' +/- '+deviation(LinearDistance)$'.2f'+' pixel', 'window', 334, 10, 'black', 'true')
disp_message (WindowHandle, '- Angular: '+mean(AngularDistance)$'.2f'+' +/- '+deviation(AngularDistance)$'.2f'+' deg ', 'window', 358, 10, 'black', 'true')
*
* Close the measure
close_measure (MeasureHandle)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
***************************for循环,测量其他圆环******************************** endfor
基于HALCON的机器视觉系统的研究与实现 近年来,机器视觉系统以其高效率、高可靠、低成本的特点在国外取得了广泛的应用。机器视觉系统适用于众多领域,例如工业自动化、医药业、制造业、农业等,弥补了人类视觉的很多不足。本文采用德国MVTec公司的专业机器视觉软件HALCON来开发机器视觉系统,提出了相关机器视觉实现方法,并且在机器视觉实验平台上完成了一个弹簧片检测任务。 目前关注较多的是机器视觉系统的硬件部分,而机器视觉软件部分关注较少,一个先进的机器视觉系统除了具有高性能的硬件外,还需要有高性能的软件,虽然说许多常见的开发软件例如Mircosoft的Visual Studio、NI的LabWindows\CVI等等都可以开发机器视觉系统,但是开发周期比较长,针对性较弱,程序的复杂程度较高。而采用HALCON作为机器视觉和图像处理核心软件,不仅大大缩短了开发周期,降低了开发难度,而且可以参考HALCON提供的众多机器视觉和图像处理例程来针对具体的任务做具体开发。 文章的第一章研究了机器视觉系统的组成、应用现状和发展,并且对机器视觉软件HALCON做了概述。第二章根据相关要求,选择合适的硬件单元,设计和搭建了VS-ZM1200机器视觉实验平台。第三章研究了机器视觉中常用的一些图像处理技术,重点讨论了在弹簧片检测任务中所采用的图像处理技术和算法,如图像的增强,分割,边缘检测等。第四章研究了机器视觉软件,重点研究了HALCON,并且对在Visual C++开发环境下如何使用HALCON编写的程序做了讨论。第五章介绍了在VS-ZM1200机器视觉实验平台上,使用HALCON和Visual C++开发的一套弹簧片检测系统,该系统完成关于弹簧片的尺寸参数测量和外观参数判别的任务。
跟我学机器视觉-HALCON学习例程中文 详解-测量圆环脚宽间距 * This example program demonstrates the basic usage of a circular measure object. * Here, the task is to determine the width of the cogs. *首先读取图像,获取图像的宽度和高度 * First, read in the image and initialize the program. read_image (Image, 'rings_and_nuts') dev_close_window () dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle) set_display_font (WindowHandle, 14, 'mono', 'true', 'false') get_image_size (Image, Width, Height) *读到的图像如下: * Extract the rings. *******************自动阈值分割,此时为一个区域,如图**************************** bin_threshold (Image, Region)
connection (Region, ConnectedRegions)
***************************填充区域内的孔洞,效果如图************************** fill_up (ConnectedRegions, RegionFillUp)