Dimensions of arrays being concatenated are not consisten on workspace

1 view (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 ?
clear;
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');
[~,jumlah_file_tbc]=bwlabel(bw2,4);
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');
%%end
end
array = [array;area];
cell_area(l,:) = {file_tbc(l).name, area};
hold off
end

Answers (1)

DGM
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?
  4 Comments
Voss
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.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!