机器视觉 实验指导书 2010-2011-2(王海晖)

萌到你眼炸
844次浏览
2021年02月12日 20:52
最佳经验
本文由作者推荐

-

2021年2月12日发(作者:夏日天空战袍)


《机器视觉》实验指导书




实验一





电子元件插针引脚测量实验




(一)


实验类型:


验证性实验




(二)


实验目的:


< br>通过该实验使得学生掌握如何建立被测量工件对象的基本检测步骤,


包括工件定位 、


光


源调整、


焦距调节等图象测量,< /p>


进而掌握工件尺寸测量可视化组态编程方法和技术,


掌握如


何对多插针相关尺寸参数同时测量的新方法,


建立被测量插针的基本检 测框架,


进而扎实掌


握插件多针尺寸、


针间距及针宽度测量新技术,


具备解决实际复杂插件多针参数视觉测量的


技术难题。





(三)


实验要求:


< br>熟练掌握:用


Halcon


平台测量的步骤




学生分组人数:


1


人


/


组




(四)


实验内容:



【实验内容】



测量如图


1-1


所示电子插件插针参数尺寸






(a)





























(b)


图


1-1


:


开


关




(


a


)


需要测量开关引脚的宽度及相互之间的距离。


(

b


)确定检测边缘的矩形感兴趣区。




【实验原理】


1


、单个相机的影像截取控制流程如


a)

< br>所示,



第



1


页



共



20


页





实验中,采用单相机采集图像,对应的图像处理算子类型为:



open_framegrabber (Name, 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'default', Board0, -1, -1, FGHandle0)



grab_image (Image0, FGHandle0)




2


、算子说明:


1


)


open_framegrabber


函


数


功


能

< br>:


打


开


并


设


置


一


个


图


像


采


集


器



调


用


格


式


:


open_framegrabber ( : :


Name


, HorizontalResolution, VerticalResolution, ImageWidth, ImageHeight,


StartRow, StartColumn, Field, BitsPerChannel, ColorSpace,


Generic


, ExternalTrigger, CameraType, Device, Port,


LineIn :


FGHandle


)


参

< p>
数


:


Name--Halcon

< br>图


像


采


集


接


口


。


比


如


:


对


应


的


DLL(Windows)


的


名

< p>
字


或


者


共


享


的


库


函

数


(


UNIX


)

< br>的


名


字









Gerneric--

< p>
图


像


增


益


数


,


可


以

控


制


采


集


到


的


图


像


亮< /p>


度









FGHandle--


图


像


采


集


函


数

句


柄




2


)


grab_image


函数功能:采集图像



调用格式:


grab_image ( : Image : FGHandle : )



参数:


Image----


采集图像的名称








FGHandle--


帧采集器句柄



===================


采集图像部分的程序 实例


====================


1


)



dev_close_window ()


open_framegrabber


('PXC',


1,


1,


0,


0,


0,


0,


'default',


8,


'default',


-1,


'default',


'AUTO',


'default',


-1,


1,


FGHandle)


grab_image (Image, FGHandle)


get_image_pointer1 (Image, Pointer, Type, Width, Height)


dev_open_window (0, 0, Width, Height, 'black', WindowHandle)


2


)



第



2


页



共



20


页



dev_update_window ('off')


open_framegrabber


('DT315x',


2,


2,


0,


0,


0,


0,


'default',


-1,


'default',


-1,


'default',


'default',


'default',


-1,


1,


FGHandle)


grab_image (Image, FGHandle)


get_image_pointer1 (Image, Pointer, Type, Width, Height)


dev_close_window ()


dev_open_window (0, 0, Width, Height, 'black', WindowID)



【实验步骤】



1

、工件定位:将工件放置到平台转盘上相应位置,确保动态图象采集根据被测对象要求。


2


、光源调整:选择前光源或背光源确保图象不受自然光 源影响。



3


、焦距调节:根据被测对 象大小和厚度,调节支架横枞位置和相机焦距。



4

< p>
、打开图像处理软件,通过变量输入窗口、源程序窗口、菜单栏算子三种方式添加算子,

< p>
编写工件尺寸的测量与标定程序。


其中,


相机采集 部分的算子可借助菜单栏


“助手”


—


“


open


new Image acquisition


”


设置,


生成代码。


(参见前一部分


《图像处理软件操作》


)

,


如


MV- VS860


平台中相机的代码:



open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', 'MV One


Video Capture', 0, -1, FGHandle)//


采集卡初始化设置



set_framegrabber_param (FGHandle, 'saturation', 0)//


设置采集卡参数



while (true)//


循环采集



grab_image (Image, FGHandle)


*



Do something



//


在这里添加算法步骤,对


Image


,即原始图像做处理



endwhile


close_framegrabber (FGHandle)


主要算法步骤:



-----------------------







1)


通过包含如图


1-1(b)

< p>
描述引脚的


ROI


矩形创建测量对象。

< p>
ROI


矩形用以取得垂直于矩


形主轴的各个直线边 界对。运用算子


gen_measure_


rectangl e2


返回所创建对象的句柄


MeasureHandle


。



gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2,Interpolation, MeasureHandle)



------------------------


2)< /p>


将句柄


MeasureHandle


传寄 给测量函数进行测量。


用


measure_pairs


算子检测


,


该算子能提


取出引脚对应的边缘对并返回其宽度


(


每组边缘对的两边之间 的距,


IntraDistance)


和距离

< br>(


连


续两个边缘对之间的距离,


InterDistance)


。



第



3


页



共



20


页



measure_pairs


(Image,


MeasureHandle,


Sigma,


Threshold,


Transition,


Select,RowEdgeFirst,


ColumnEdgeFirst,


AmplitudeFirst,RowEdgeSecond,


ColumnEdgeSecond,


AmplitudeSecond,IntraDistance, InterDistance)


------------------------


要测量引脚的宽度及其之间的距离。


用一维测量,


这个任务可迎刃而解,因为位置、距离都


是沿着一条线测量的。



============================

< p>
参考程序


============================= ==


open_framegrabber('DirectShow',1,1,0 ,0,0,0,'default',-1,'gray',-1,'default','default', default',-1,-1,FG


Handle)













//


打开图像采集设备


, Direc tShow


即为相机的参数


,'gray'

表示黑白相机



set_framegrabber_param (FGHandle, 'saturation', 0)//


设置采集卡参数



while (true)//


循环采集



grab_image (Image, FGHandle)













//


抓取图像












read_image (Image, 'C:/Templ')




//< /p>


读取图像,因


grab_image


抓取 的图像保存在


C:/Templ


中



get_image_pointer1 (Image, Pointer, Type, Width, Height)



//


获得图像参数



dev_close_window ()


dev_open_window (0, 0, Width, Height, 'black', WindowHandle) //


打开图像显示窗口,

< p>
设置背景


色为黑色



dev_display (Image)



//


显示图像



Row := 390


Column := 380//


定义


ROI


矩形的


R/C


坐标



Phi := rad(-60) //


矩形偏移水平位置的角度,逆时针为正,顺时针为负



Length1 := 60


Length2 := 10




//


定义


ROI


矩形长度及宽度



Interpolation := 'nearest_neighbor'



//


定义被检测对象的相互关系



gen_measure_rectangle2


(Row,


Column,


Phi,


Length1,


Length2,


Width,


Height,


Interpolation,


MeasureHandle)



/ /


创建


ROI


矩形


Sigma := 0.9


Threshold := 12


Transition := 'negative'


Select := 'all'


measure_pairs


(Image,


MeasureHandle,


Sigma,


Threshold,


Transition,


Select,


RowEdgeFirst,


ColumnEdgeFirst,


AmplitudeFirst,


RowEdgeSecond,


ColumnEdgeSecond,


AmplitudeSecond,


IntraDistance, InterDistance) //


在指定


ROI


矩形区域内,检测符合

< br>Threshold := 12


的所有边缘


对



第



4


页



共



20


页



dev_display (Image)


dev_set_draw ('margin')//


设置图形的显示方式



dev_set_color ('black')//


设置字体的颜色



gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)//


在显示窗口中输出矩形



p_disp_dimensions


(RowEdgeFirst,


ColumnEdgeFirst,


RowEdgeSecond,


ColumnEdgeSecond,


IntraDistance, InterDistance, Phi, Length2, WindowHandle)



//


测量结果显示函数



close_measure (MeasureHandle)//


清除与测量相关的缓存文件



endwhile


close_framegrabber (FGHandle)//


关闭图像采集设备




5


、电子插件参数实验测量结果如图


1-2


所示:标示了引脚的边缘,宽度及其之间的距离。





图

< p>
1-2


:开关引脚宽度及相互距离检测结果




参考例程:


examplessol ution_guide1d_measuringmeasure_


。



6


、延伸训练




图


1-3



a)


测


引


脚< /p>


的


宽


度


以


及


各


个


引

< p>
脚


的


间


距





b)

< br>测


引


脚


的


长


度




这 个训练的任务是检测芯片的主要尺寸


(


见图

1-3)


。




主要算法步骤:



第



5


页



共



20


页



------------------------


1)< /p>


检测每个引脚的宽度以及各个引脚的间距(更加重要)


。我们定义 一个包含引脚的矩形


ROI(


见



图


1-3a),


这就产生了测量目标。









gen_measure_rectangle2


(Row,


Column,


Phi,


Length1,


Length2,


Width,


eight,’nearest_neighbor’, MeasureHandle)



measure_pairs (


Image, MeasureHandle, 1.5, 30, ’negative’, ’all’,RowEdgeFirst, ColumnEdgeFirst,


AmplitudeFirst,


PinDistance)


通过取出的各个直线边界对,就可以得到引脚数目、平均宽度和他们的平均间距



numPins := |PinWidth|


avgPinWidth := sum(PinWidth)/|PinWidth|


avgPinDistance := sum(PinDistance)/|PinDistance|



2)


确定引脚的长度。尽管每个引脚的宽度都只有几个象素,但还是可以求出他 们的长度。为


了完成这个任务,


我们画出了一个包含了芯片两个 对边引脚的矩形,


从而得到了一个新的测


量区域


(


见图


1-3b)


。找到的第 一条边和第二条边之间的距离就是上侧引脚的长度,第三条和


第四条就是下面引脚的长度 。



gen_measure_rectangle2


(Row,


Column,


Phi,


Length1,


Length2,


Width,


RowEdgeSecond,ColumnEdgeSecond,


AmplitudeSecond,


PinWidth,


Height,’nearest_neighbor’, MeasureHandle)



measure_pos


(Image,


MeasureHandle,


1.5,


30,


’all’,


’all’,


RowEdge,ColumnEdge,


Amplitude,


Distance)


参考例程


: exampleshdevelopApplicationsMeasuremeasure


============================


参考程序


===============================


dev_close_window ()


read_image (Image, 'ic_pin')


get_image_pointer1 (Image, Pointer, Type, Width, Height)


dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)


dev_display (Image)


stop ()


draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)//


创建矩形,各参 数定


义如下。



Row := 47 //


矩形中心点


Row


值



Column := 485


Phi := 0 //


矩形偏移水平位置的角度,逆时针为正,顺时针为负



第



6


页



共



20


页



Length1 := 420 //


矩形长度



Length2 := 10 //


矩形宽度



dev_set_color ('green')


dev_set_draw ('margin')


dev_set_line_width (3)


gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)


gen_measure_rectangle2


(Row,


Column,


Phi,


Length1,


Length2,


Width,


Height,


'nearest_neighbor', MeasureHandle)


stop ()


dev_update_pc ('off')


dev_update_var ('off')


n := 100


count_seconds (Seconds1)


for i := 1 to n by 1






measure_pairs


(Image,


MeasureHandle,


1.5,


30,


'negative',


'all',


RowEdgeFirst,


ColumnEdgeFirst,


AmplitudeFirst,


RowEdgeSecond,


ColumnEdgeSecond,


AmplitudeSecond,


PinWidth, PinDistance)


endfor


count_seconds (Seconds2)


Time := Seconds2-Seconds1


stop ()


dev_set_color ('red')


disp_line


(WindowHandle,


RowEdgeFirst,


ColumnEdgeFirst,


RowEdgeSecond,


ColumnEdgeSecond)


avgPinWidth := sum(PinWidth)/|PinWidth|


avgPinDistance := sum(PinDistance)/|PinDistance|


numPins := |PinWidth|


OpSystem := environment('OS')


if (OpSystem='Windows_NT')






set_font (WindowHandle, '-Arial-18-*-*-*-*-*-')


else






set_font (WindowHandle, '-*-courier-*-*-*-*-18-*-*-*-*-*-*-*')


endif


dev_set_color ('yellow')


第



7


页



共



20


页



set_tposition (WindowHandle, 200, 100) //


设置文字输入位置



write_string (WindowHandle, 'Number of pins: '+numPins)//


显示测量数据:


Number of pins


set_tposition (WindowHandle, 260, 100)


write_string (WindowHandle, 'Average Pin Width:



'+avgPinWidth)


set_tposition (WindowHandle, 320, 100)


write_string (WindowHandle, 'Average Pin Distance:



'+avgPinDistance)


dump_window (WindowHandle, 'tiff_rgb', 'C:Temppins_result')


stop ()


draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)


Row1 := 0


Column1 := 600


Row2 := 100


Column2 := 700


dev_set_color ('blue')


disp_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)


stop ()


dev_set_part (Row1, Column1, Row2, Column2)


dev_display (Image)


dev_set_color ('green')


dev_display (Rectangle)


dev_set_color ('red')


disp_line


(WindowHandle,


RowEdgeFirst,


ColumnEdgeFirst,


ColumnEdgeSecond)


stop ()


close_measure (MeasureHandle)


dev_set_part (0, 0, Height-1, Width-1)


dev_display (Image)


stop ()


dev_set_color ('green')


draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)


Row := 508


Column := 200


Phi := -1.5708


Length1 := 482


第



8


页



共



20


页



RowEdgeSecond,


Length2 := 35


gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)


gen_measure_rectangle2


(Row,


Column,


Phi,


Length1,


Length2,


Width,


Height,


'nearest_neighbor', MeasureHandle)


stop ()


measure_pos


(Image,


MeasureHandle,


1.5,


30,


'all',


'all',


RowEdge,


ColumnEdge,


Amplitude,


Distance)


PinHeight1 := RowEdge[1]-RowEdge[0]


PinHeight2 := RowEdge[3]-RowEdge[2]


dev_set_color ('red')


disp_line (WindowHandle, RowEdge, ColumnEdge-Length2, RowEdge, ColumnEdge+Length2)


dev_set_color ('yellow')


set_tposition (WindowHandle, RowEdge[1]+40, ColumnEdge[1]+100)


write_string (WindowHandle, 'Pin Height:



'+PinHeight1)


set_tposition (WindowHandle, RowEdge[3]-120, ColumnEdge[3]+100)


write_string (WindowHandle, 'Pin Height:



'+PinHeight2)


dump_window (WindowHandle, 'tiff_rgb', 'C:Temppins_height_result')


close_measure (MeasureHandle)


dev_set_draw ('fill')


dev_set_line_width (1)




第



9


页



共



20


页



实验二







线弧测量及定标实验




(一)


实验类型


:验证性实验




(二)


实验目的


:



通过该实验使得学生掌握如何建立线弧类工件的基 本检测步骤,


如何将轮廓分成线段和


(圆)弧以及如何测定相关 的参数。具备解决实际工件参数(如长、宽、高)视觉测量的问


题。进而扎实掌握工件边 缘长度测量、圆弧测量以及线段与圆弧混合测量等新技术。




(三)


实验要求


:


熟练掌握:用


Halcon


测量 弧线及标定算法




学生分组人数:< /p>


1


人


/


组





(四)


实验内容


:



测量包 括工件顶端凸圆半径、凸缘端两耳朵圆孔半径、两切线长度、中心小孔半径、矩形长


度和 宽度,工件图像如图


2-1


所示。




图


2-1



待测工件



【实验原理】



1

、线弧类工件的测量,需采用近似匹配的方法。



2


、亚像素(


Sub-Pixel


)定义:摄像机的成像面的分辨率以像素数量来衡量。但像素中


心之 间的距离有几个至十几个微米不等。


为了最大限度利用图像信息来提高分辨率,


提出了


Sub-Pixel


概念。意思是说,在 两个物理像素之间还有像素,称之为


Sub-Pixel


,它完 全是通


过计算方法的出来的。



3


、算子及函数说明:



1


)


edges_sub_pix < /p>


函数功能:用


Deriche,Lanser,Shen,


或者


Canny


算法进行亚像素级边缘提取。



调用格式:


edges_sub_pix( Image : Edges : Filter, Alpha, Low, High : )


参数 :


Image--


输入图像









Edges--


得到的图像边缘



第



10


页



共



20


页


-


-


-


-


-


-


-


-