I am also confused on how to find the threshold value to isolate the two blobs from all the other junk in the background.
why am I receiving this error code when plotting motion over time?
3 次查看(过去 30 天)
显示 更早的评论
Hi I am using this code to plot motion over time in 2021b matlab:
% Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
vidObj = VideoReader('hand.MOV');
numberOfFrames = vidObj.NumFrames;
% numberOfFrames = 10 % For making testing quicker
xPaddle = nan(1, numberOfFrames);
yPaddle = nan(1, numberOfFrames);
xDot = nan(1, numberOfFrames);
yDot = nan(1, numberOfFrames);
for frameIndex = 1 : numberOfFrames
frame = read(vidObj, frameIndex);
subplot(2, 3, 1);
imshow(frame)
axis('on', 'image');
caption = sprintf('Original Image Frame %d of %d', frameIndex, numberOfFrames);
title(caption, 'fontSize', fontSize);
drawnow;
% Convert to gray scale
grayImage = rgb2gray(frame);
% Crop frame to get rid of white window to the right.
grayImage = grayImage(:, 1 : 500); % You need to determine the column
subplot(2, 3, 2);
imshow(grayImage)
axis('on', 'image');
impixelinfo;
caption = sprintf('Gray Scale Image Frame %d of %d', frameIndex, numberOfFrames);
title(caption, 'fontSize', fontSize);
drawnow;
%========================================================
% First find the white paddle.
% Threshold or call imbinarize()
paddleMask = imbinarize(grayImage);
% Fill holes
paddleMask = imfill(paddleMask, 'holes');
% Take largest blob only. That's the white paddle
paddleMask = bwareafilt(paddleMask, 1);
% We need to shrink the paddle because the edges are less bright and they
% get detected when we try to get the dot. So let's explude edges.
se = strel('disk', 15, 0);
paddleMask = imerode(paddleMask, se);
subplot(2, 3, 3);
axis('on', 'image');
imshow(paddleMask)
impixelinfo;
title('Paddle Mask', 'fontSize', fontSize);
drawnow;
% Find centroid of white paddle.
props = regionprops(paddleMask, 'Centroid');
% Skip it if the paddle can't be found, like it's too dark or something.
if isempty(props)
continue;
end
% Get x and y
xPaddle(frameIndex) = props.Centroid(1);
yPaddle(frameIndex) = props.Centroid(2);
%========================================================
% Now get the black dot.
thresholdValue = 220;
blackBlobs = grayImage < thresholdValue;
% Erase junk outside the paddle
blackBlobs = blackBlobs & paddleMask;
% Erase the axle.
blackBlobs(104:157, 264:319) = false;
% Note, black dot might not be resolved enough to find it!
% Take blobs only if they're in the 2-50 pixel range.
% That should get us the black dot.
props = regionprops(blackBlobs, 'Centroid', 'Area');
allAreas = [props.Area];
blackBlobs = bwareafilt(blackBlobs, [2, 70]);
% Sometimes there is noise, and we get 2 blobs, so just take the largest blob in that range.
blackBlobs = bwareafilt(blackBlobs, 1);
subplot(2, 3, 4);
imshow(blackBlobs)
impixelinfo;
axis('on', 'image');
title('Black Dot', 'fontSize', fontSize);
drawnow;
% Find centroid of black dot.
props = regionprops(blackBlobs, 'Centroid', 'Area');
allAreas = [props.Area];
% Skip it if the dot can't be found, like it's too blurred or something.
if isempty(props)
fprintf('No dot found on frame #%d of %d.\n', frameIndex, numberOfFrames);
continue;
end
% Get x and y
xDot(frameIndex) = props.Centroid(1);
yDot(frameIndex) = props.Centroid(2);
subplot(2, 3, 5:6);
plot(xDot, 'b.-', 'LineWidth', 1, 'MarkerSize', 15);
hold on;
plot(yDot, 'r.-', 'LineWidth', 1, 'MarkerSize', 15);
grid on;
title('Black Dot. X = Blue, y = red', 'fontSize', fontSize);
xlabel('Frame Number', 'fontSize', fontSize);
ylabel('Column or Row', 'fontSize', fontSize);
if frameIndex == 1
% Maximize the figure window.
g = gcf;
g.WindowState = 'maximized';
end
drawnow;
end
% Interpolate any missing ones.
missingIndexes = isnan(xDot);
if any(missingIndexes)
xFit = 1 : numberOfFrames;
xDot = interp1(find(~missingIndexes), xDot(~missingIndexes), xFit, 'spline');
yDot = interp1(find(~missingIndexes), yDot(~missingIndexes), xFit, 'spline');
subplot(2, 3, 5:6);
plot(xDot, 'b.-', 'LineWidth', 1, 'MarkerSize', 15);
hold on;
plot(yDot, 'r.-', 'LineWidth', 1, 'MarkerSize', 15);
grid on;
title('Black Dot. X = Blue, y = red', 'fontSize', fontSize);
xlabel('Frame Number', 'fontSize', fontSize);
% ylabel('Column or Row', 'fontSize', fontSize);
end
fprintf('Done running %s.m ...\n', mfilename);
I am recieving this error code:
No dot found on frame #1 of 375.
Intermediate dot '.' indexing produced a comma-separated list with 3 values, but it must produce a
single value when followed by subsequent indexing operations.
Error in motiontrackingandplotting (line 60)
xPaddle(frameIndex) = props.Centroid(1);
I believe this is due to my video but I am not sure what I should about my video that would resolve this error code. I cannot attatch the zip file because it is too large but here is figure 1 from the code showing the blobs and you can see that my hand is not detected:
How can I improve my video so that the code will detect my hand as the large blob. I have not yet added anything to be the smaller blob but will after I figure this part out.
回答(1 个)
Shree Charan
2023-9-7
Hi Sara,
In lines 59,60 and 93,94 ‘props’ is being dot indexed.
However, the ‘regionprops’ function returns a struct array (https://www.mathworks.com/help/images/ref/regionprops.html#buoixjn-4).
‘props’ contains multiple variables and hence by dot indexing it, you are trying to apply one index to multiple variables leading to the error “Intermediate dot '.' indexing produced a comma-separated list with 3 values, but it must produce a single value when followed by subsequent indexing operations”.
You may refer to this answer to understand this better: https://www.mathworks.com/matlabcentral/answers/1722130-intermediate-dot-indexing-produced-a-comma-separated-list-error-prevents-extracting-values-from?s_tid=answers_rc1-2_p2_MLT
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!