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

4 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Image Analyst
Image Analyst il 8 Giu 2021
Modificato: Image Analyst il 8 Giu 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 Commenti
David Bukowski
David Bukowski il 10 Giu 2021
Hi there,
I am not seeing files show up anywhere. Should they appear in the Current Folder window on the left side? I believe the issue might actually be with the ExcelCol file? I'm getting an error message about 'In' when I run it, I'll show you the screenshot here.

Accedi per commentare.

Più risposte (1)

Geoff Hayes
Geoff Hayes il 8 Giu 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 Commenti
Geoff Hayes
Geoff Hayes il 9 Giu 2021
David - please see the ASCII tables from https://en.wikipedia.org/wiki/ASCII. Note that the integer 65 is mapped to the character 'A', 66 to 'B', etc. and so that is why we offset by 64. You could start with AA if you wish. The code would need to be updated to account for that.
David Bukowski
David Bukowski il 9 Giu 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) ?

Accedi per commentare.

Categorie

Scopri di più su Convert Image Type in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by