Index exceeds matrix dimensions.
이전 댓글 표시
This is a program computing the distance of an object from camera using stereo vision camera system. However, sometimes it works but sometimes when I run the program, it pops up with this exceeds matrix error at:
matchedPoints1=matchedPoints1(match(:)&match(:)>0);
matchedPoints2=matchedPoints2(match(:)&match(:)>0);
Could someone help me?
%%Stereo Calibration and Scene Reconstruction
numImagePairs = 14;
imageFiles1 = cell(numImagePairs, 1);
imageFiles2 = cell(numImagePairs, 1);
imageDir = fullfile(matlabroot, 'toolbox', 'vision', 'visiondata', ...
'calibration', 'stereoWebcams');
for i = 1:numImagePairs
imageFiles1{i} = fullfile('160117',sprintf('l_%d.jpg', i));
imageFiles2{i} = fullfile('160117',sprintf('r_%d.jpg', i));
end
%%1) READ IN IMAGES
images1 = cast([], 'uint8');
images2 = cast([], 'uint8');
load 'Ldist.mat'
load 'Rdist.mat'
for i = 1:numel(imageFiles1)
im = apply_distortion_map(Lqdu,Lqdv,imread(imageFiles1{i}));
images1(:, :, :, i) = im;
im = apply_distortion_map(Rqdu,Rqdv,imread(imageFiles2{i}));
images2(:, :, :, i) = im;
end
% %%2) CALIBRATE THE STEREO SYSTEM
%
% % Detect the checkerboard in all stereo pairs of images.
[imagePoints, boardSize] = detectCheckerboardPoints(images1, images2);
%
% % Generate world coordinates of the checkerboard points.
squareSize = 40; % millimeters
worldPoints = generateCheckerboardPoints(boardSize, squareSize);
%
% % Compute the stereo camera parameters.
stereoParams = estimateCameraParameters(imagePoints, worldPoints);
%
% % Evaluate calibration accuracy.
figure;
showReprojectionErrors(stereoParams);
%
%
%%ABOVE STEPS CAN BE ELIMINATED BY USING 'STEREO CAMERA CALIBRATOR' APP IN MATLAB 2015 VERSION OR NEWER
%%3) RECTIFY A STEREO PAIR OF IMAGES
% Read in the stereo pair of images. CHANGE THE PATH ACCORDINGLY
I1 = apply_distortion_map(Lqdu,Lqdv,imread('l_18.jpg'));
I2 = apply_distortion_map(Rqdu,Rqdv,imread('r_18.jpg'));
J1 = apply_distortion_map(Lqdu,Lqdv,imread('l_18.jpg'));
J2 = apply_distortion_map(Rqdu,Rqdv,imread('r_18.jpg'));
% Rectify the images.
[J1, J2] = rectifyStereoImages(I1, I2, stereoParams, 'OutputView', 'valid');
% % Display the images before rectification.
figure;
subplot(2,1,1);
imshow(I1);
subplot(2,1,2);
imshow(I2);
title('Before Rectification');
imshow(stereoAnaglyph(I1, I2), 'InitialMagnification', 50);
title('Before Rectification');
%
% % Display the images after rectification.
% figure;
figure;
subplot(2,1,1);
imshow(J1);
subplot(2,1,2);
imshow(J2);
title('Before Rectification');
imshow(stereoAnaglyph(J1, J2), 'InitialMagnification', 50);
title('After Rectification');
%%4) COMPUTE DISPARITY FOR 3-D RECONSTRUCTION (DISPARITY MAP)
disparityRange = [0, 144];
%disparityMap = disparity(rgb2gray(J1), rgb2gray(J2), 'DisparityRange', ...
% disparityRange);
disparityMap = disparity(J1, J2, 'DisparityRange', ...
disparityRange);
figure;
imshow(disparityMap, disparityRange, 'InitialMagnification', 50);
colormap('jet');
colorbar;
title('Disparity Map(Before)');
%remove noise from disparityMap
%remove negative disparity
for i=1:(size(disparityMap,1)*size(disparityMap,2))
if disparityMap(i)<0
disparityMap(i)= 0;
end
end
for i=1:(size(disparityMap,1)*size(disparityMap,2)-size(disparityMap,1))
if (disparityMap(i)-(disparityMap(i+size(disparityMap,1)))) >1
disparityMap(i)=0;
end
end
figure;
imshow(disparityMap, disparityRange, 'InitialMagnification', 50);
colormap('jet');
colorbar;
title('Disparity Map(After)');
%%Correspondence searching using detectSURF
K1 = J1;
K2 = J2;
points1 = detectSURFFeatures(K1);
points2 = detectSURFFeatures(K2);
% Find matching points in SURF
%Extract the features.
[f1, vpts1] = extractFeatures(K1, points1);
[f2, vpts2] = extractFeatures(K2, points2);
%Retrieve the locations of matched points.
indexPairs = matchFeatures(f1, f2) ;
matchedPoints1 = vpts1(indexPairs(:, 1));
matchedPoints2 = vpts2(indexPairs(:, 2));
%Calculating disparity
m=size(matchedPoints1,1)-100;
for i = 1:m
Cam1(:,:,i) = matchedPoints1.Location([i i]);
Cam2(:,:,i) = matchedPoints2.Location([i i]);
distlr(i) = -(Cam1((2*i)-1) - Cam2((2*i)-1));
%
end
Num = 0;
% remove small distlr (outliers)
for i = 1:m
if distlr(i) < 5
distlr(i) = 0;
Num = Num + 1;
Cam1(:,:,i) = 0;
Cam2(:,:,i) = 0;
end
end
% remove large distlr (outliers)
for i = 1:m
if ((distlr(i) - sum(distlr(1:m))/(m-Num)) > 5)
distlr(i) = 0;
Num = Num + 1;
Cam1(:,:,i) = 0;
Cam2(:,:,i) = 0;
end
end
for i = 1:m
if (( sum(distlr(1:m))/(m-Num)-distlr(i))> 5)
distlr(i) = 0;
Num = Num + 1;
Cam1(:,:,i) = 0;
Cam2(:,:,i) = 0;
end
end
%zero all for match(i)
for i=1:m
match(i)= 0;
end
for i=1:m
if distlr(i) > 0
match(i)= i;
end
end
%Display the matching points.
figure;hold on
matchedPoints1=matchedPoints1(match(:)&match(:)>0);
matchedPoints2=matchedPoints2(match(:)&match(:)>0);
distlr=distlr(match(:)&match(:)>0);
% ax = axes;
% showMatchedFeatures(K1, K2, matchedPoints1(match(:)&match(:)>0), matchedPoints2(match(:)&match(:)>0),'montage', 'Parent', ax );
showMatchedFeatures(K1, K2, matchedPoints1, matchedPoints2);
% legend(ax,'Matched points 1','Matched points 2');
legend('Matched points 1','Matched points 2');
% figure; hold on
%
% ax = axes;
% showMatchedFeatures(K1, K2, matchedPoints1(match(:)&match(:)>0), matchedPoints2(match(:)&match(:)>0), 'Parent', ax );
% legend(ax,'Matched points 1','Matched points 2');
%
%Finding Depth
sensorsize = 4.65*10^(-6);
avgDist = sum(distlr(:))/size(distlr,2); %unit = pixels
avgFL = ((stereoParams.CameraParameters1.IntrinsicMatrix(1,1) + stereoParams.CameraParameters2.IntrinsicMatrix(1,1)) /2); %unit = pixels
baseline = (abs(stereoParams.TranslationOfCamera2(1)) * 10^(-3)); %unit = m
%z = fB/d
calDepth = ((avgFL/avgDist)*baseline); %unit = m
댓글 수: 1
It seems like this problem could be resolved by writing functions instead of scripts. No matter how much beginners love scripts, functions are much easier to work with, and avoid these kind of pointless problems:
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB Support Package for USB Webcams에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!