0.hom_mat2d_identity( : : : HomMat2DIdentity)
hom_mat2d_identity生成描述相同二维变换的齐次变换矩阵HomMat2DIdentity
工业视觉二维旋转变换基础函数介绍
1.affine_trans_pixel( : : HomMat2D, Row, Col : RowTrans, ColTrans)
affine_trans_pixel对输入像素(Row, Col )应用任意仿射二维变换,
即缩放、旋转、平移和倾斜,并返回结果像素(RowTrans, ColTrans);
输入和输出像素是亚像素精度坐标。仿射变换用hommat2d给出的齐次变换矩阵来描述。
与 affine_trans_point_2d不同,
affine_trans_pixel首先将输入坐标从Halcon的标准坐标系(原点位于左上像素的中心)转换为原点位于左上像素左上角的坐标系。
使用hommat2d进行转换后,结果将转换回标准坐标系。
这样,affine_trans_pixel 就和
affine_trans_image,
affine_trans_image_size,
affine_trans_region,
affine_trans_contour_xld,
affine_trans_polygon_xld 兼容。
affine_trans_pixel对应于以下变换链(输入和输出像素作为齐次向量):
工业视觉二维旋转变换基础函数介绍
所以
affine_trans_pixel (HomMat2D, Row, Col, RowTrans, ColTrans)
等同于下面操作
affine_trans_point_2d (HomMat2D, Row+0.5, Col+0.5, RowTmp, ColTmp)
RowTrans := RowTmp-0.5
ColTrans := ColTmp-0.5
2.hom_mat2d_translate( : : HomMat2D, Tx, Ty : HomMat2DTranslate)
hom mat2d_translate将矢量t=(tx,ty)的转换添加到齐次二维转换矩阵hommat2d,并返回hommat2dtranse中。
转换是相对于全局(即固定)坐标系执行的;这对应于以下转换矩阵链:
工业视觉二维旋转变换基础函数介绍
要在局部坐标系(即HomMat2D描述的坐标系)中执行转换,
请使用hom_mat2d_translate_local。
3.hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate)
hom_mat2d_rotate将角度phi的旋转添加到齐次二维变换矩阵hommat2d,并返回HomMat2DRotate中。
旋转由2×2旋转矩阵R描述。
它是相对于全局(即固定)坐标系执行的;
这对应于以下变换矩阵链:
工业视觉二维旋转变换基础函数介绍
点(px,py)是转换的固定点,即使用hommat2drotate转换时,该点保持不变。
为了获得这种行为,首先在输入变换矩阵中添加一个平移,
将固定点移动到全局坐标系的原点上。
然后,添加旋转,最后是将固定点移回其原始位置的平移。
这对应于以下转换链:
工业视觉二维旋转变换基础函数介绍
要在局部坐标系(即HomMat2D描述的坐标系)中执行转换,
请使用hom_mat2d_rotate_local。
上面函数的例程:
例程实现的功能是对IC引脚进行测量。
例程使用形状模板匹配查找测量对象,
再将查找到的测量对象旋转平移到固定位置进行一维测量。
F5执行例程,例程不断旋转,进行模板匹配与旋转平移变换,
点击鼠标 右键可以停止循环,结果测量。
执行结果如图:
工业视觉二维旋转变换基础函数介绍
下面是例程的代码:
- dev_update_pc ('off')
- dev_update_window ('off')
- dev_update_var ('off')
- open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'board/board.seq', 'default', -1, 1, FGHandle)
- grab_image (Image, FGHandle)
- get_image_size (Image, Width, Height)
- dev_close_window ()
- dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
- dev_open_window (Height + 70, 0, Width, 120, 'black', WindowHandleText)
- dev_set_window (WindowHandle)
- dev_set_color ('red')
- dev_display (Image)
- Row1 := 188
- Column1 := 182
- Row2 := 298
- Column2 := 412
- *打印标识区域
- gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
- area_center (Rectangle, Area, Row, Column)
- Rect1Row := -102
- Rect1Col := 5
- Rect2Row := 107
- Rect2Col := 5
- RectPhi := 0
- RectLength1 := 170
- RectLength2 := 5
- *要检测两边的管脚区域
- gen_rectangle2 (Rectangle1, Row + Rect1Row, Column + Rect1Col, RectPhi, RectLength1, RectLength2)
- gen_rectangle2 (Rectangle2, Row + Rect2Row, Column + Rect2Col, RectPhi, RectLength1, RectLength2)
- reduce_domain (Image, Rectangle, ImageReduced)
- *创建形状模板用于查询打印标识
- create_shape_model (ImageReduced, 4, 0, rad(360), rad(1), 'none', 'use_polarity', 30, 10, ModelID)
- get_shape_model_contours (ShapeModel, ModelID, 1)
- *生成齐次变换矩阵
- hom_mat2d_identity (HomMat2DIdentity)
- hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
- *将轮廓移动到打印标识中心
- affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DTranslate)
- *显示测量相关对象
- dev_display (Image)
- dev_set_color ('green')
- dev_display (ShapeModelTrans)
- dev_set_color ('blue')
- dev_set_draw ('margin')
- dev_set_line_width (3)
- dev_display (Rectangle1)
- dev_display (Rectangle2)
- dev_set_draw ('fill')
- dev_set_line_width (1)
- dev_set_color ('yellow')
- disp_message (WindowHandle, ['Press left button to start','and stop the demo'], 'window', 12, 12, 'black', 'true')
- get_mbutton (WindowHandle, Row3, Column3, Button1)
- wait_seconds (0.5)
- Button := 0
- while (Button != 1)
- dev_set_window (WindowHandle)
- dev_set_part (0, 0, Height - 1, Width - 1)
- grab_image (ImageCheck, FGHandle)
- dev_display (ImageCheck)
- count_seconds (S1)
- *在图片中查找打印标识
- find_shape_model (ImageCheck, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 4, 0.7, RowCheck, ColumnCheck, AngleCheck, Score)
- count_seconds (S2)
- dev_display (ImageCheck)
- if (|Score| > 0)
- dev_set_color ('green')
- hom_mat2d_identity (HomMat2DIdentity)
- hom_mat2d_translate (HomMat2DIdentity, RowCheck, ColumnCheck, HomMat2DTranslate)
- hom_mat2d_rotate (HomMat2DTranslate, AngleCheck, RowCheck, ColumnCheck, HomMat2DRotate)
- *将找到的轮廓进行平移和旋转
- affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DRotate)
- dev_display (ShapeModelTrans)
- *将测量矩形中心的像素点进行旋转平移
- affine_trans_pixel (HomMat2DRotate, Rect1Row, Rect1Col, Rect1RowCheck, Rect1ColCheck)
- affine_trans_pixel (HomMat2DRotate, Rect2Row, Rect2Col, Rect2RowCheck, Rect2ColCheck)
- gen_rectangle2 (Rectangle1Check, Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2)
- gen_rectangle2 (Rectangle2Check, Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2)
- dev_set_color ('blue')
- dev_set_draw ('margin')
- dev_set_line_width (3)
- dev_display (Rectangle1Check)
- dev_display (Rectangle2Check)
- dev_set_draw ('fill')
- count_seconds (S3)
- *在指定区域开始测量
- gen_measure_rectangle2 (Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)
- gen_measure_rectangle2 (Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle2)
- measure_pairs (ImageCheck, MeasureHandle1, 2, 90, 'positive', 'all', RowEdgeFirst1, ColumnEdgeFirst1, AmplitudeFirst1, RowEdgeSecond1, ColumnEdgeSecond1, AmplitudeSecond1, IntraDistance1, InterDistance1)
- measure_pairs (ImageCheck, MeasureHandle2, 2, 90, 'positive', 'all', RowEdgeFirst2, ColumnEdgeFirst2, AmplitudeFirst2, RowEdgeSecond2, ColumnEdgeSecond2, AmplitudeSecond2, IntraDistance2, InterDistance2)
- close_measure (MeasureHandle1)
- close_measure (MeasureHandle2)
- count_seconds (S4)
- dev_set_color ('red')
- disp_line (WindowHandle, RowEdgeFirst1 - RectLength2 * cos(AngleCheck), ColumnEdgeFirst1 - RectLength2 * sin(AngleCheck), RowEdgeFirst1 + RectLength2 * cos(AngleCheck), ColumnEdgeFirst1 + RectLength2 * sin(AngleCheck))
- disp_line (WindowHandle, RowEdgeSecond1 - RectLength2 * cos(AngleCheck), ColumnEdgeSecond1 - RectLength2 * sin(AngleCheck), RowEdgeSecond1 + RectLength2 * cos(AngleCheck), ColumnEdgeSecond1 + RectLength2 * sin(AngleCheck))
- disp_line (WindowHandle, RowEdgeFirst2 - RectLength2 * cos(AngleCheck), ColumnEdgeFirst2 - RectLength2 * sin(AngleCheck), RowEdgeFirst2 + RectLength2 * cos(AngleCheck), ColumnEdgeFirst2 + RectLength2 * sin(AngleCheck))
- disp_line (WindowHandle, RowEdgeSecond2 - RectLength2 * cos(AngleCheck), ColumnEdgeSecond2 - RectLength2 * sin(AngleCheck), RowEdgeSecond2 + RectLength2 * cos(AngleCheck), ColumnEdgeSecond2 + RectLength2 * sin(AngleCheck))
- dev_set_line_width (1)
- NumLeads := |IntraDistance1| + |IntraDistance2|
- MinDistance := min([InterDistance1,InterDistance2])
- dev_set_window (WindowHandleText)
- dev_set_part (0, 0, 119, Width - 1)
- dev_clear_window ()
- disp_message (WindowHandleText, 'Matching: Time: ' + ((S2 - S1) * 1000)
- 5.2f' + 'ms , Score: ' + Score
- 7.5f', 'image', 20, 20, 'green', 'false')
- disp_message (WindowHandleText, 'Measure: Time: ' + ((S4 - S3) * 1000)
- 5.2f' + ' ms, Num. leads: ' + NumLeads
- 2d', 'image', 50, 20, 'red', 'false')
- disp_message (WindowHandleText, ' Min. lead dist: ' + MinDistance
- 6.3f', 'image', 80, 20, 'red', 'false')
- endif
- dev_error_var (Error, 1)
- dev_set_check ('~give_error')
- get_mposition (WindowHandle, R, C, Button)
- dev_error_var (Error, 0)
- dev_set_check ('give_error')
- if (Error != H_MSG_TRUE)
- Button := 0
- endif
- endwhile
- dev_set_window (WindowHandleText)
- dev_close_window ()
- clear_shape_model (ModelID)
- close_framegrabber (FGHandle)
复制代码
|