I am tracking an object that is thrown in the air, and this object governs a parabolic pattern. I'm tracking the object through a series of 30 images. I managed to exclude all the background and keep the object apparent, then used its centroid to get its coordinates and plot them. Now I'm supposed to predict where the object is going to fall, so I used polyfit & polyval. The problem is, MATLAB says:
??? Index exceeds matrix dimensions.
Now the centroid creates its own structure with a row and two columns. Everytime the object moves in the loop, it updates the first row only.
Here is part of the code:
For N = 1:30
.
.
.
x = centroid(1,1); % extract first row and column for x
y = centroid(1,2); % extract secnd row and column for x
plot_xy = plot(x,y)
set(plot_xy,'XData',x(1:N),'YData',y(1:N));
fitting = polyfit(x(1:N),y(1:N),2);
parabola = plot(x,nan(23,1));
evaluate = polyval(fitting,x);
set(parabola,'YData',evaluate)
.
.
end
The error message I get is:
??? Index exceeds matrix dimensions.
It seems that (1:N) is causing the problems. I honestly do not know why, but when I remove N, the object is plotted along with its points, but polyfitting won't work. It gives me an error saying:
Warning: Polynomial is not unique; degree >= number of
data points.
> In polyfit at 72
If I made it (1:N-1) or something, it plots more points before it starts giving me the same error (not unique ...). But I can't remove (1:N), because I have to evaluate the coefficients of the polynomial in each loop (each value of N), so what's the solution?
Edit 2 :
This is more of my code
for N = 1:30
hold on
I = figure.image.(['j' num2str(N)]);
bw = (I);
imshow(bw)
ss = bwlabel(bw);
s = regionprops(bw,'centroid');
centroids = cat(1, s.Centroid);
hold(imgca,'on')
plot(imgca,centroids(1,1), centroids(1,2),'r*')
x = centroids(1,1);
y = centroids(1,2);
points = plot(x,y,'bo',x,y,'rx');
hold on;
for N>3
C1 = centroids(1,1);
C2 = centroids(1,2);
set(points,'XData',C1,'YData',C2);
poly = polyfit(C1,C2,2);
parabola = plot(C1,nan(size(centroids,1),1));
pval = polyval(poly,x);
set(parabola,'YData',pval);
pause(0.5)
end
end
Edit 3
Code to display object distination:
poly = polyfit(C1,C2,2);
g = roots(poly);
v = max(g)
plot(xPlot,polyval(poly,xPlot),'y')
plot(v,'go')
The parabola line is plotted correctly due to xPlot, but for g
(the prediction), it all goes wrong... Am I using the wrong syntax to get the max value?
I also realized if I put plot(g,'g--')
instead of v, I get:
Wonder if I can get this horizontal instead of vertical. Then I will have a line with a circle where the parabola is predicted to stop.