halcon相机标定及图像矫正(代码)
侵刪
1 halcon相機(jī)標(biāo)定和圖像矯正
對(duì)于相機(jī)采集的圖片,會(huì)由于相機(jī)本身和透鏡的影響產(chǎn)生形變,通常需要對(duì)相機(jī)進(jìn)行標(biāo)定,獲取相機(jī)的內(nèi)參或內(nèi)外參,然后矯正其畸變。相機(jī)畸變主要分為徑向畸變和切向畸變,其中徑向畸變是由透鏡造成的,切向畸變是由成像儀與相機(jī)透鏡的不平行造成的。
針孔模型是理想透鏡的成像模型,但是實(shí)際中相機(jī)的透鏡不可能是理想的模型,透鏡形狀的非理想特征造成像點(diǎn)會(huì)沿徑向發(fā)生畸變。一個(gè)像點(diǎn)沿徑向內(nèi)縮叫負(fù)畸變,或桶形畸變沿徑向外延叫正畸變,或枕形畸變。這種崎變相對(duì)于光軸嚴(yán)格對(duì)稱(chēng)的,也是畸變的主要分量。
圖1徑向畸變 ??????????????????????????圖2切向畸變
相機(jī)標(biāo)定模型公式,
?
? ? ? ? ? ? ? ? ? Or ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? (1-1)
其中,(X,Y,Z)為世界坐標(biāo)系中的實(shí)際點(diǎn)坐標(biāo),(u,v)為圖像坐標(biāo)系統(tǒng)的像素坐標(biāo),A為相機(jī)內(nèi)參,f像素單元的焦距,c圖像像素中心點(diǎn)。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? (1-2)
?
圖3相機(jī)成像模型
1.1?halcon相機(jī)標(biāo)定
1.1.1標(biāo)定助手及相機(jī)參數(shù)設(shè)置
打開(kāi)halcon標(biāo)定助手,加載標(biāo)定板文件,選擇相機(jī)類(lèi)型,設(shè)置相機(jī)參數(shù),然后加載相機(jī)采集的標(biāo)定圖像,如下圖所示,圖像中全部標(biāo)定點(diǎn)能夠檢測(cè)出,即采集的相機(jī)已經(jīng)設(shè)置好,然后生成代碼。
?
圖4 halcon標(biāo)定助手
標(biāo)定板生成:
標(biāo)定板文件的生成分為.descr和.cpd的文件,不同格式文件需使用不同函數(shù)進(jìn)行生成。如,
*生成的是27*31,標(biāo)定點(diǎn)直徑0.0075mm的.cpd標(biāo)定板(精度高標(biāo)定板)
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,標(biāo)定點(diǎn)之間距離0.1m,直徑0.5的.descr標(biāo)定板
gen_caltab( 7, 7, 0.1, 0.5, 'D:/caltab.descr', 'caltab.ps')
加載標(biāo)定圖像:
?
圖5加載標(biāo)定圖像
如圖5所示,加載標(biāo)定圖像后,狀態(tài)為確定時(shí),表示標(biāo)定圖像可用于進(jìn)行標(biāo)定,然后通過(guò)標(biāo)定助手直接生成標(biāo)定代碼即可。
? ? 如圖6所示,加載標(biāo)定圖像后,狀態(tài)為檢測(cè)出品質(zhì)問(wèn)題,此原因大多是照片質(zhì)量問(wèn)題,例如光照、對(duì)焦、曝光等,需按照halcon標(biāo)定注意事項(xiàng)的內(nèi)容拍攝照片。一般照片檢測(cè)出品質(zhì)問(wèn)題也可以進(jìn)行相機(jī)標(biāo)定。
加載標(biāo)定板圖像會(huì)出現(xiàn)“標(biāo)志點(diǎn)提取失敗”,出現(xiàn)此原因的需要根據(jù)標(biāo)定界面下的狀態(tài)欄查找halcon錯(cuò)誤信息,例”inconsistent....(image mirrored? )”,則需要查找相應(yīng)原因(實(shí)際標(biāo)定板(透明)與標(biāo)定板文件圖片的可能有鏡像區(qū)別,將實(shí)際標(biāo)定板翻轉(zhuǎn)即可,多試)。此不可進(jìn)行相機(jī)標(biāo)定。
? ? ? ? ? ? ? ? ? ? ? ? ? ??
圖6 相機(jī)標(biāo)定問(wèn)題
相機(jī)初始參數(shù)設(shè)置:
例如,
*(f, k, cell width, cell height, cx, cy, width, height)k為畸變系數(shù)
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]
其中,相機(jī)初始參數(shù)根據(jù)相機(jī)模型的不同而不同,若相機(jī)模型為area(division),則相機(jī)參數(shù)為7個(gè),若area(多項(xiàng)式),則相機(jī)參數(shù)12個(gè)。
1.1.2?標(biāo)定程序
利用標(biāo)定助手生成代碼,然后執(zhí)行結(jié)果,分析其正確性,若需要將其生成為C/C++程序,可通過(guò)halcon界面的‘文件’項(xiàng)‘導(dǎo)出’成.cpp文件。
?
圖7 halcon生成C程序
Halcon標(biāo)定程序
*Calibration 01: Code generated by Calibration 01
*讀入某個(gè)文件夾下的所有標(biāo)定圖像
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)
*初始標(biāo)定參數(shù)設(shè)置及定義標(biāo)定板文件
TmpCtrl_ReferenceIndex := 0
TmpCtrl_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 data
create_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 plates
gen_empty_obj (Images)
for Index := 0 to |ImageFiles|-1 by 1
????read_image (Image, ImageFiles[Index])
????concat_obj(Images,Image,Images)
*提取圖像Images中標(biāo)定板上的圓形標(biāo)志來(lái)確定標(biāo)定板的有效區(qū)域
????find_calib_object (Image, CalibHandle, 0, 0, Index, TmpCtrl_FindCalObjParNames, TmpCtrl_FindCalObjParValues)
endfor
* Calibration 01: Perform the actual calibration
calibrate_cameras (CalibHandle, TmpCtrl_Errors)
get_calib_data (CalibHandle, 'camera', 0, 'params', CameraParam)
?
???*將內(nèi)外參保存到磁盤(pán)
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()
?
*另外讀取內(nèi)外參文件的函數(shù)
read_cam_par ('D:/halconvc/campar.dat', CameraParam)
read_pose ('D:/halconvc/campos.dat', CameraPose)
1.2圖像矯正
Halcon中圖像矯正,接上述程序,以下介紹兩種方法。
①使用內(nèi)外參,即相機(jī)內(nèi)參和位資
* ?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
*當(dāng)設(shè)備固定后,位資是唯一的
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 it
ExtentX := MaxX-MinX
ExtentY := MaxY-MinY
WidthRectifiedImage := ExtentX/ScaleForSimilarPixelSize
HeightRectifiedImage := ExtentY/ScaleForSimilarPixelSize
* ?create mapping with the determined parachuangjia
*創(chuàng)建一個(gè)投射圖,其描述圖像平面與坐標(biāo)軸系統(tǒng)中平面Z為零之間的映射
gen_image_to_world_plane_map(Map, CameraParam, PoseForEntireImage, Width, Height, WidthRectifiedImage, HeightRectifiedImage, \
?????????????????????????????ScaleForSimilarPixelSize, 'bilinear')
clear_calib_data (CalibHandle)
?
* Map the images
for 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
②僅使用內(nèi)參
read_image (Image,'D:/halconvc/img/IMG_00.JPG')
read_cam_par('D:/halconvc/campar.dat',CameraParam)
CarParamVirtualFixed:=CameraParam
*對(duì)于area(diversion)相機(jī)模型(7個(gè)參數(shù))
change_radial_distortion_cam_par('adaptive',CameraParam,0,CarParamVirtualFixed)
*對(duì)于area(多項(xiàng)式)相機(jī)模型(12個(gè)參數(shù))
change_radial_distortion_cam_par('adaptive',CameraParam,[0,0,0,0,0],CarParamVirtualFixed)
*上述相機(jī)模型選一種后進(jìn)行下面的map_image
*創(chuàng)建一個(gè)投射圖,其描述圖像與其相應(yīng)正在改變的徑向畸變,而對(duì)于12個(gè)參數(shù)的畸變包括徑向和切向畸變
gen_radial_distortion_map(Map,CameraParam,CarParamVirtualFixed,'bilinear')
map_image(Image,Map,ImageMapped)
總結(jié)
以上是生活随笔為你收集整理的halcon相机标定及图像矫正(代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MyEclipse移动开发教程:迁移HT
- 下一篇: Window winload.efi 文