キャリブレートされた​カメラによる平面オブ​ジェクトの測定につい​て

5 views (last 30 days)
直暉 田鴈
直暉 田鴈 on 10 Feb 2023
Commented: 直暉 田鴈 on 11 Feb 2023
現在起こっているエラー:
MATLABで実行するとある一文がエラーになり、実行が遮断されてしまう。
エラー箇所:
camExtrinsics = estimateExtrinsics(imagePoints, worldPoints, camIntrinsics);
エラー内容:
スクリプトestimateExtrinsicsが関数として実行することをサポートされません。
プログラムを1個ずつ実行したり、MATLABが提供しているソースコードをブロックごとにコピー&ペーストを行ったが変化なし
プログラムのエラー解決をお願いいたします。
  2 Comments
Tohru Kikawada
Tohru Kikawada on 10 Feb 2023
どちらのソースコードを実行されてますでしょうか。例題でしたらリンクを貼っていただけると何か分かるかもしれません。
直暉 田鴈
直暉 田鴈 on 10 Feb 2023
Moved: Atsushi Ueno on 10 Feb 2023
こちらがプログラムの内容になります。
キャリブレーション イメージの準備
numImages = 9;
files = cell(1, numImages);
for i = 1:numImages
files{i} = fullfile(matlabroot, 'toolbox', 'vision', 'visiondata', ...
'calibration', 'slr', sprintf('image%d.jpg', i));
end
% Display one of the calibration images
magnification = 25;
I = imread(files{1});
figure; imshow(I, InitialMagnification = magnification);
title("One of the Calibration Images");
カメラ パラメータの推定
% Detect the checkerboard corners in the images.
[imagePoints, boardSize] = detectCheckerboardPoints(files);
% Generate the world coordinates of the checkerboard corners in the
% pattern-centric coordinate system, with the upper-left corner at (0,0).
squareSize = 29; % in millimeters
worldPoints = generateCheckerboardPoints(boardSize, squareSize);
% Calibrate the camera.
imageSize = [size(I, 1), size(I, 2)];
cameraParams = estimateCameraParameters(imagePoints, worldPoints, ...
ImageSize = imageSize);
% Evaluate calibration accuracy.
figure; showReprojectionErrors(cameraParams);
title("Reprojection Errors");
推定するオブジェクトのイメージの読み取り
imOrig = imread(fullfile(matlabroot, "toolbox", "vision", "visiondata", ...
"calibration", "slr", "image9.jpg"));
figure; imshow(imOrig, InitialMagnification = magnification);
title("Input Image");
イメージの歪み補正
% Since the lens introduced little distortion, use 'full' output view to illustrate that
% the image was undistored. If we used the default 'same' option, it would be difficult
% to notice any difference when compared to the original image. Notice the small black borders.
[im, newOrigin] = undistortImage(imOrig, cameraParams, OutputView = "full");
figure; imshow(im, InitialMagnification = magnification);
title("Undistorted Image");
コインのセグメント化
% Convert the image to the HSV color space.
imHSV = rgb2hsv(im);
% Get the saturation channel.
saturation = imHSV(:, :, 2);
% Threshold the image
t = graythresh(saturation);
imCoin = (saturation > t);
figure; imshow(imCoin, InitialMagnification = magnification);
title("Segmented Coins");
コインの検出
% Find connected components.
blobAnalysis = vision.BlobAnalysis(AreaOutputPort = true,...
CentroidOutputPort = false,...
BoundingBoxOutputPort = true,...
MinimumBlobArea = 200, ExcludeBorderBlobs = true);
[areas, boxes] = step(blobAnalysis, imCoin);
% Sort connected components in descending order by area
[~, idx] = sort(areas, "Descend");
% Get the two largest components.
boxes = double(boxes(idx(1:2), :));
% Reduce the size of the image for display.
scale = magnification / 100;
imDetectedCoins = imresize(im, scale);
% Insert labels for the coins.
imDetectedCoins = insertObjectAnnotation(imDetectedCoins, "rectangle", ...
scale * boxes, "penny");
figure; imshow(imDetectedCoins);
title("Detected Coins");
外部パラメータの計算(エラー箇所)
% Detect the checkerboard.
[imagePoints, boardSize] = detectCheckerboardPoints(im);
% Adjust the imagePoints so that they are expressed in the coordinate system
% used in the original image, before it was undistorted. This adjustment
% makes it compatible with the cameraParameters object computed for the original image.
imagePoints = imagePoints + newOrigin; % adds newOrigin to every row of imagePoints
% Extract camera intrinsics.
camIntrinsics = cameraParams.Intrinsics;
% Compute extrinsic parameters of the camera.↓ココ↓
camExtrinsics = estimateExtrinsics(imagePoints, worldPoints, camIntrinsics);
最初のコインの測定
% Adjust upper left corners of bounding boxes for coordinate system shift
% caused by undistortImage with output view of 'full'. This would not be
% needed if the output was 'same'. The adjustment makes the points compatible
% with the cameraParameters of the original image.
boxes = boxes + [newOrigin, 0, 0]; % zero padding is added for width and height
% Get the top-left and the top-right corners.
box1 = double(boxes(1, :));
imagePoints1 = [box1(1:2); ...
box1(1) + box1(3), box1(2)];
% Get the world coordinates of the corners
worldPoints1 = img2world2d(imagePoints1, camExtrinsics, camIntrinsics);
% Compute the diameter of the coin in millimeters.
d = worldPoints1(2, :) - worldPoints1(1, :);
diameterInMillimeters = hypot(d(1), d(2));
fprintf("Measured diameter of one penny = %0.2f mm\n", diameterInMillimeters);
2個目のコインの測定
% Get the top-left and the top-right corners.
box2 = double(boxes(2, :));
imagePoints2 = [box2(1:2); ...
box2(1) + box2(3), box2(2)];
% Apply the inverse transformation from image to world
worldPoints2 = img2world2d(imagePoints2, camExtrinsics, camIntrinsics);
% Compute the diameter of the coin in millimeters.
d = worldPoints2(2, :) - worldPoints2(1, :);
diameterInMillimeters = hypot(d(1), d(2));
fprintf("Measured diameter of the other penny = %0.2f mm\n", diameterInMillimeters);
最初のコインまでの距離の測定
% Compute the center of the first coin in the image.
center1_image = box1(1:2) + box1(3:4)/2;
% Convert to world coordinates.
center1_world = img2world2d(center1_image, camExtrinsics, camIntrinsics);
% Remember to add the 0 z-coordinate.
center1_world = [center1_world 0];
% Compute the distance to the camera.
cameraPose = extr2pose(camExtrinsics);
cameraLocation = cameraPose.Translation;
distanceToCamera = norm(center1_world - cameraLocation);
fprintf("Distance from the camera to the first penny = %0.2f mm\n", ...
distanceToCamera);

Sign in to comment.

Answers (1)

Tohru Kikawada
Tohru Kikawada on 10 Feb 2023
estimateExtrinsicsはR2022bで導入された関数となっております。(ドキュメント下部にIntroduced in R2022bとございます。)
それ以前のバージョンでご利用になられたい場合にはドキュメントのURLを下記のように変更してみていただけますでしょうか。(古いバージョンには対応する例題がない場合もございます。ご了承ください。)
他のバージョンのドキュメンテーションについては下記もご参照ください。
今回の例題についてはドキュメンテーション右上の「Copy Command」をクリックすることで例題を開くためのコマンドをコピーすることができます。こちらをお持ちのMATLABで実行いただければ対応している例題を開いていただけます。
(繰り返しになりますが例題によっては過去のバージョンに含まれていないものもございます。)
openExample('vision/MeasuringPlanarObjectsExample')
  1 Comment
直暉 田鴈
直暉 田鴈 on 11 Feb 2023
ありがとうございます。おかげさまでプログラムが上手く動きました。
結局のところ関数が古いバージョンとの互換性が合わないせいでサービス側が拒否したんですね。

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!