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

3 ビュー (過去 30 日間)
David Bukowski 2021 年 6 月 7 日
コメント済み: Image Analyst 2021 年 6 月 10 日
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!

サインインしてコメントする。

### 採用された回答

Image Analyst 2021 年 6 月 8 日

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 件のコメント表示非表示 5 件の古いコメント
Image Analyst 2021 年 6 月 10 日

サインインしてコメントする。

### その他の回答 (1 件)

Geoff Hayes 2021 年 6 月 8 日
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 件のコメント表示非表示 3 件の古いコメント
David Bukowski 2021 年 6 月 9 日
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) ?

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by