Halcon相機標定


Halcon程序


 

*相機的內參數:
*f:相機的主矩,即焦距
*k:徑向扭曲的大小,即徑向畸變,一般不考慮切向畸變
*sx,sy:圖像傳感器在水平和垂直方向上相鄰像素之間的距離
*cx,cy: 投影中心在成像平面的垂直投影

*相機外參數
*平移向量X,Y,Z
*旋轉向量X,Y,Z

*透視矯正

dev_close_window()
dev_update_off()
dev_set_draw('margin')
read_image(Image, 'scratch/scratch_perspective')
get_image_size(Image, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
set_display_font(WindowHandle, 16, 'mono', 'true', 'false')
dev_display(Image)
disp_continue_message(WindowHandle, 'black', 'true')

*標定相機

*標定板描述文件
CaltabName := 'caltab_30mm.descr'

*初始內參數
*參數:焦距,厚度,單個像元的寬,單個像元的高,圖形中心x,圖像中心y,圖形寬,圖像高
StartCamPar := [0.012,0,0.0000055,0.0000055, Width/2, Height/2, Width, Height]

*創建標定數據模型
create_calib_data('calibration_object', 1, 1, CalibDataID)

*設置標定相機參數模型
set_calib_data_cam_param(CalibDataID, 0, 'area_scan_division', StartCamPar)

*在標定模型中制定標定板描述文件
set_calib_data_calib_object(CalibDataID, 0, CaltabName)

***********************************
*獲得標定板描述文件中標志點中心坐標
* caltab_points(CaltabName,X, Y, Z)
* NStartPose := []
* NRow := []
* NCol := []
***********************************

NumImages := 12
for i :=1 to NumImages by 1
read_image(Image, 'scratch/scratch_calib_'+i$'02d')
dev_display(Image)
*尋找標定板區域
find_caltab(Image, Caltab, CaltabName, 3, 112, 5)
dev_set_color('green')
dev_display(Caltab)

*尋找標定板標志點坐標和預估外參
find_marks_and_pose(Image, Caltab, CaltabName, StartCamPar,128, 10, 18, 0.9, 15, 100, RCoord, CCoord, StartPose)
dev_set_color('red')
disp_circle(WindowHandle, 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)

******************************
*組合所有標定外參數和中心坐標
* NStartPose := [NStartPose, StartPose]
* NRow := [NRow, RCoord]
* NCol := [NCol, CCoord]
******************************
endfor

******************************
*標定相機所有參數(可以不使用算子set_calib_data_observ_points)
* camera_calibration(X, Y, Z, NRow, NCol, StartCamPar, NStartPose, 'all', CameraParam, NFinalPose, Errors)
******************************

*標定相機參數
calibrate_cameras(CalibDataID, Error)

*獲取相機內參數
get_calib_data(CalibDataID,'camera',0, 'params', CamParam)

*獲取相機外參數
get_calib_data(CalibDataID, 'calib_obj_pose',[0,1],'pose',PoseCalib)


*轉換原點坐標
set_origin_pose(StartPose, -0.04, -0.03, 0.00075, Pose)

*設置標定后的圖像一個像素代表的實際距離
PixelDist := 0.00013

*生成映像圖像
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(WindowHandle)
dev_display(Image)


*應用映像轉換圖像
map_image(Image, Map,ImageMapped)

endfor


 

camera_parameters.dat文件內容

#
# INTERNAL CAMERA PARAMETERS
#

# ===== generic parameter syntax ==========================================
#
# Syntax: ParGroup: <pargroupname>;
# <pargroup_description>;
#
# <name> : <shortname> : <default_value>;
# <type> : <lower_bound> : <upper_bound>;
# <description>;
#
# [ <type> ::= BOOL|XBOOL|INT|FLOAT|DOUBLE|STRING ]
#
# =========================================================================

 

##############################################################################
#
# Camera : Parameter
# > Focus
# > Kappa
# > Sx
# > Sy
# > Cx
# > Cy
# > ImageWidth
# > ImageHeight
#
##############################################################################

ParGroup: Camera: Parameter;
"Internal camera parameters";

Focus:foc: 0.0161703501831527;
DOUBLE:0.0:;
"Focal length of the lens";

Kappa:kappa: -639.990421851654;
DOUBLE::;
"Radial distortion coefficient";

Sx:sx: 7.40108791846177e-006;
DOUBLE:0.0:;
"Width of a cell on the sensor";

Sy:sy: 7.4e-006;
DOUBLE:0.0:;
"Height of a cell on the sensor";

Cx:cx: 333.895895987285;
DOUBLE::;
"X-coordinate of the image center";

Cy:cy: 244.935973115852;
DOUBLE::;
"Y-coordinate of the image center";

ImageWidth:imgw: 652;
INT:1:32768;
"Width of the images";

ImageHeight:imgh: 494;
INT:1:32768;
"Height of the images";


#
# HALCON Version 12.0 -- Tue Apr 14 22:41:15 2015
#


 

camera_pose.dat文件內容

#
# 3D POSE PARAMETERS: rotation and translation
#

# Used representation type:
f 0

# Rotation angles [deg] or Rodriguez vector:
r 3.11886998639614 358.984666972156 30.2659614074603

# Translation vector (x y z [m]):
t 0.0248668618545196 0.0133955230862179 0.401474091384729

#
# HALCON Version 12.0 -- Tue Apr 14 22:41:15 2015

#


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM