PDA

查看完整版本 : 由于矩阵大小,MATLAB不会提取第一行和第一列


poster
2019-12-10, 20:41
我正在跟踪一个抛在空中的对象,该对象控制着抛物线模式。我正在通过一系列30张图像跟踪对象。我设法排除了所有背景并保持对象清晰可见,然后使用其质心获取其坐标并进行绘制。现在,我应该预测对象将要下落的位置,因此我使用了polyfit和polyval。问题是, MATLAB (https://en.wikipedia.org/wiki/MATLAB)说:

???索引超出矩阵尺寸。

现在,质心创建了自己的具有一行和两列的结构。每当对象在循环中移动时,它只会更新第一行。

这是代码的一部分:

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 我收到的错误消息是:

??? Index exceeds matrix dimensions.

似乎是(1:N)引起了问题。老实说,我不知道为什么,但是当我删除N时,将绘制对象及其点,但是多边形拟合将不起作用。它给我一个错误,说:

Warning: Polynomial is not unique; degree >= number of data points. > In polyfit at 72 如果我做到了(1:N-1)之类的东西,它会在开始给我同样的错误之前绘制出更多的点(不是唯一的...)。但是我无法删除(1:N),因为我必须评估每个循环中多项式的系数(每个N的值),那么解决方案是什么?

编辑2:

这是我更多的代码

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 编辑3

显示对象方向的代码:

poly = polyfit(C1,C2,2); g = roots(poly); v = max(g) plot(xPlot,polyval(poly,xPlot),'y') plot(v,'go') 由于xPlot正确绘制了抛物线,但对于g (预测),一切都出错了...我使用错误的语法来获取最大值吗?

https://i.stack.imgur.com/nTSH9.jpg (https://i.stack.imgur.com/nTSH9.jpg)

我还意识到,如果我把plot(g,'g--')代替v,我会得到:想知道我是否可以使这个水平而不是垂直。然后,我将在一条带圆圈的直线上预测抛物线将停止。

https://i.stack.imgur.com/atJBP.jpg (https://i.stack.imgur.com/atJBP.jpg)



回答:

如果我正确理解了您想做什么,这就是您的代码的外观:

%# if there is only one centroid per image, preassign centroid array like this centroids = zeros(30,1); %# case A %# if there can be any number of centroids per image, preassign like this centroids = cell(30,1); %# case B for N=1:30 hold on I = figure.image.(['j' num2str(N)]); bw=(I); imshow(bw) ss = bwlabel(bw); s = regionprops(bw,'centroid'); %# for case A centroids(N,:) = cat(1, s.Centroid); %# for case B centroids{N} = cat(1,s.Centroid); hold(imgca,'on') %# case A plot(imgca,centroids(N,1), centroids(N,2),'r*') %# case B if ~isempty(centroids{N}) plot(imgca,centroids{N}(:,1), centroids{N}(:,2), 'r*'); end %# I don't think the following lines do anything useful x=centroids(1,1); y=centroids(1,2); points=plot(x,y,'bo',x,y,'rx'); %# update plots drawnow %# you can only do the fitting once you collected all centroids end %# case A - do nothing b/c centroids is already numeric %# case B - catenate centroids to make a numeric array with 2 columns centroids = cat(1,centroids{:}); C1=centroids(:,1); C2=centroids(:,2); %#set(points,'XData',C1,'YData',C2); poly=polyfit(C1,C2,2); %# you can use the output of polyval directly as y-coordinate parabola=plot(C1,polyval(poly,C1));

更多&回答... (https://stackoverflow.com/questions/2842468)