Index exceeds matrix dimensions.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
xinxin zhu
el 16 de En. de 2017
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 comentario
Respuesta aceptada
Walter Roberson
el 16 de En. de 2017
Your code is not written as a function. You do not initialize all arrays to definite size before you use them. When you run your code multiple times, sometimes the new arrays are going to be smaller than on the previous run, but because you did not initialize them, they are going to keep the size of the previous run. That can lead to indexing errors when you then use the entire array instead of just the part known to be valid.
Note:
Your code surrounding "match" could be much more efficient. It could just be
match = distlr > 0;
with no loops, and using it could be
matchedPoints1 = matchedPoints1(match);
matchedPoints2 = matchedPoints2(match);
In your current expression match(:)&match(:)>0, the first part is true for all non-zero entries in match, so it is only false for entries which are 0. The second part is only true for entries that are positive. But entries that are 0 are never positive, so the second part would also be false for 0, so the & part is redundant. Therefor match(:)>0 would be enough. But your match array is only 0 and 1, and in the case of 0 and 1, match(:)>0 is the same as match(:)~=0 which in turn is the same as just writing match(:) . As well, the value being indexed into is not two dimensional and neither is match, so the (:) does not add value:
>> ABC = 1:10
ABC =
1 2 3 4 5 6 7 8 9 10
>> ABC([3:5].')
ans =
3 4 5
>> ABC([3:5])
ans =
3 4 5
so using a column vector as an index does not matter. So you can skip the (:) and just use match as the index.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Support Package for USB Webcams en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!