Dimensions of arrays being concatenated are not consisten on workspace

조회 수: 1 (최근 30일)
fayza nayla
fayza nayla 2023년 1월 4일
댓글: Voss 2023년 1월 4일
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

답변 (1개)

DGM
DGM 2023년 1월 4일
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
fayza nayla
fayza nayla 2023년 1월 4일
it's still giving me the same error
Voss
Voss 2023년 1월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by