[英]Matlab code crashes and gives error: Dimension of matrices being concatenated are not consistent
Kindly have a look at this code and help me out. 请看一下这段代码,帮帮我。 I am doing something wrong with the Matlab line command. 我用Matlab line命令做错了。 The code give warnings and then crashes. 该代码给出警告,然后崩溃。 The error is in the 2nd last line. 错误在第二行。
while ~isDone(videoSource)
frame = readFrame(videoSource);
mask = detectObjects(frame,Fgdetector);
[areas, centroids, bboxes]= step(blobAnalyser,mask);
% tracing boundires around the detected obbjects
% BW = im2bw(I, graythresh(I));
[B,L] = bwboundaries(mask,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
%for star skeleton
x = boundary(:,2);
y = boundary(:,1);
indexes = convhull(x, y);
hold on;
% plot(x(indexes), y(indexes), 'm-', 'LineWidth', 2);
line([x(indexes(k)), centroids], [y(indexes(k)),centroids ], 'Color', 'r', 'LineWidth', 2);
end
What I suspect is happening is that centroids
is a N x 2 array, or a 2 x N array and you are trying to concatenate a matrix with a single value to create another matrix. 我怀疑发生的是centroids
是一个N x 2数组或2 x N数组,并且您试图将一个具有单个值的矩阵连接起来以创建另一个矩阵。 The sizes are inconsistent and so that's why you're getting that error. 大小不一致,所以这就是为什么您会收到该错误。 I don't know what shape centroids
is (ie if it's N x 2
or 2 x N
) because detectObjects
is something you wrote, or a function that exists in a later version of MATLAB that I don't have access to, so one of these should work. 我不知道形centroids
是什么形状(即,它是N x 2
还是2 x N
),因为detectObjects
是您编写的东西,或者是我无法访问的更高版本的MATLAB中存在的函数,因此这些应该工作。 When you use line
you need to provide the x
locations and ending y
locations for each segment of the line that you want. 使用line
,需要为所需线条的每个线段提供x
位置和y
结尾位置。
Assuming that the first row/column is the x
coordinates and the second row/column is the y
coordinates, do this: 假设第一行/列是x
坐标,第二行/列是y
坐标,请执行以下操作:
centroids - N x 2
line(centroids(:,1), centroids(:,2), 'Color', 'r', 'LineWidth', 2);
centroids - 2 x N
line(centroids(1,:), centroids(2,:), 'Color', 'r', 'LineWidth', 2);
As a minor note, the x
and y
variables in the line
call look like they are coordinates for the convex hull of your shapes. 作为次要说明, line
调用中的x
和y
变量看起来像是形状凸包的坐标。 You are trying to combine these with the centroids... which doesn't really make any sense. 您正在尝试将它们与质心结合起来……这实际上没有任何意义。 Stick with drawing one or the other. 坚持画一个或另一个。 If you want to draw both, then make two separate line calls: 如果要同时绘制两者,请进行两个单独的线路调用:
line(centroids(:,1), centroids(:,2), 'Color', 'r', 'LineWidth', 2);
% or
% line(centroids(1,:), centroids(2,:), 'Color', 'r', 'LineWidth', 2);
line(x(indexes), y(indexes), 'Color', 'r', 'LineWidth', 2);
Don't mix apples and oranges together. 不要将苹果和橙子混合在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.