How to store the result of areas on every images on loop in one folder to workspace?

3 ビュー (過去 30 日間)
fayza nayla
fayza nayla 2023 年 1 月 5 日
回答済み: Abhinav Aravindan 2024 年 12 月 3 日
i keep getting an error and it only loaded some of the image not all of them, i have like 600+ files but it only loaded for 19 images idk why
clc; clear; close all;
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);
calc = [];
for l = 1:jumlah_file_nrml
I = imread(fullfile(folder_nrml, file_nrml(l).name));
figure, imshow(I)
bw1= im2bw(I);
bw2 = imfill(bw1,'holes');
s = regionprops(bw2, 'centroid', 'area');
centroids = cat(1, s.Centroid);
area = cat(1, s.Area);
sum_area = sum(area);
hold on
plot(centroids(:, 1), centroids(:,2), 'r*')
[B,L] = bwboundaries(bw2, 'noholes');
[~,l]= bwlabel(bw2,4);
for k = 1:l
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
calc = [calc;sum_area];
cell_areas(l,:) = {file_nrml(l).name, sum_area};
hold off
end
here are the result and error

回答 (1 件)

Abhinav Aravindan
Abhinav Aravindan 2024 年 12 月 3 日
The “bwboundaries” function returns the coordinates of boundary vertices to “B” as a p-by-1 cell array, where "p" is the number of objects and holes. The “bwlabel” function returns the number of connected objects in the input binary image to the variable “l” in your code.
The error you are encountering might be because, for this particular image file, the number of connected objects "l" is greater than the number of boundary vertices returned in "B". Hence, when the value of loop variable "k" increases to a value larger than size of "B", an index greater than the size of the cell array is being accessed resulting in this error.
Additionally, it is to be noted that the same variable “l” is being used twice in the code: as the loop iteration variable and as the output of the “bwlabel” function which can cause an issue.
To resolve this error, you may try replacing the code snippet to plot the boundaries as follows:
for k = 1:size(B, 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
Please find below the related documentation for your reference:

カテゴリ

Help Center および File ExchangeFractals についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by