How to put name of images files on workspace ?
조회 수: 1 (최근 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 :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1250502/image.png)
댓글 수: 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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!