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

37 visualizaciones (últimos 30 días)
Tahir el 14 de Mayo de 2015
Editada: Neha Rathore el 28 de Abr. de 2022
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
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Walter Roberson el 5 de Abr. de 2021
Image Analyst el 6 de Abr. de 2021
@Rajesh Das, and should be down below in the official "Answer" section with all the other answers of this question.

Iniciar sesión para comentar.

Sven el 14 de Mayo de 2015
Editada: Sven el 14 de Mayo de 2015
Hi Tahir, try this:
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.
##### 6 comentariosMostrar 4 comentarios más antiguosOcultar 4 comentarios más antiguos
Walter Roberson el 9 de Abr. de 2021
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
Neha Rathore el 28 de Abr. de 2022
Editada: Neha Rathore el 28 de Abr. de 2022
Hi! The code worked well. How can i recombine these patches to form an original image.

Iniciar sesión para comentar.

### Más respuestas (2)

Image Analyst el 14 de Mayo de 2015
This is answered by the FAQ in two different ways (mat2cell and indexing). Take your pick as to which is easier, more convenient, and intuitive for you.
##### 11 comentariosMostrar 9 comentarios más antiguosOcultar 9 comentarios más antiguos
Walter Roberson el 8 de Sept. de 2018
glcm_output = blockproc(YourRGBMatrix, [blockSizeR, blockSizeC], @(block_struct) graycomatrix(block_struct.data(:,:,1), 'Offset', [0 1;-1 1;-1 0;-1 -1], 'NumLevel', 8, 'Symmetric',true), 'TrimBorder', false);
glcm_output will be a 2D matrix that is all of the glcm results concatenated together. So if the output of glcm is 8 x 8, and there are (say) 39 groups of 100 pixels vertically, and 48 groups of 100 pixels horizontally (say the images was 384 x 480), the output would be (39*8) x (48*8) which would be 312 x 384.
Only the first color plane will be examined in this code, because glcm is only for grayscale not for color. Instead of block_struct.data(:,:,1) you could use rgb2gray(block_struct.data) to convert to grayscale... but it would be more efficient to instead
glcm_output = blockproc(rgb2gray(YourRGBMatrix), [blockSizeR, blockSizeC], @(block_struct) graycomatrix(block_struct.data, 'Offset', [0 1;-1 1;-1 0;-1 -1], 'NumLevel', 8, 'Symmetric',true), 'TrimBorder', false);
With a slight bit of extra work you could use a real function instead of an anonymous function, and have that real function call GLCM_Features1 on the output of the graycomatrix, and pick out the required stats and return them.
Image Analyst el 8 de Sept. de 2018
I do have a version of blockproc() that uses a separate function, where you can do more complicated operations than in an anonymous function. It is attached.
There is also a function called graycoprops() that you may want instead of graycomatrix(). It gives you key features from the GLCM matrix.

Iniciar sesión para comentar.

Ahmad el 30 de Mzo. de 2017
hello. its work well on 256 by 256 image but i have 512 by 512 image. it gives the following error. Index exceeds matrix dimensions.
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Walter Roberson el 30 de Mzo. de 2017
See the FAQ that Image Analyst posted.

Iniciar sesión para comentar.

### Categorías

Más información sobre Texture Analysis en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by