Halcon相机标定及畸变矫正

您所在的位置:网站首页 halcon图像坐标为负值 Halcon相机标定及畸变矫正

Halcon相机标定及畸变矫正

2023-04-16 01:48| 来源: 网络整理| 查看: 265

1 halcon相机标定和图像矫正     对于相机采集的图片,会由于相机本身和透镜的影响产生形变,通常需要对相机进行标定,获取相机的内参或内外参,然后矫正其畸变。相机畸变主要分为径向畸变和切向畸变,其中径向畸变是由透镜造成的,切向畸变是由成像仪与相机透镜的不平行造成的。(更多技术分享,请关注微信公众号:善眸科技;更多技术交流请加入qq机器视觉联盟群:1020941040)。     针孔模型是理想透镜的成像模型,但是实际中相机的透镜不可能是理想的模型,透镜形状的非理想特征造成像点会沿径向发生畸变。一个像点沿径向内缩叫负畸变,或桶形畸变沿径向外延叫正畸变,或枕形畸变。这种崎变相对于光轴严格对称的,也是畸变的主要分量。

相机标定模型公式,

其中,(X,Y,Z)为世界坐标系中的实际点坐标,(u,v)为图像坐标系统的像素坐标,A为相机内参,f像素单元的焦距,c图像像素中心点。

1.1 halcon相机标定1.1.1标定助手及相机参数设置 打开halcon标定助手,加载标定板文件,选择相机类型,设置相机参数,然后加载相机采集的标定图像,如下图所示,图像中全部标定点能够检测出,即采集的相机已经设置好,然后生成代码。

图4 halcon标定助手

标定板生成: 标定板文件的生成分为.descr和.cpd的文件,不同格式文件需使用不同函数进行生成。如, *生成的是27*31,标定点直径0.0075mm的.cpd标定板(精度高标定板) create_caltab (27, 31, 0.0075, [13, 6, 6, 20, 20], [15, 6, 24, 6, 24], 'light_on_dark', 'D:/calplate.cpd', 'caltab.ps') *生成的是7*7,标定点之间距离0.1m,直径0.5的.descr标定板 gen_caltab( 7, 7, 0.1, 0.5, 'D:/caltab.descr', 'caltab.ps') 加载标定图像:

图5加载标定图像

如图5所示,加载标定图像后,状态为确定时,表示标定图像可用于进行标定,然后通过标定助手直接生成标定代码即可。    如图6所示,加载标定图像后,状态为检测出品质问题,此原因大多是照片质量问题,例如光照、对焦、曝光等,需按照halcon标定注意事项的内容拍摄照片。一般照片检测出品质问题也可以进行相机标定。 加载标定板图像会出现“标志点提取失败”,出现此原因的需要根据标定界面下的状态栏查找halcon错误信息,例”inconsistent....(image mirrored? )”,则需要查找相应原因(实际标定板(透明)与标定板文件图片的可能有镜像区别,将实际标定板翻转即可,多试)。此不可进行相机标定。

图6 相机标定问题

相机初始参数设置: 例如, *(f, k, cell width, cell height, cx, cy, width, height)k为畸变系数 StartParameters := [0.029,0,4.3e-006,4.3e-006,2592,1728,5184,3456] *(f, k1, k2, k3, p1, p2, cell width, cell height, cx, cy, width, height)k1,k2,k3径向畸变, p1,p2为切向畸变 StartParameters:=[0.029,0,0,0,0,0,4.3e-006,4.3e-006,2592,1728,5184,3456] 其中,相机初始参数根据相机模型的不同而不同,若相机模型为area(division),则相机参数为7个,若area(多项式),则相机参数12个。

1.1.2 标定程序 利用标定助手生成代码,然后执行结果,分析其正确性,若需要将其生成为C/C++程序,可通过halcon界面的‘文件’项‘导出’成.cpp文件。

图7 halcon生成C程序

Halcon标定程序

*Calibration 01: Code generated by Calibration 01*读入某个文件夹下的所有标定图像list_files ('D:/halconvc/img', ['files','follow_links'], ImageFiles)tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)*初始标定参数设置及定义标定板文件TmpCtrl_ReferenceIndex := 0TmpCtrl_PlateDescription := 'D:/calib/calplate.cpd'StartParameters := [0.029,0,0,0,0,0,4.3e-006,4.3e-006,2592,1728,5184,3456]TmpCtrl_FindCalObjParNames := 'sigma'TmpCtrl_FindCalObjParValues := 1 * Calibration 01: Create calibration model for managing calibration datacreate_calib_data ('calibration_object', 1, 1, CalibHandle)set_calib_data_cam_param(CalibHandle,0,'area_scan_polynomial', StartParameters)set_calib_data_calib_object (CalibHandle, 0, TmpCtrl_PlateDescription)* Calibration 01: Collect mark positions and estimated poses for all platesgen_empty_obj (Images)for Index := 0 to |ImageFiles|-1 by 1 read_image (Image, ImageFiles[Index]) concat_obj(Images,Image,Images)*提取图像Images中标定板上的圆形标志来确定标定板的有效区域 find_calib_object (Image, CalibHandle, 0, 0, Index, TmpCtrl_FindCalObjParNames, TmpCtrl_FindCalObjParValues)endfor* Calibration 01: Perform the actual calibrationcalibrate_cameras (CalibHandle, TmpCtrl_Errors)get_calib_data (CalibHandle, 'camera', 0, 'params', CameraParam) *将内外参保存到磁盘write_cam_par(CameraParam,'D:/halconvc/campar.dat')get_calib_data (CalibHandle, 'calib_obj_pose', [0, TmpCtrl_ReferenceIndex], 'pose', CameraPose)set_origin_pose (CameraPose, 0, 0, 0.01, CameraPose)write_pose(CameraPose,'D:/halconvc/campos.dat')stop() *另外读取内外参文件的函数read_cam_par ('D:/halconvc/campar.dat', CameraParam)read_pose ('D:/halconvc/campos.dat', CameraPose)

1.2图像矫正 Halcon中图像矫正,接上述程序,以下介绍两种方法。 ①使用内外参,即相机内参和位姿

* goal: rectify images* first determine parameters such that the entire image content is visible* -> transform image boundary into world plane, determine smallest* rectangle around it*当设备固定后,位资是唯一的select_obj(images,image,1)get_image_pointer1(Image, Pointer, Type, Width, Height)gen_rectangle1 (ImageRect, 0, 0, Height-1, Width-1)gen_contour_region_xld (ImageRect, ImageBorder, 'border')contour_to_world_plane_xld(ImageBorder, ImageBorderWCS, CameraParam,CameraPose, 1)smallest_rectangle1_xld (ImageBorderWCS, MinY, MinX, MaxY, MaxX)set_origin_pose(CameraPose, MinX, MinY, 0.01, PoseForEntireImage)image_points_to_world_plane(CameraParam,PoseForEntireImage,[Height/2, Height/2, Height/2+1], [Width/2, Width/2+1, Width/2], 1, WorldPixelX, WorldPixelY)distance_pp(WorldPixelY[0], WorldPixelX[0], WorldPixelY[1], WorldPixelX[1], WorldLength1)distance_pp(WorldPixelY[0], WorldPixelX[0], WorldPixelY[2], WorldPixelX[2], WorldLength2)ScaleForSimilarPixelSize := (WorldLength1+WorldLength2)/2* -> determine output image size such that entire input image fits into itExtentX := MaxX-MinXExtentY := MaxY-MinYWidthRectifiedImage := ExtentX/ScaleForSimilarPixelSizeHeightRectifiedImage := ExtentY/ScaleForSimilarPixelSize* create mapping with the determined parachuangjia*创建一个投射图,其描述图像平面与坐标轴系统中平面Z为零之间的映射gen_image_to_world_plane_map(Map, CameraParam, PoseForEntireImage, Width, Height, WidthRectifiedImage, HeightRectifiedImage, \ ScaleForSimilarPixelSize, 'bilinear')clear_calib_data (CalibHandle) * Map the imagesfor I := 1 to |ImageFiles| by 1 select_obj (Images, Img, I) map_image (Img, Map, ImageMapped) write_image (ImageMapped, 'jpg', 0, 'D:/halconvc/picture/'+I)Endfor

②仅使用内参

read_image (Image,'D:/halconvc/img/IMG_00.JPG')read_cam_par('D:/halconvc/campar.dat',CameraParam)CarParamVirtualFixed:=CameraParam*对于area(diversion)相机模型(7个参数)change_radial_distortion_cam_par('adaptive',CameraParam,0,CarParamVirtualFixed)*对于area(多项式)相机模型(12个参数)change_radial_distortion_cam_par('adaptive',CameraParam,[0,0,0,0,0],CarParamVirtualFixed) *上述相机模型选一种后进行下面的map_image*创建一个投射图,其描述图像与其相应正在改变的径向畸变,而对于12个参数的畸变包括径向和切向畸变gen_radial_distortion_map(Map,CameraParam,CarParamVirtualFixed,'bilinear')map_image(Image,Map,ImageMapped)

总结:

可以先使用标定助手获取标定参数

在标定助手的安装页面,注意标定板的描述文件,投影模型参数的设置

在标定页面先选择一幅图设为参考位姿,之后点击标定

标定完之后获得相机内参,图片矫正程序方法

* Calibration 01: Code generated by Calibration 01CameraParameters := ['area_scan_division',0.0186441,-520.011,8.35005e-006,8.3e-006,246.657,254.674,640,480]CameraPose := [0.00994196,-0.00291689,0.290462,358.424,32.411,91.0749,0]stop ()*获取径向畸变矫正的外参change_radial_distortion_cam_par ('adaptive', CameraParameters, 0, CamParamOut) read_image (CalibDistorted03, 'C:/Users/Public/Documents/MVTec/HALCON-13.0/examples/images/scratch/scratch_perspective.png')*第一种方法使用map_image()进行畸变矫正gen_radial_distortion_map (Map, CameraParameters, CamParamOut, 'bilinear')map_image (CalibDistorted03, Map, ImageMapped)*第二种方法使用change_radial_distortion_image进行径向畸变矫正change_radial_distortion_image (CalibDistorted03,CalibDistorted03 , ImageRectified, CameraParameters, CamParamOut)

此种只进行畸变矫正,并不进行图像的由于高度差造成的3D方向的位姿矫正。若要进行3D方向的矫正可以参考范例 image_to_world_plane.hdev 畸变矫正范例为: change_radial_distortion_image.hdev change_radial_distortion_points.hdev

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3