(halcon实例)测量透视畸变图像上的划痕的长度


这个例子有点意思 ,非常值得学习一下。

它是测量透视畸变图像上的划痕的长度。

image.png


利用halcon标定板,在透视畸变下进行标定。

image.png

image.png

把畸变消除后,测量标准片的长度,很准确,是3cm。

image.png


然后测量透视畸变图像上的划痕的长度。

image.png


代码:

* This program measures the length of scratches in world
* coordinates in a perspectively distorted image
* 
dev_close_window ()
dev_close_window ()
dev_update_off ()
dev_set_draw ('margin')
read_image (Image, 'scratch/scratch_perspective')
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle1)
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
dev_display (Image)
dev_set_line_width (2)
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Step: calibrate the camera
* 
CaltabName := 'caltab_30mm.descr'
* Make sure that the file 'CaltabDescrName' is in the current directory,
* the HALCONROOT/calib directory, or use an absolut path
gen_cam_par_area_scan_division (0.012, 0, 0.0000055, 0.0000055, Width / 2, Height / 2, Width, Height, StartCamPar)
create_calib_data ('calibration_object', 1, 1, CalibDataID)
set_calib_data_cam_param (CalibDataID, 0, [], StartCamPar)
set_calib_data_calib_object (CalibDataID, 0, CaltabName)
NumImages := 12
for I := 1 to NumImages by 1
    read_image (Image, 'scratch/scratch_calib_' + I$'02d')
    dev_display (Image)
    find_calib_object (Image, CalibDataID, 0, 0, I, [], [])
    get_calib_data_observ_contours (Caltab, CalibDataID, 'caltab', 0, 0, I)
*     find_caltab (Image, Caltab, CaltabName, 3, 112, 5)
    dev_set_color ('green')
    dev_display (Caltab)
    get_calib_data_observ_points (CalibDataID, 0, 0, I, RCoord, CCoord, Index, StartPose)
*     find_marks_and_pose (Image, Caltab, CaltabName, StartCamPar, 128, 10, 18, 0.9, 15, 100, RCoord, CCoord, StartPose)
    dev_set_color ('red')
    disp_circle (WindowHandle1, RCoord, CCoord, gen_tuple_const(|RCoord|,2.5))
    dev_set_part (0, 0, Height - 1, Width - 1)
*     set_calib_data_observ_points (CalibDataID, 0, 0, i, RCoord, CCoord, 'all', StartPose)
endfor
dev_update_time ('on')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
calibrate_cameras (CalibDataID, Error)
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)
get_calib_data (CalibDataID, 'calib_obj_pose', [0,1], 'pose', PoseCalib)
* 
* Step: transform images
* 
dev_open_window (0, Width + 5, Width, Height, 'black', WindowHandle2)
set_display_font (WindowHandle2, 14, 'mono', 'true', 'false')
tuple_replace (PoseCalib, 5, PoseCalib[5] - 90, PoseCalibRot)
set_origin_pose (PoseCalibRot, -0.04, -0.03, 0.00075, Pose)
PixelDist := 0.00013
pose_to_hom_mat3d (Pose, HomMat3D)
gen_image_to_world_plane_map (Map, CamParam, Pose, Width, Height, Width, Height, PixelDist, 'bilinear')
Imagefiles := ['scratch/scratch_calib_01','scratch/scratch_perspective']
for I := 1 to 2 by 1
    read_image (Image, Imagefiles[I - 1])
    dev_set_window (WindowHandle1)
    dev_display (Image)
    dev_set_window (WindowHandle2)
    map_image (Image, Map, ModelImageMapped)
    dev_display (ModelImageMapped)
    if (I == 1)
        gen_contour_polygon_xld (Polygon, [230,230], [189,189 + 0.03 / PixelDist])
        disp_message (WindowHandle2, '3cm', 'window', 205, 195, 'red', 'false')
        dev_display (Polygon)
        disp_continue_message (WindowHandle2, 'black', 'true')
        stop ()
    endif
endfor
* 
* Step: measure in world coordinates in the transformed (rectified) image
* 
dev_set_draw ('fill')
fast_threshold (ModelImageMapped, Region, 0, 80, 20)
fill_up (Region, RegionFillUp)
erosion_rectangle1 (RegionFillUp, RegionErosion, 5, 5)
reduce_domain (ModelImageMapped, RegionErosion, ImageReduced)
fast_threshold (ImageReduced, Region1, 55, 100, 20)
dilation_circle (Region1, RegionDilation1, 2.0)
erosion_circle (RegionDilation1, RegionErosion1, 1.0)
connection (RegionErosion1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','ra'], 'and', [40,15], [2000,1000])
count_obj (SelectedRegions, NumScratches)
dev_display (ModelImageMapped)
for I := 1 to NumScratches by 1
    dev_set_color ('yellow')
    select_obj (SelectedRegions, ObjectSelected, I)
    skeleton (ObjectSelected, Skeleton)
    gen_contours_skeleton_xld (Skeleton, Contours, 1, 'filter')
    dev_display (Contours)
    length_xld (Contours, ContLength)
    area_center_points_xld (Contours, Area, Row, Column)
    disp_message (WindowHandle2, 'L= ' + (ContLength * PixelDist * 100)$'.4' + ' cm', 'window', Row - 10, Column + 20, 'yellow', 'false')
    disp_continue_message (WindowHandle2, 'black', 'true')
    stop ()
endfor
dev_close_window ()
clear_calib_data (CalibDataID)


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

作者:hackpig

来源:www.skcircle.com

版权声明:本文为博主原创文章,转载请附上博文链接!


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

发表评论:

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

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 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