Index exceeds the number of array elements. Index must not exceed 4.
28 views (last 30 days)
Show older comments
cell_areas variable contains all of the files name of images in one folder and the sum or area that every image has. The problem is that in workspace, it only loads some images, not all of them.
Note : every image has their own variety in number of regions, in each image there could be 1 or 2 or 3 or so regions and i want to sum each of its area and save it to cell_areas variable
clear;
clear clc;
folder_nrml = ('/Users/*/Documents/MATLAB/regionbased_seg/cv/NORMAL');
file_nrml = dir(fullfile(folder_nrml, '*jpeg'));
jumlah_file_nrml = numel(file_nrml);
training_data_nrml = zeros(jumlah_file_nrml);
array = [];
for l = 1:jumlah_file_nrml
I = imread(fullfile(folder_nrml, file_nrml(l).name));
figure, imshow(I)
bw= imbinarize(I);
bw2 = imfill(bw,'holes');
s=regionprops(bw2, 'centroid', 'area');
centroids = cat(1, s.Centroid);
area = cat(1, s.Area);
sum_area = sum(area);
%%cell_area = cell(jumlah_file_tbc, 2);
hold on
plot(centroids(:,1), centroids(:,2),'r*')
[B,L]=bwboundaries(bw2, 'noholes');
[~,jumlah_file_nrml]=bwlabel(bw2,4);
cell_area = cell(jumlah_file_tbc, 2);
for k=1:jumlah_file_nrml
%%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; sum_area];
%cell_area(l,:) = cell(file_tbc(l).name,area);
cell_areas(l,:) = {file_nrml(l).name, sum_area};
hold off
end


1 Comment
Mathieu NOE
on 4 Jan 2023
hello
why are you doing the same for loop twice ?
the inner loop is for what ?
for k=1:jumlah_file_nrml
%%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
I would suppose there is only one for loop and the section I extracted above should be run in the main loop directly
so the code should look like :
clear;
clear clc;
folder_nrml = ('/Users/*/Documents/MATLAB/regionbased_seg/cv/NORMAL');
file_nrml = dir(fullfile(folder_nrml, '*jpeg'));
jumlah_file_nrml = numel(file_nrml);
training_data_nrml = zeros(jumlah_file_nrml);
array = [];
for l = 1:jumlah_file_nrml
I = imread(fullfile(folder_nrml, file_nrml(l).name));
figure, imshow(I)
bw= imbinarize(I);
bw2 = imfill(bw,'holes');
s=regionprops(bw2, 'centroid', 'area');
centroids = cat(1, s.Centroid);
area = cat(1, s.Area);
sum_area = sum(area);
%%cell_area = cell(jumlah_file_tbc, 2);
hold on
plot(centroids(:,1), centroids(:,2),'r*')
[B,L]=bwboundaries(bw2, 'noholes');
[~,jumlah_file_nrml]=bwlabel(bw2,4);
cell_area = cell(jumlah_file_tbc, 2);
% for k=1:jumlah_file_nrml
%%for k=1 : size(bw2, 1)
% boundary = B{k};
boundary = B;
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; sum_area];
%cell_area(l,:) = cell(file_tbc(l).name,area);
cell_areas(l,:) = {file_nrml(l).name, sum_area};
hold off
end
Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!