im trying to run matlab video processing code and getting this error "Index in position 1 exceeds array bounds (must not exceed 1).
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
clear all
clc
% Create a VideoCapture object
cap = webcam;
% Create the background subtractor object
% Use the last 700 video frames to build the background
back_sub = vision.ForegroundDetector('NumTrainingFrames', 700, 'InitialVariance', 25);
% Create a structuring element for morphological operations
% You can tweak the dimensions of the element
% e.g. instead of [20, 20], you can try [30, 30].
se = strel('rectangle', [20, 20]);
while(true)
% Capture frame-by-frame
frame = snapshot(cap);
% Use every frame to calculate the foreground mask and update
% the background
fg_mask = step(back_sub, frame);
% Close dark gaps in foreground object using closing
fg_mask = imclose(fg_mask, se);
% Remove salt and pepper noise with a median filter
fg_mask = medfilt2(fg_mask, [5, 5]);
% % Threshold the image to make it either black or white
fg_mask = uint32(fg_mask);
fg_mask = (imbinarize(fg_mask));
% % Find the index of the largest contour and draw bounding box
fg_mask_bb = fg_mask;
stats = regionprops(fg_mask_bb, 'Area', 'BoundingBox', 'Centroid');
areas = [stats.Area];
% If there are no contours
if isempty(areas)
% Display the resulting frame
imshow(frame);
% % If any key is pressed on the keyboard,
% % exit this loop
% key = getkeywait(1);
% if ~isempty(key)
% break;
% end
% Go to the top of the while loop
continue;
else
% Find the largest moving object in the image
[~, max_index] = max(areas);
end
clear cap;
% Draw the bounding box
x = round(stats(max_index).BoundingBox(1));
y = round(stats(max_index).BoundingBox(2));
w = round(stats(max_index).BoundingBox(3));
h = round(stats(max_index).BoundingBox(4));
rectangle('Position', [x, y, w, h], 'EdgeColor', [0, 0, 1], 'LineWidth', 3);
% Draw circle in the center of the bounding box
x2 = x + round(w/2);
y2 = y + round(h/2);
viscircles([x2, y2], 4, 'EdgeColor', [0, 1, 0], 'LineWidth', 2);
% Print the centroid coordinates (we'll use the center of the
% bounding box) on the image
text = sprintf('x: %d, y: %d', x2, y2);
textLocation = [x2 - 10, y2 - 10];
textH = text('Position', textLocation, 'String', text, 'FontSize', 12, 'Color', [0, 1, 0]);
% Display the resulting frame
imshow(frame);
% If any key is pressed on the keyboard,
% exit this loop
key = getkeywait(1);
if ~isempty(key)
break;
end
end
% Close down the video stream
clear cap;
this code is working pretty well however, this is error im getting.
Index in position 1 exceeds array bounds (must not exceed 1).
Error in imagepro (line 74)
textH = text('Position', textLocation, 'String', text, 'FontSize', 12, 'Color', [0, 1, 0]);
0 Commenti
Risposte (2)
recent works
il 4 Ott 2023
The error you are encountering, "Index in position 1 exceeds array bounds (must not exceed 1)," suggests that there is an issue with the index you are trying to access. In your code, the error occurs at line 74 when you are trying to create a text object.
Based on your code, it appears that you are trying to display text on an image. The error message suggests that the textLocation variable might be exceeding the array bounds. This usually happens when textLocation is a 2-element vector representing the x and y coordinates, and one of these values is not within the valid bounds.
To resolve this issue, you should check the values of x2 and y2 that you are using to set the textLocation. Make sure that these values are within the valid bounds of your image. You can use size(frame) to get the dimensions of your image and then ensure that x2 and y2 are within these bounds.
% ...
while(true)
% Capture frame-by-frame
frame = snapshot(cap);
% Use every frame to calculate the foreground mask and update
% the background
fg_mask = step(back_sub, frame);
% Close dark gaps in foreground object using closing
fg_mask = imclose(fg_mask, se);
% Remove salt and pepper noise with a median filter
fg_mask = medfilt2(fg_mask, [5, 5]);
% Threshold the image to make it either black or white
fg_mask = uint32(fg_mask);
fg_mask = (imbinarize(fg_mask));
% Find the index of the largest contour and draw bounding box
fg_mask_bb = fg_mask;
stats = regionprops(fg_mask_bb, 'Area', 'BoundingBox', 'Centroid');
areas = [stats.Area];
% If there are no contours
if isempty(areas)
% Display the resulting frame
imshow(frame);
% Go to the top of the while loop
continue;
else
% Find the largest moving object in the image
[~, max_index] = max(areas);
end
% Draw the bounding box
x = round(stats(max_index).BoundingBox(1));
y = round(stats(max_index).BoundingBox(2));
w = round(stats(max_index).BoundingBox(3));
h = round(stats(max_index).BoundingBox(4));
% Check if x and y are within image bounds
x2 = x + round(w/2);
y2 = y + round(h/2);
[imageHeight, imageWidth, ~] = size(frame);
if x2 < 1
x2 = 1;
elseif x2 > imageWidth
x2 = imageWidth;
end
if y2 < 1
y2 = 1;
elseif y2 > imageHeight
y2 = imageHeight;
end
% Draw circle in the center of the bounding box
viscircles([x2, y2], 4, 'EdgeColor', [0, 1, 0], 'LineWidth', 2);
% Print the centroid coordinates (we'll use the center of the
% bounding box) on the image
text = sprintf('x: %d, y: %d', x2, y2);
textLocation = [x2 - 10, y2 - 10];
textH = text('Position', textLocation, 'String', text, 'FontSize', 12, 'Color', [0, 1, 0]);
% Display the resulting frame
imshow(frame);
% If any key is pressed on the keyboard,
% exit this loop
key = getkeywait(1);
if ~isempty(key)
break;
end
end
% ...
0 Commenti
Walter Roberson
il 4 Ott 2023
text = sprintf('x: %d, y: %d', x2, y2);
After that line text is now a variable rather than a function.
0 Commenti
Vedere anche
Categorie
Scopri di più su Detection in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!