How to put name of images files on workspace ?
2 views (last 30 days)
Show older comments
i have this code to store the number of white pixels that multiple images have. i store it in count variable and it worked but i also want it to also put the file name of images on workspace instead of only showing the data of number of white pixels in variable count so i could see which number of white pixels each image have.
folder_nrml = ('/Users/*/Documents/MATLAB/regionbased_seg/cv/NORMAL');
file_nrml = dir(fullfile(folder_nrml, '*jpg'));
jumlah_file_nrml = numel(file_nrml);
training_data_nrml = zeros(jumlah_file_nrml);
count = [];
for k = 1:jumlah_file_nrml
I = imread(fullfile(folder_nrml, file_nrml(k).name));
BW1 = edge(I, 'canny');
%%imshow(BW1);
[m,n]= size(BW1);
%%count = [];
calc = 0;
for i = 1 : m
for j = 1 : n
if BW1(i,j) == 1
calc = calc+1;
%%else
end
end
end
count = [count;calc];
imwrite(BW1,sprintf('/Users/*/Documents/MATLAB/regionbased_seg/final/NORMAL/MASK_0%d.jpg',k));
end
here is how the data store on count variable :
2 Comments
MarKf
on 3 Jan 2023
This is anyway another basic matlab operations question, so do take some time to look at the resources, I assure you it'd save time, and I do mean yours.
The name you want to add next to the numbers is a string, so you'd need a different kind of variable than double (just numbers). You could make a cell ( cell_count(k,:) = {file_nrml(k).name, calc}; no need to preallocate) or a table or fprintf what you need
Accepted Answer
cr
on 3 Jan 2023
Initialise
count = {};
instead of count = [];
then replace the count update before imwrite() as
count(end+1,:) = {file_nrml(k).name,calc};
More Answers (1)
Mathieu NOE
on 3 Jan 2023
hello
I have not the Image Processing Tbx , so I cannot fully use your code , but I believe my suggestions should work
first suggestion, no need for the two inner for loops to find the number of white pixel
this can be done in one line without any for loop
the second suggestio is to store the filename and the count result in a cell array so you have both infos
folder_nrml = ('/Users/*/Documents/MATLAB/regionbased_seg/cv/NORMAL');
file_nrml = dir(fullfile(folder_nrml, '*jpg'));
jumlah_file_nrml = numel(file_nrml);
training_data_nrml = zeros(jumlah_file_nrml);
count = [];
for k = 1:jumlah_file_nrml
filename = file_nrml(k).name;
I = imread(fullfile(folder_nrml, file_nrml(k).name));
BW1 = edge(I, 'canny');
% %%imshow(BW1);
% [m,n]= size(BW1);
%%count = [];
% calc = 0;
% for i = 1 : m
% for j = 1 : n
% if BW1(i,j) == 1
% calc = calc+1;
% %%else
% end
% end
calc = numel(find(BW1>0)); % or == 1 ?
out{k,1} = filename;
out{k,2} = calc;
imwrite(BW1,sprintf('/Users/*/Documents/MATLAB/regionbased_seg/final/NORMAL/MASK_0%d.jpg',k));
end
See Also
Categories
Find more on Convert Image Type 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!