Saving an image in a for loop with an index

조회 수: 5 (최근 30일)
Zachary Gancarz
Zachary Gancarz 2017년 12월 27일
댓글: Zachary Gancarz 2018년 1월 11일
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월 11일
thanks brother

댓글을 달려면 로그인하십시오.


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