How to put name of images files on workspace ?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
MarKf
2023-1-3
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
采纳的回答
cr
2023-1-3
Initialise
count = {};
instead of count = [];
then replace the count update before imwrite() as
count(end+1,:) = {file_nrml(k).name,calc};
更多回答(1 个)
Mathieu NOE
2023-1-3
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!