Saving an image in a for loop with an index
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi there,
I am very new to coding so please excuse any ignorance - i am learing. My objective is to get an image, split it into a grid, and then save each grid into a folder. I have been successful for most of it except saving each grid into a folder. I can only save the first grid that i have produced. I need to be able to save every block from my grid into the folder and assign it a unique index. Can anyone help please? Here is my code:
thank you so much
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
%%caption = sprintf('B#%d', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
%%saving to new directory for processing in CNN
imwrite (rgbBlock, 'Z:\Aerial Images\Images\Output1.jpg', 'jpg');
plotIndex = plotIndex + 1;
end
end
0 Kommentare
Antworten (2)
Walter Roberson
am 27 Dez. 2017
projectdir = 'Z:\Aerial Images\Images';
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
%%caption = sprintf('B#%d', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
%%saving to new directory for processing in CNN
filename = fullfile(projectdir, 'Output%03d_%03d.jpg', r, c);;
imwrite(rgbBlock, filename);
plotIndex = plotIndex + 1;
end
end
5 Kommentare
Image Analyst
am 10 Jan. 2018
rgbBlock is just a single image that gets overwritten every single iteration and so after the two loops, it will be the last image. So you can't have another loop outside the first pair since there's no use - A will be just one single image, not a cell array of images. I recommend that, if you want to save each image block, to put the imwrite() inside your loop over c.
Siehe auch
Kategorien
Mehr zu Blue finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

