Matlab code crashes and gives error: Dimension of matrices being concatenated are not consistent

124 views Asked by At

Kindly have a look at this code and help me out. I am doing something wrong with the Matlab line command. 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
1

There are 1 answers

4
rayryeng On

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. The sizes are inconsistent and so that's why you're getting that error. I don't know what shape centroids is (i.e. 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. When you use line you need to provide the x locations and ending y locations for each segment of the line that you want.

Assuming that the first row/column is the x coordinates and the second row/column is the y coordinates, do this:

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. 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.