Matlab 双目标定 自动化程序

Matlab 双目标定 自动化程序,第1张

%实现.m文件自动化双目标定

% Auto-generated by stereoCalibrator app on 10-Jul-2019

%-------------------------------------------------------

%function  stereoParams = tereoCameraCalibrator01(file_path01,file_path02)

h=waitbar(0,'计算中,请稍候!')%增加进度条

file_path01 =  'left\'% 图像文件夹路径

file_path02 =  'right\'% 图宽碧衫像文件夹路径

img_path_list01 = dir(strcat(file_path01,'*.png'))%获取该文件夹中所有.PNG格式的图像

img_num01 = length(img_path_list01)%获慎腔取图像总数慧返

imageFileNames1 = cell(1,img_num01)

if img_num01 >0 %有满足条件的图像

for pn = 1:img_num01 %逐一读取图像

            image_name = img_path_list01(pn).name% 图像名

            %img_origin =  imread(strcat(file_path01,image_name))%读取图像

            imageFileNames1(1,pn) ={ strcat(file_path01,image_name)}

            %fprintf('%d %s\n',pn,strcat(file_path01,image_name))% 显示正在处理的图像名

                        %%此处添加具体的图像处理程序

end

end

img_path_list02 = dir(strcat(file_path02,'*.png'))%获取该文件夹中所有.PNG格式的图像

img_num02 = length(img_path_list02)%获取图像总数

imageFileNames2 = cell(1,img_num02)

if img_num02 >0 %有满足条件的图像

for pn = 1:img_num02 %逐一读取图像

            image_name = img_path_list02(pn).name% 图像名

            %img_origin =  imread(strcat(file_path01,image_name))%读取图像

            imageFileNames2(1,pn) ={ strcat(file_path02,image_name)}

            %fprintf('%d %s\n',pn,strcat(file_path01,image_name))% 显示正在处理的图像名

                        %%此处添加具体的图像处理程序

end

end

waitbar(0.1)

% Detect checkerboards in images

[imagePoints, boardSize, imagesUsed] = detectCheckerboardPoints(imageFileNames1, imageFileNames2)

waitbar(0.2)

% Generate world coordinates of the checkerboard keypoints

squareSize = 24  % in units of 'millimeters'

worldPoints = generateCheckerboardPoints(boardSize, squareSize)

waitbar(0.3)

% Read one of the images from the first stereo pair

I1 = imread(imageFileNames1{1})

[mrows, ncols, ~] = size(I1)

waitbar(0.4)

% Calibrate the camera

[stereoParams, pairsUsed, estimationErrors] = estimateCameraParameters(imagePoints, worldPoints, ...

    'EstimateSkew', false, 'EstimateTangentialDistortion', true, ...

    'NumRadialDistortionCoefficients', 3, 'WorldUnits', 'millimeters', ...

    'InitialIntrinsicMatrix', [], 'InitialRadialDistortion', [], ...

    'ImageSize', [mrows, ncols])

waitbar(0.8)

% View reprojection errors

h1=figureshowReprojectionErrors(stereoParams)

% Visualize pattern locations

h2=figureshowExtrinsics(stereoParams, 'CameraCentric')

waitbar(0.9)

% Display parameter estimation errors

%displayErrors(estimationErrors, stereoParams)

% You can use the calibration data to rectify stereo images.

%I2 = imread(imageFileNames2{1})

%[J1, J2] = rectifyStereoImages(I1, I2, stereoParams)

% See additional examples of how to use the calibration data.  At the prompt type:

% showdemo('StereoCalibrationAndSceneReconstructionExample')

% showdemo('DepthEstimationFromStereoVideoExample')

fid=fopen('CameraParameter.txt','wt')

fprintf(fid,'stereoParams.RotationOfCamera2:\n')

fprintf(fid,'%f  %f  %f \n',stereoParams.RotationOfCamera2)

fprintf(fid,'stereoParams.TranslationOfCamera2:\n')

fprintf(fid,'%f  %f  %f\n',stereoParams.TranslationOfCamera2)

fprintf(fid,'stereoParams.CameraParameters1.IntrinsicMatrix:\n')

fprintf(fid,'%f  %f  %f\n',stereoParams.CameraParameters1.IntrinsicMatrix)

fprintf(fid,'stereoParams.CameraParameters2.IntrinsicMatrix:\n')

fprintf(fid,'%f  %f  %f\n',stereoParams.CameraParameters2.IntrinsicMatrix)

fclose(fid)

close(h)

%mcc -mv tereoCameraCalibrator01.m

0.   A4纸打印出来(或直接在手机上)并用尺子测量一个格的边长。

1.用想要标定的相机拍摄各种角度的照片。(我用的是电脑相机拍摄手机上的图片,手机上全屏的图片用尺子测量的是小格子的边长为7mm),15到20张即可。

2.打开matlab命令行输入cameraCalibrator回车。点Add images,导入所拍的照片,改为7mm,确定。

3.点击Options,勾选如图所示。点击Calibrate。

4,最后点击。Export Camera Parameters导出参数。

5,回到matlab工作区。

IntrinsicMatrix对应内参,注意这个和程序中是转置的关系,注意不要搞错。

RadiaDistortion即为相机的畸变矩阵对应k1,k2,k3。

TangentialDistortion对应p1,p2。

畸变参数键洞野,总共有五个,径向畸变3个(k1,k2,k3)和切向畸变2个(p1,p2)。

RotationMatrices为16张图片的外参矩阵translationVectors 为16张图片的转移矩阵。因为每张图片对应一个世界坐标系(旋转矩阵和平移矩阵是世界坐标系相对于摄像机坐标系的,而算法中规定世界坐标系是标定板为z=0的平面,对于不同位置的标定板,其世界坐标系的定义是不同的,这也就不难解释为什么有多个旋转矩阵和平移矩阵了),所以稿喊实际情况时,内参矩阵,畸变矩阵确定后,选一个合适的世界坐标系,将其对应的外参矩阵和平移矩阵带入即可用于颤凳相机的坐标系转换。

至此,相机标定结束。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12224898.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存