Saving an image in a for loop with an index

5 visualizaciones (últimos 30 días)
Zachary Gancarz
Zachary Gancarz el 27 de Dic. de 2017
Comentada: Zachary Gancarz el 11 de En. de 2018
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

Respuestas (2)

Walter Roberson
Walter Roberson el 27 de Dic. de 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 comentarios
Image Analyst
Image Analyst el 10 de En. de 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.
Zachary Gancarz
Zachary Gancarz el 11 de En. de 2018
thanks brother

Iniciar sesión para comentar.


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

Categorías

Más información sobre Blue en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by