Cutting up image into smaller blocks - how to name resulting blocks according to grid pattern?

1 view (last 30 days)
Hello All,
Total Matlab noob here. Thanks to these forums, I've managed to split a larger image into a grid of much smaller images. I've also managed to save each of these smaller images as a .png. However, I wish to save these images with file names relevant to their location in the grid. (The upper left block would be A_001, the block one to its right would be B_001, the one below that would be B_002, etc.) However, I am unsure how to do this. I'll show the relevant code below.
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);
drawnow;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
end
% Display the original image in the upper left.
subplot(4, 6, 1);
imshow(rgbImage);
title('Original Image');
for K = 1 : numel(ca)
filename = sprintf('%d.png', K);
imwrite(ca{K}, filename);
end
The first, much larger part of the code shows how we define %d. For the very first block, %d is 1, for the second, %d is 2, etc. The last few lines at the very end show how we name each individual photo file after %d. However, this is as far as I've gotten. I have tried altering the bottom lines, but everything I've tried has resulted in an error. Is there a way to name images in the grid fashion by using %d? Do I need a whole nother system all together?
Thank you!

Accepted Answer

Image Analyst
Image Analyst on 8 Jun 2021
Edited: Image Analyst on 8 Jun 2021
David:
Try this:
numPlotsR = 3;
numPlotsC = 37; % or size(ca, 2);
folder = pwd; % Or wherever you want.
for r = 1 : numPlotsR
for c = 1 : numPlotsC
col = ExcelCol(c);
baseFileName = sprintf('%s_%03d.png', col{1}, r);
fullFileName = fullfile(folder, baseFileName);
fprintf('Writing %s...\n', fullFileName);
% imwrite(ca{r,c}, fullFileName);
end
end
Looks like
...
Writing C:\Users\David\Matlab\work\Tests\AI_003.png...
Writing C:\Users\David\work\Tests\AJ_003.png...
Writing C:\Users\David\work\Tests\AK_003.png...
and so on.
ExcelCol.m is attached and converts numbers to the Excel column letter codes like you asked for.
  6 Comments

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 8 Jun 2021
David - ca seems to represent your cell array of blocks. The code you have (already) to write the blocks is
for K = 1 : numel(ca)
filename = sprintf('%d.png', K);
imwrite(ca{K}, filename);
end
can be modified to do (what I think you want) as
for r = 1 : numPlotsR
for c = 1 : numPlotsC
filename = sprintf('%c_%03d.png', char(c + 64), r)
imwrite(ca{r,c}, filename);
end
end
In the above, we convert the integer columns (offset by 64) 65, 66, 67, ... to their ASCII equivalent of 'A', 'B', 'C", ...
  4 Comments
David Bukowski
David Bukowski on 9 Jun 2021
Thank you Geoff. If I wanted to make it AA, would the line of code just be:
filename = sprintf('%c_%03d.png', char(c + 64)char(c + 64), r) ?

Sign in to comment.

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!