imread figures from new folder created

3 views (last 30 days)
viet le
viet le on 1 Sep 2016
Answered: Vidhi Agarwal on 23 Aug 2024
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);

Answers (1)

Vidhi Agarwal
Vidhi Agarwal on 23 Aug 2024
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!

Categories

Find more on Application Deployment in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!