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

조회 수: 5(최근 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일
편집: 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