Index in position 1 exceeds array bounds. Index must not exceed 5. Error in CV (line 63)

3 ビュー (過去 30 日間)
Tejaswini Bharatha
Tejaswini Bharatha 2022 年 4 月 3 日
回答済み: Omega 2023 年 12 月 22 日
my code
% Load images.
PhotoDir = fullfile("TOOLBOX_calib/calib_example/");
PhotoScene = imageDatastore(PhotoDir);
% Display images to be stitched.
montage(PhotoScene.Files)
% Read the first image from the image set.
I = readimage(PhotoScene,1);
%I = imrotate(I,-90);
% Initialize features for I(1)
grayImage = im2gray(I);
[y,x,m] = harris(grayImage,500,'tile',[15 15],'disp');
points=[x,y];
[features, points] = extractFeatures(grayImage,points);
numImages = numel(PhotoScene.Files);
tforms(numImages) = projective2d(eye(3));
% Initialize variable to hold image sizes.
imageSize = zeros(numImages,2);
% Iterate over remaining image pairs
for n = 2:numImages
% Store points and features for I(n-1).
pointsPrevious = points;
featuresPrevious = features;
% Read I(n).
I = readimage(PhotoScene, n);
I = imrotate(I,-90);
% Convert image to grayscale.
grayImage = im2gray(I);
% Save image size.
imageSize(n,:) = size(grayImage);
% Detect and extract SURF features for I(n).
[y,x,m] = harris(grayImage,500,'tile',[6 6],'disp');
points=[x,y];
[features, points] = extractFeatures(grayImage, points);
% Find correspondences between I(n) and I(n-1).
indexPairs = matchFeatures(features, featuresPrevious, 'Unique', true);
matchedPoints = points(indexPairs(:,1), :);
matchedPointsPrev = pointsPrevious(indexPairs(:,2), :);
% Estimate the transformation between I(n) and I(n-1).
tforms(n) = estimateGeometricTransform2D(matchedPoints, matchedPointsPrev,...
'projective', 'Confidence', 99.9, 'MaxNumTrials', 500);
% Compute T(n) * T(n-1) * ... * T(1)
tforms(n).T = tforms(n).T * tforms(n-1).T;
end
% Compute the output limits for each transform.
for i = 1:numel(tforms)
[xlim(i,:), ylim(i,:)] = outputLimits(tforms(i), [1 imageSize(i,2)], [1 imageSize(i,1)]);
end
avgXLim = mean(xlim, 2);
[~,idx] = sort(avgXLim);
centerIdx = floor((numel(tforms)+1)/2);
centerImageIdx = idx(centerIdx);
Tinv = invert(tforms(centerImageIdx));
for i = 1:numel(tforms)
tforms(i).T = tforms(i).T * Tinv.T;
end
for i = 1:numel(tforms)
[xlim(i,:), ylim(i,:)] = outputLimits(tforms(i), [1 imageSize(i,2)], [1 imageSize(i,1)]);
end
maxImageSize = max(imageSize);
% Find the minimum and maximum output limits.
xMin = min([1; xlim(:)]);
xMax = max([maxImageSize(2); xlim(:)]);
yMin = min([1; ylim(:)]);
yMax = max([maxImageSize(1); ylim(:)]);
% Width and height of panorama.
width = round(xMax - xMin);
height = round(yMax - yMin);
% Initialize the "empty" panorama.
panorama = zeros([height width 3], 'like', I);
blender = vision.AlphaBlender('Operation', 'Binary mask', ...
'MaskSource', 'Input port');
% Create a 2-D spatial reference object defining the size of the panorama.
xLimits = [xMin xMax];
yLimits = [yMin yMax];
panoramaView = imref2d([height width], xLimits, yLimits);
% Create the panorama.
for i = 1:numImages
I = readimage(buildingScene, i);
I = imrotate(I,-90);
% Transform I into the panorama.
warpedImage = imwarp(I, tforms(i), 'OutputView', panoramaView);
% Generate a binary mask.
mask = imwarp(true(size(I,1),size(I,2)), tforms(i), 'OutputView', panoramaView);
% Overlay the warpedImage onto the panorama.
panorama = step(blender, panorama, warpedImage, mask);
end
figure
imshow(panorama)

回答 (1 件)

Omega
Omega 2023 年 12 月 22 日
Hi Tejaswini,
It appears that you have encountered an indexing error in your MATLAB code while trying to stitch images together to create a panorama. The error message suggests that you are trying to access an index of an array that does not exist, which typically happens when the index is larger than the array size.
From the code provided, it seems that the error is related to the variable "tforms", which is used to store projective transformations for each image. However, there are a few issues in the code that need to be addressed:
  1. The variable "PhotoScene" is used to read images, but later in the loop, you are attempting to read from "buildingScene" instead, which is not defined in the provided code. You should use "PhotoScene" consistently.
  2. The "tforms" array is initialized with a size based on "numImages", but the "imageSize" array is not initialized before the loop that populates it. This might lead to an index out-of-bounds error if "numImages" is greater than the default size of uninitialized arrays in MATLAB.
  3. The "tforms" array is being used inside a loop with an index i that starts from 1, which should be correct, but if "numImages" is incorrect or not properly calculated, it could lead to an index out-of-bounds error.
Try to debug your code and check if the "numImages" is calculated as expected.
Hope it helps!

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by