MATLAB: Convert image into Patches of size 64*64 and get each patch

Image Processing Toolboxpatchesrgb

Hello, I have RGB image dataset.I want to convert an image into patches and save each patch.How to do this in matlab. The patch size should be 64*64

Best Answer

Hi Tahir, try this:
I = imread('rice.png');
imSz = size(I);
patchSz = [64 64];
xIdxs = [1:patchSz(2):imSz(2) imSz(2)+1];
yIdxs = [1:patchSz(1):imSz(1) imSz(1)+1];
patches = cell(length(yIdxs)-1,length(xIdxs)-1);
for i = 1:length(yIdxs)-1
Isub = I(yIdxs(i):yIdxs(i+1)-1,:);
for j = 1:length(xIdxs)-1
patches{i,j} = Isub(:,xIdxs(j):xIdxs(j+1)-1);
end
end
This produces a cell of your 64x64 patches:
>> patches
patches =
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
And you can access each one:
figure, imagesc(patches{2,3})
Did that do what you wanted?
Thanks, Sven.