MATLAB: How to divide a image of 1500*1500 into different blocks of 32*32 and save all the images in a folder with different subfolders in it.

image analysis

I have a folder in which i have sub folders of different image groups. Each image is 1500*1500, How can I divide it into different blocks of 32*32 and save all the images in that folder with different subfolders in it.

Best Answer

%divide into blocks
temp = cell2mat(YourImage, [32 * ones(1, 46), 28], [32 * ones(1, 46), 28]);
separate_blocks = temp(1:end-1, 1:end-1);
%create output folder, making sure we do not overwrite any existing folder
while true
foldername = tempdir('.');
if ~exist(foldername, 'dir'); break; end
end
mkdir(foldername);
%write the images
for K = 1 : numel(separate_blocks)
filename = fullfile(foldername, sprintf('%04d.bmp', K));
imwrite(filename, separate_blocks{K});
end