imread figures from new folder created

3 次查看(过去 30 天)
I created figures in the folder. then, I want to read figures again to calculate, and then save .xls file in the folder created. how to do this? this is my code, but it is not working:
for ii=0:10:360
for jj=0:10:90
view(ii,jj);
%%%create a multiple folder
newdir=sprintf('view%2dview%2d',ii,jj);
mkdir(fullfile(newdir));
%rotate masks
for i=1:36
rotate(aa,[0,0,1],1);
% print figure with rotate
filename = sprintf('Slice_%d.png', i);
print('-dpng', fullfile(newdir, filename));
pause(2);
end
%%%%%%%%calculate red pixels and save in excel file
%%%%%%%%%%%%%%%
for i = 1 : 36
filename=['D:\Matlab code RMC\zzzz\newdir\Slice_ ' num2str(i,'%01d') ];
rgbImage = imread(filename);
imshow(rgbImage);
redChannel = rgbImage(:,:, 1);
greenChannel = rgbImage(:,:, 2);
blueChannel = rgbImage(:,:, 3);
greenness = mean2(greenChannel);
% Extract the individual red, green, and blue color channels.
redPixels = redChannel == 255 & greenChannel == 0 & blueChannel == 0;
redArea(i) = sum(redPixels(:));
end
%I want to save .xls into the folder created view(ii)view(jj)
filename='test.xlsx';
A=[redArea];
xlswrite(filename,A);

回答(1 个)

Vidhi Agarwal
Vidhi Agarwal 2024-8-23
Hi Viet
I understand you are trying to create figures, save them as images, and then calculate some properties of these images to save the results in an Excel file. Some enhancements to implement in existing code are:
  • Path Management: Make sure to use the correct paths when saving and reading files. Use “fullfile” to construct paths.
  • Image Saving and Reading: Ensure to correctly save and read the images. The path should be consistent.
  • Excel File Saving: Use the correct path when saving the Excel file within the newly created directories.
  • Loop and Path Variables: Ensure the variables used in paths are correctly defined.
Here is the enhanced code following the same:
% Example of creating a plot and storing its handle in 'aa'
[x, y, z] = sphere; % Create a sphere for demonstration
figure;
aa = surf(x, y, z); % Create a surface plot and store the handle in 'aa'
for ii = 0:10:360
for jj = 0:10:90
view(ii, jj);
% Create a multiple folder
newdir = sprintf('view%02dview%02d', ii, jj);
mkdir(newdir);
% Initialize array to store red areas
redArea = zeros(1, 36);
% Rotate masks and save figures
for i = 1:36
rotate(aa, [0, 0, 1], 1);
% Print figure with rotation
filename = sprintf('Slice_%d.png', i);
filepath = fullfile(newdir, filename);
print('-dpng', filepath);
pause(2);
end
% Calculate red pixels and save in Excel file
for i = 1:36
filename = sprintf('Slice_%d.png', i);
filepath = fullfile(newdir, filename);
rgbImage = imread(filepath);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Calculate red pixels
redPixels = redChannel == 255 & greenChannel == 0 & blueChannel == 0;
redArea(i) = sum(redPixels(:));
end
% Save redArea results to an Excel file
excelFilename = fullfile(newdir, 'test.xlsx');
xlswrite(excelFilename, redArea');
end
end
Hope that helps!

类别

Help CenterFile Exchange 中查找有关 Application Deployment 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by