Dimensions of arrays being concatenated are not consisten on workspace

3 views (last 30 days)
i tried this code below and i still got an error and only load some images not all of them. every image has their own number of region so in one image, it could be 2, 3, or 6 areas.
or maybe if it's easier, how to just sum all of areas of each region in each image ?
folder_tbc = ('/Users/*/Documents/MATLAB/regionbased_seg/cv/TBC');
file_tbc = dir(fullfile(folder_tbc, '*png'));
jumlah_file_tbc = numel(file_tbc);
training_data_tbc = zeros(jumlah_file_tbc);
array = [];
for l = 1:jumlah_file_tbc
I = imread(fullfile(folder_tbc, file_tbc(l).name));
figure, imshow(I)
bw= imbinarize(I);
bw2 = imfill(bw,'holes');
s=regionprops(bw2, 'centroid', 'area');
centroids = cat(1, s.Centroid);
area = cat(2, s.Area);
hold on
plot(centroids(:,1), centroids(:,2),'r*')
[B,L]=bwboundaries(bw2, 'noholes');
for k=1:jumlah_file_tbc
%%for k=1 : size(bw2, 1)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'y', 'LineWidth',2)
text(boundary(1,2), boundary(1,1), strcat(['Area =', num2str(area(k))]),'Color', 'r', 'FontSize',15, 'FontWeight','bold');
array = [array;area];
cell_area(l,:) = {file_tbc(l).name, area};
hold off

Answers (1)

DGM on 4 Jan 2023
Here, you're concatenating the object areas to form a variable-length row vector.
area = cat(2,s.Area);
Here, you're trying to vertically concatenate the variable length rows.
array = [array;area];
If you want to have an array where each row corresponds to each image, then you might use a cell array instead. It seems you already have that as cell_area. You could preallocate cell_area:
cell_area = cell(jumlah_file_tbc,2); % preallocate
As to what to do with "array", I don't really know why you need it if you have the results stored in cell_area. If you want to keep a running sum or something, you could do that in the loop or simply end concatenate all the variable-length area vectors and then sum it at the end?
Voss on 4 Jan 2023
You also need to comment-out or delete the line:
array = [array;area];
because the idea was to replace "array" with "cell_area", since "array" cannot be built the way you want, since "area" is of varying length.

Sign in to comment.


Find more on Matrices and Arrays in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!