Receiving this error code when extracting centroid and plotting motion
显示 更早的评论
I keep getting this error when running my code:
Intermediate dot '.' indexing produced a comma-separated list with 2 values, but
it must produce a single value when followed by subsequent indexing operations.
Error in referencemotionplot (line 57)
xhand1(frameIndex) = props.Centroid(1);
This is the code I am running on 2021b MATLAB to plot the coordinates of my hand motion:
% Clear the command windowv1
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
format compact
fontSize = 14;
vidObj = VideoReader('v3.MOV');
numberOfFrames = 50;
for frameIndex = 1 : numberOfFrames
frame = read(vidObj, frameIndex);
figure(1)
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()
hand = imbinarize(grayImage);
% Fill holes
hand = imfill(hand, 'holes');
% Take largest blob only. That's the white paddle
hand = bwareafilt(hand, 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);
hand = imerode(hand, se);
figure(2)
subplot(2, 3, 3);
axis('on', 'image');
imshow(hand)
impixelinfo;
title('hand', 'fontSize', fontSize);
drawnow;
% Find centroid of white paddle.
props = regionprops(hand, '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
xhand1(frameIndex) = props.Centroid(1);
yhand1(frameIndex) = props.Centroid(2);
%========================================================
% Now get the black dot.
thresholdValue = 0;
whiteBlobs = grayImage > thresholdValue;
% Erase junk outside the paddle
whiteBlobs = whiteBlobs & hand;
% Erase the axle.
whiteBlobs(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(whiteBlobs, 'Centroid', 'Area');
allAreas = [props.Area];
whiteBlobs = bwareafilt(whiteBlobs,1, "smallest");
% Sometimes there is noise, and we get 2 blobs, so just take the largest blob in that range.
whiteBlobs = bwareafilt(whiteBlobs, 1);
figure(2)
subplot(2, 3, 4);
imshow(whiteBlobs)
impixelinfo;
axis('on', 'image');
title('Black Dot', 'fontSize', fontSize);
drawnow;
% Find centroid of black dot.
props = regionprops(whiteBlobs, '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
figure(3)
xDot(frameIndex) = props.Centroid(1);
yDot(frameIndex) = props.Centroid(2);
subplot(2, 3, 5:6);
plot(xDot, yDot, 'r');
hold on;
grid off;
title('verticalplot_4');
xlabel('x', 'fontSize', fontSize);
ylabel('y', 'fontSize', fontSize);
xlim([100 500])
ylim([1000 2000])
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,yDot);
hold on;
grid on;
title('horizontal_plot1. X = Blue, y = red', 'fontSize', fontSize);
xlabel('Frame Number', 'fontSize', fontSize);
ylabel('Column or Row', 'fontSize', fontSize);
end
%%
xyDot=[xDot;yDot];
save('v16''xyDot')
filename='v16.mat';
save(filename,"xyDot");
Here is the video I am using:
Does anyone know why I am getting this error?
采纳的回答
更多回答(2 个)
Image Analyst
2023-3-22
0 个投票
Not having run the code, it looks like your binary image may have two blobs in it. Even though you used bwareafilt to get just one blob, you then eroded it which may have produced two or more blobs. If you're expecting just one blob, then you had better call bwareafilt again to get just one blob.
8 个评论
Sara K Takiguchi
2023-3-22
Image Analyst
2023-3-22
I can't download that from YouTube. Can you just attach the video here with the paperclip icon?
Sara K Takiguchi
2023-3-22
Sara K Takiguchi
2023-3-22
Image Analyst
2023-3-23

I downloaded the video and your code. The code seems to have almost nothing to do with the video, but it ran without throwing that error you gave.. I have no idea what the white spots and black dots referred to are. Also I have no idea how you're defining the centroid of your hand since it's connected to the arm. How are you dividing the hand from the arm?
Sara K Takiguchi
2023-3-23
Image Analyst
2023-3-24
You can use the color thresholder on the apps tab of the tool ribbon to get code to mask out that paper on your hand. See attached demo where I track a green sharpie marker.
If you still can't figure it out, let me know.
Sara K Takiguchi
2023-3-31
类别
在 帮助中心 和 File Exchange 中查找有关 Image Segmentation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
