Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Why won't my video processing code work!

1 Ansicht (letzte 30 Tage)
TT Simmons
TT Simmons am 17 Apr. 2014
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I've created the below code to take apart a video and process the individual frames to identify the center of a circle moving around on a background.
The script works fine when I use it on just one frame but when I try it as a full vide I get the following error...
"Field reference for multiple structure elements that is followed by more reference blocks is an error."
It apparently doesn't like these lines.
X = floor(CircleLocations.Centroid(1));
Y = floor(CircleLocations.Centroid(2));
As I say this worked perfectly fine when doing just 1 frame and using imshow to see the result so I've no idea what's wrong with this one, if anyone can shed some light on this it'd be much appreciated.
Full code below.
%create an object for the video
CircleOb = VideoReader('IMG_0752.mov');
sedisk = strel('disk',50);
nframes = get(CircleOb, 'NumberOfFrames');
I = read(CircleOb, 1);
for k = 1 : nframes
singleframe = (read(CircleOb, k));
%greyscale read of frame 1 of circleob
grayscaled = rgb2gray(read(CircleOb, k));
%Identifies everything below a certain threshold, in this case 100
Binarised = imextendedmin(grayscaled, 100);
% remove from binarised anthing smaller or different in shape than sedisk
smallobjectsremoved = imopen(Binarised, sedisk);
% this gets the region properties of the Binarised image, in this case the
% 'centroid' which is the X and Y coordinates of the center of the object.
CircleLocations = regionprops(smallobjectsremoved, 'Centroid');
%round them up so they can be planted on an actual pixel value
X = floor(CircleLocations.Centroid(1));
Y = floor(CircleLocations.Centroid(2));
singleframe(Y, X+1, :) = [0, 255, 0]; % r, g, b are uint8 values.
singleframe(Y, X+2, :) = [0, 255, 0]; % r, g, b are uint8 values.
singleframe(Y, X-1, :) = [0, 255, 0]; % r, g, b are uint8 values.
singleframe(Y, X-2, :) = [0, 255, 0]; % r, g, b are uint8 values.
end
frameRate = get(CircleOb,'FrameRate');
implay(singleframe,frameRate);
  1 Kommentar
Image Analyst
Image Analyst am 17 Apr. 2014
Can you attach IMG_0752.mov, and your m-file so we can try it ourselves?

Antworten (1)

Image Analyst
Image Analyst am 17 Apr. 2014
Bearbeitet: Image Analyst am 17 Apr. 2014
Are you looking at smallobjectsremoved? I don't see that you're checking this for the case where there are no blobs in it. That's not a very robust way to write code. You need to handle situations that might possibly arise and that seems like a likely one.
This doesn't look right:
CircleLocations.Centroid(1)
What if there are two blobs? You'd need CircleLocations(2).Centroid(1) to refer to the second one.
what if CircleLocations has multiple blobs? You'd need an array. Here's a better way . . . From my Image Segmentation Tutorial http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 you can do this:
% We can get the centroids of ALL the blobs into 2 arrays,
% one for the centroid x values and one for the centroid y values.
allBlobCentroids = [blobMeasurements.Centroid];
centroidsX = allBlobCentroids(1:2:end-1);
centroidsY = allBlobCentroids(2:2:end);
Chances are that your problem is you either have more than 1 blob or no blobs at all. Your mistake was in thinking that you will always have exactly one blob, and that's not a robust way to write code, and thus you will eventually encounter errors.

Community Treasure Hunt

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

Start Hunting!

Translated by