(halcon实例)halcon 3d_coordinates 测量世界坐标中的倾斜物体(已详细注释)


*
* Initialize the program
dev_close_window ()    //关闭图形窗口
dev_open_window (0, 0, 768, 576, 'black', WindowHandle)   //打开一个原点在(0,0)的宽为768,高为576,背景为黑色的图形窗口;
dev_update_off ()
dev_set_draw ('margin')   //定义区域的填充模式,margin显示边缘,fill填充全部区域

dev_set_line_width (3)     //定义线宽为3
set_display_font (WindowHandle, 14, 'mono', 'true', 'false') //设置显示字体(窗口句柄,字体大小,字体,是否加粗,是否斜体)

*
* Calibrate the camera
*
CalTabDescrFile := 'caltab_big.descr'   //标定板描述文件
StartCamPar := [0.008,0,0.0000086,0.0000086,384,288,768,576] //相机参数(焦距,畸变,像素尺寸宽,像素尺寸高,中心点坐标宽,中心点坐标高,图片宽,图片高)
create_calib_data ('calibration_object', 1, 1, CalibDataID)  //创建标定数据模型,即标定的数据将存放于CalibDataID中。
set_calib_data_cam_param (CalibDataID, 0, 'area_scan_division', StartCamPar) //设定相机的初始化参数(标定数据模型句柄,相机索引,相机类型,相机参数)
set_calib_data_calib_object (CalibDataID, 0, CalTabDescrFile) //在标定模型中设定标定对象(对象句柄,标定对象索引,3D标定板文件名)
NumImages := 10
for I := 1 to NumImages by 1
    read_image (Image, 'calib/calib-3d-coord-' + I$'02d') //读取图像(输出对象,文件名)
    dev_display (Image)//显示图片,类似matlab中的imshow
    Message := 'Find calibration plate in\nall calibration images (' + I + '/' + NumImages + ')'//
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')//显示消息(窗口句柄,要显示的字符,当前的坐标系统,窗口显示的起始坐标,字体颜色,是否要显示白色底纹)
    * Find the calibration plate
    find_calib_object (Image, CalibDataID, 0, 0, I - 1, [], [])//(输入图像,标定数据模型句柄,相机索引,标定对象索引,标定对象位置索引,通用参数名称,通用参数值)
    get_calib_data (CalibDataID, 'camera', 0, 'init_params', StartCamPar)//获取标定数据(标定数据模型句柄,标定数据项的类型,影响项目索引,查询数据名称,响应数据)(依靠索引号和数据名称来返回输出的数据值。可查询与模型相关的数据,与相机相关的数据(包括相机的内外参数),与标定对象相关的数据,与标定对象的姿态相关的数据。控制输出是要查询的标定数据)
    get_calib_data_observ_points (CalibDataID, 0, 0, I - 1, Row, Column, Index, Pose)//获取观察点数据(标定数据模型句柄,相机索引,标定对象索引,位姿索引,检测点行号,检测点列号,检测点对应到标定对象观察点索引,粗略的位姿数据(相对相机))
    get_calib_data_observ_contours (Contours, CalibDataID, 'caltab', 0, 0, I - 1)//获取校准数据轮廓(输出轮廓,标定数据模型句柄,轮廓名,相机索引,标定对象索引,位姿索引)
    gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)//为每个输入点生成一个十字形(生成XLD轮廓,输入点集行号,输入点集列号,十字形长度,十字形方向)
    dev_set_color ('green')//用于定义显示区域颜色,这个设置到下一个dev_set_color才会被改变;
    dev_display (Contours)//显示对象
    dev_set_color ('yellow')//
    dev_display (Cross)//
stop()
endfor
disp_continue_message (WindowHandle, 'black', 'true')// 等待用户操作是否继续
stop ()
calibrate_cameras (CalibDataID, Error)// 通过最小化过程确定相机的参数
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)//
*
* Perform measurements
*
for I := 1 to NumImages by 1
    read_image (Image, 'calib/calib-3d-coord-' + I$'02d') //
    *
    * Now, measure the size of the black border of the plate
    *用矩形的形状创建一个XLD轮廓,依次为矩形的XLD轮廓,中心的行坐标,矩形的中心坐标,矩形的主轴的方向
    get_measure_positions (Image, PlateRegion, CalibDataID, I, Distance, Phi, RowCenter, ColumnCenter)//
    gen_rectangle2_contour_xld (Rectangle, RowCenter, ColumnCenter, Phi, Distance * 0.52, 8)//
    gen_measure_rectangle2 (RowCenter, ColumnCenter, Phi, Distance * 0.52, 8, 768, 576, 'nearest_neighbor', MeasureHandle)//产生测量句柄,准备提取和矩形主轴垂直的直边缘
    *提取垂直于矩形或环形弧的直边共18个点
    measure_pos (Image, MeasureHandle, 1, 40, 'all', 'all', RowEdge, ColumnEdge, Amplitude, Distance1)//提取和矩形或弧垂直的边缘线
    close_measure (MeasureHandle)//
    Rows := [RowEdge[0],RowEdge[|RowEdge| - 1]]//
    Columns := [ColumnEdge[0],ColumnEdge[|RowEdge| - 1]]//
    gen_cross_contour_xld (Cross, Rows, Columns, 16, Phi)//
    
    * 将两个边界点转换为世界坐标系
    get_calib_data (CalibDataID, 'calib_obj_pose', [0,I - 1], 'pose', Pose)
    *将图像点转换为z=0的世界坐标系
    image_points_to_world_plane (CamParam, Pose, Rows, Columns, 'm', SX, SY)//
    distance_pp (SY[0], SX[0], SY[1], SX[1], Width)//
    *
    * Display results of width measurement
    dev_display (Image)//
    dev_set_color ('white')//
    dev_set_line_width (3)//
    dev_display (Rectangle)//
    dev_set_color ('green')//
    dev_set_draw ('fill')//
    dev_set_line_width (2)//
    dev_display (Cross)//
    dev_set_draw ('margin')//
    disp_message (WindowHandle, 'Width = ' + (Width * 100)$'8.3f' + 'cm', 'window', 12, 12, 'black', 'true')//
    disp_continue_message (WindowHandle, 'black', 'true')//
    stop ()
    *
    * 现在,测量校准标记点的尺寸
    *  提取图像中的椭圆
    erosion_circle (PlateRegion, ROI, 17.5)  //生成一个圆形结构膨胀元素(,腐蚀区域,元素结构尺寸)
    reduce_domain (Image, ROI, ImageReduced)//选取图像中感兴趣的区域
    edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 60) //使用滤波器来精确检测边缘
    select_contours_xld (Edges, SelectedEdges, 'contour_length', 20, 99999999, -0.5, 0.5)//根据一些特征选择XLD轮廓(contour)
    * Fit ellipses to extracted edges
    fit_ellipse_contour_xld (SelectedEdges, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder)//根据椭圆和椭圆弧近似提取XLD轮廓
    MeanRadius1 := mean(Radius1)//
    MeanRadius2 := mean(Radius2)//
    DevRadius1 := deviation(Radius1)//
    DevRadius2 := deviation(Radius2)//
    * Transform the ellipses to world coordinates, where they should be circles
    * 把圆转换到世界坐标.
    contour_to_world_plane_xld (SelectedEdges, WorldCircles, CamParam, Pose, 'mm')//
    * 拟合椭圆
    fit_ellipse_contour_xld (WorldCircles, 'fitzgibbon', -1, 2, 0, 200, 3, 2, Row, Column, Phi, RadiusW1, RadiusW2, StartPhi, EndPhi, PointOrder)//
    *取均值
    MeanRadiusW1 := mean(RadiusW1)//
    MeanRadiusW2 := mean(RadiusW2)//
    DevRadiusW1 := deviation(RadiusW1)//
    DevRadiusW2 := deviation(RadiusW2)//
    *
    * Display results of ellipse measurement
    dev_display (Image)//
    dev_set_color ('yellow')//
    dev_set_line_width (3)//
    dev_display (SelectedEdges)//
    Message := 'Measured dimensions of the ellipses'//
    Message[0] := '                    Mean Radius1; Mean Radius2; (Standard deviations [%])'//
    Message[1] := 'Image coordinates:       ' + MeanRadius1$'5.2f' + 'px; ' + MeanRadius2$'5.2f' + 'px            (' + (DevRadius1 / MeanRadius1 * 100)$'4.2f' + ', ' + (DevRadius2 / MeanRadius2 * 100)$'4.2f' + ')'//
    Message[2] := 'World coordinates:       ' + (MeanRadiusW1 / 10)$'5.2f' + 'cm; ' + (MeanRadiusW2 / 10)$'5.2f' + 'cm            (' + (DevRadiusW1 / MeanRadiusW1 * 100)$'4.2f' + ', ' + (DevRadiusW2 / MeanRadiusW2 * 100)$'4.2f' + ')'//
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')//
    if (I < 10)
        disp_continue_message (WindowHandle, 'black', 'true')//
        stop ()
    endif
endfor
clear_calib_data (CalibDataID)//


 get_measure_positions (Image, PlateRegion, CalibDataID, I, Distance, Phi, RowCenter, ColumnCenter)

* 提取特征
threshold (Image, Region, 0, 120)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['holes_num','rect2_len1','rect2_len2'], 'and', [1,120,120], [1,200,200])
fill_up (SelectedRegions, PlateRegion)
*从表的边框构造线条
gen_contour_region_xld (PlateRegion, Contours, 'center') //画轮廓
segment_contours_xld (Contours, ContoursSplit, 'lines', 7, 4, 2)//分割轮廓
regress_contours_xld (ContoursSplit, RegressContours, 'no', 1)//计算回归(后一步的先决条件,下一步包含direction  or currature)
select_contours_xld (RegressContours, VerticalContours, 'direction', rad(45), rad(135), -0.5, 0.5)   //过滤轮廓
select_contours_xld (VerticalContours, LongContours, 'length', 150, 500, -0.5, 0.5)
* 获取两条线段的端点
select_obj (LongContours, Contour, 1)//选第一条线
get_contour_xld (Contour, Rows, Columns)
RowBegin1 := Rows[0] //两端点
ColBegin1 := Columns[0]
RowEnd1 := Rows[|Rows| - 1]
ColEnd1 := Columns[|Columns| - 1]
select_obj (LongContours, Contour, 2)//第二条线
get_contour_xld (Contour, Rows, Columns)
RowBegin2 := Rows[0]
ColBegin2 := Columns[0]
RowEnd2 := Rows[|Rows| - 1]
ColEnd2 := Columns[|Columns| - 1]
get_calib_data_observ_points (CalibDataID, 0, 0, PoseIndex - 1, Row, Column, PoseIndex, _Pose)    //获取点坐标
Row1 := Row[find(PoseIndex,21)]  //第22个和28个点
Row2 := Row[find(PoseIndex,27)]
Column1 := Column[find(PoseIndex,21)]
Column2 := Column[find(PoseIndex,27)]
*找两个交点坐标
intersection_lines (Row1, Column1, Row2, Column2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, RowA, ColA, IsOverlapping)  //第一个交点
intersection_lines (Row1, Column1, Row2, Column2, RowBegin2, ColBegin2, RowEnd2, ColEnd2, RowB, ColB, IsOverlapping)  //第二个交点
distance_pp (RowA, ColA, RowB, ColB, Distance)  //距离
line_orientation (RowA, ColA, RowB, ColB, Phi)  //角度
RowCenter := (RowA + RowB) / 2                  //中心点
ColumnCenter := (ColA + ColB) / 2
return ()



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

作者:hackpig
来源:
www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864