[imagePoints, boardSize, imagesUsed] = detectCheckerboardPoints(imageFileNames); for i = 1:numel(imagesUsed) if imagesUsed(i) fprintf('Image %d: \n', i); disp(imagePoints(:,:,i)); img = imread(imageFileNames{i}); imshow(img); hold on; plot(imagePoints(:,1,i), imagePoints(:,2,i), 'ro', 'MarkerSize', 10); hold off; drawnow; else fprintf('Image %d: Not used\n', i); end end 优化代码 使之可以显示出来而不是一闪而过
时间: 2024-01-25 14:03:28 浏览: 130
可以将 `drawnow` 移到 `for` 循环内部,并添加一个 `waitforbuttonpress` 函数,使程序等待用户按下任意键后再进行下一张图片的显示。修改后的代码如下:
```
[imagePoints, boardSize, imagesUsed] = detectCheckerboardPoints(imageFileNames);
for i = 1:numel(imagesUsed)
if imagesUsed(i)
fprintf('Image %d: \n', i);
disp(imagePoints(:,:,i));
img = imread(imageFileNames{i});
imshow(img);
hold on;
plot(imagePoints(:,1,i), imagePoints(:,2,i), 'ro', 'MarkerSize', 10);
hold off;
drawnow;
waitforbuttonpress;
else
fprintf('Image %d: Not used\n', i);
end
end
```
其中,`waitforbuttonpress` 用于等待用户按下任意键。
相关问题
[imagePoints,boardSize] = detectCheckerboardPoints('calibration.jpg'); worldPoints = generateCheckerboardPoints(boardSize,25); cameraParams = estimateCameraParameters(imagePoints,worldPoints);代码解释
这段代码主要是用于相机标定的操作。
`detectCheckerboardPoints('calibration.jpg')`函数用于检测标定板上的角点,返回的`imagePoints`是一个 N×2 的矩阵,表示 N 个角点在图像中的坐标。`boardSize`是标定板的尺寸,表示标定板上每行、每列的角点数量。
`generateCheckerboardPoints(boardSize,25)`函数用于生成标定板上的角点的真实坐标,其中`boardSize`表示标定板的尺寸,`25`表示标定板上每个格子的尺寸(单位为毫米)。
`estimateCameraParameters(imagePoints,worldPoints)`函数用于估计相机的内参和畸变系数,返回的`cameraParams`包含了内参矩阵、畸变系数、像素尺寸等信息。
综合上述操作,这段代码实现了基于标定板的相机标定,得到了相机的内参和外参信息,为后续的图像处理和测距提供了基础。
% 拍摄标定板,使用单目相机拍摄10张.bmp类型的棋盘格图片 % 保证每张图片拍摄时相机的位置、角度、焦距等参数都不变 % 提取角点,使用Matlab自带的函数detectCheckerboardPoints,提取出每张图片上的棋盘格角点的位置 imageFileNames = {'01.bmp', '02.bmp', '03.bmp', ... '04.bmp', '05.bmp', '06.bmp', '07.bmp', ... '08.bmp', '09.bmp', '10.bmp'}; %[imagePoints, boardSize] = detectCheckerboardPoints(imageFileNames); [imagePoints, boardSize, imagesUsed] = detectCheckerboardPoints(imageFileNames); % 显示角点提取结果,使用Matlab自带的函数showExtractionErrors showExtractionErrors(imageFileNames, imagesUsed, imagePoints); % 标定相机,使用Matlab自带的函数estimateCameraParameters,将每张图片上提取出的角点位置,作为输入,就可以得到相机的内参矩阵K、畸变参数D、旋转矩阵R、平移向量T等参数 squareSize = 2; % 棋盘格尺寸为2mm*2mm worldPoints = generateCheckerboardPoints(boardSize, squareSize); params = estimateCameraParameters(imagePoints, worldPoints, ... 'EstimateSkew', false, 'EstimateTangentialDistortion', false); % 评估标定结果,使用Matlab自带的函数showReprojectionErrors,可以显示出每张图片上提取出的角点位置与通过标定得到的内参矩阵K、畸变参数D、旋转矩阵R、平移向量T计算出的角点位置之间的误差,以评估标定结果的准确性 showReprojectionErrors(params); % 输出标定结果 disp(params.IntrinsicMatrix); disp(params.RadialDistortion); disp(params.TangentialDistortion); disp(params.RotationMatrices); disp(params.TranslationVectors); % 保存相机内外参数为.mat文件 save('camera_params.mat', 'params'); 修改代码使之可以识别showExtractionErrors()函数
`showExtractionErrors()`函数并不是Matlab自带的函数,所以你需要先定义或下载这个函数才能在你的代码中使用它。你可以在网上搜索相关的Matlab代码,或者自己编写这个函数。
如果你已经有了`showExtractionErrors()`函数的定义,你需要将其添加到你的Matlab路径中,这样Matlab才能够找到这个函数。你可以通过在Matlab命令行中使用`addpath()`函数来添加路径,例如:
```matlab
addpath('C:\path\to\showExtractionErrors\');
```
将上面代码中的路径替换为你实际定义`showExtractionErrors()`函数的路径。这样,Matlab就能够找到这个函数并正确地识别它了。
阅读全文
相关推荐
















