Cutting up image into smaller blocks - how to name resulting blocks according to grid pattern?
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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 个评论
更多回答(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 个评论
Geoff Hayes
2021-6-9
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!