Saving an image in a for loop with an index

5 次查看(过去 30 天)
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

回答(2 个)

Walter Roberson
Walter Roberson 2017-12-27
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 个评论
Image Analyst
Image Analyst 2018-1-10
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.

请先登录,再进行评论。


Zachary Gancarz
Zachary Gancarz 2018-1-9
Anyone have an idea? I'm getting blank 1x1 pixels. Can't seem to figure it out
cheers

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by