How to break an image into blocks?

I have an image with (643x643) dimensions , and i need to split it horizontally and vertically into 100 sub images,so that each sub image is (64x64)pixels using matlab code .The sub images are blocks with (64x64) pixels ,so the height and the width of each block is equal 64

7 Comments

You mean like blockproc?
No, I mean by dividing the image into separated square blocks or as grid
If you want to divide matrices into cell arrays you can use cell2mat. If you want to do something with those subsections, it might actually be usefull to use blockproc, since that will handle the loops internally for you.
Hadeel
Hadeel on 21 Aug 2022
Moved: Rik on 21 Aug 2022
please.how can i display all the blocks ?
@Hadeel Did you look at the answer before posting your comment as an answer? How did it not work for you?
yes.sure and the code work with me but i need to disply the blocks in the screen??
Do you mean you want to open the image file with your system viewer? Or do you want to show the images in a Matlab figure?

Sign in to comment.

Answers (1)

Fabio Freschi
Fabio Freschi on 10 Oct 2019
Edited: Fabio Freschi on 10 Oct 2019
Three assumprions
1) the image is a 2d matrix
2) the row/cols have 64x3, 3x64 and 3x3 blocks
3) you want 10 sub images, not 100
% A is your image
B = mat2cell(A,[64*ones(1,10) 3],[64*ones(1,10) 3]);
B is a 11x11 cell array with your sub images. You can access each of them with B{i,j}.
If the image is a 3d matrix (643x643x3), just add the third dimension to mat2cell:
B = mat2cell(A,[64*ones(1,10) 3],[64*ones(1,10) 3],3);

8 Comments

The original image is 640x640 pixels, and i want to divide it into 10 blocks in each row ,and each block has 64x64 pixels .So in total I will have 10 rows and each consists of 10 blocks .The total number of pieces is 100 blocks.
So use the method I described using mat2cell Removing the last chunk with 3 pixels
% A is your image
B = mat2cell(A,64*ones(1,10),64*ones(1,10));
Sorry, the original image is 634x643 ,and i tried to apply the above code , but i got an error : Error in first (line 2)
B = mat2cell(currentimage,64*ones(1,10),64*ones(1,10));
Error using mat2cell (line 89)
Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [643 643].
Do you understand the error message? It tells you the inputs must sum to the dimensions. The code in the answer does that for 643x643 inputs. The two codes are not interchangeable.
No, i do not understand the error message, i need to create an empty matrix with 10x10 ,then i store the croped image in this matrix like this code :
currentimage = imread('C:\Users\user\Desktop\D.gif');
[row,col,~]=size( currentimage );
height=fix(row/10);
width=fix(col/10);
for i=0:9
for j=0:9
B{i,j}=imcrop(currentimage,[(j*height) (i*width) height width]);
end
end
first, I need to create an empty array containing 100 cells and then store the 100 croped images in it.More clearly B{0,0} is the first croped image with 64x64, and B{0,1} is the second croped image with 64x64 ,and so on.
if the matrix is 634x643 you can make what are the dimensions of the block you want?
You can make:
  • rows: 9 blocks of dimension 64 along and one of dimension 58
  • cols: 10 blocks of dimensions 64 and one of dimension 3
  • In this case
% dummy matrix
A = rand(634,643);
% partition
B = mat2cell(A,[64*ones(1,9) 58],[64*ones(1,10) 3]);
This code doesn't give error messages. B is a 10x11 cell array. You can access your blocks using B{i,j}. If you have different dimensions, look at mat2cell documentation.
A more general approach: for any dimension of the original image, if you want block with dimension 64 you can use this code
% dummy matrix
A = rand(634,643);
% block dimension
dimBlock = 64;
% number of full row/col blocks
nBlocks = floor(size(A)/dimBlock);
% dimension of the leftover
nRest = size(A)-dimBlock*nBlocks
B = mat2cell(A,[dimBlock*ones(1,nBlocks(1)) nRest(1)],[dimBlock*ones(1,nBlocks(2)) nRest(2)]);
If you dont want the smaller blocks that does not fit the desired 64x64 dimension
% dummy matrix
A = rand(634,643);
% block dimension
dimBlock = 64;
% number of full row/col blocks
nBlocks = floor(size(A)/dimBlock);
B = mat2cell(A(1:nBlocks(1)*dimBlock,1:nBlocks(2)*dimBlock),dimBlock*ones(1,nBlocks(1)),dimBlock*ones(1,nBlocks(2)));
You need to tell Matlab how to split up your array. If I tell you to divide 10 objects in 3 groups of 3, you will ask me what to do with object 10. The code below will show you in multiple steps how to create the grouping for each dimension. Make sure to understand what each line of code does, and look at the contents of D{1} and D{2}.
A = rand(634,643);
D=cell(1,2);
for dim=1:2
D{dim}=64*ones(1,floor(size(A,dim)/64));
if sum(D{dim})~=size(A,dim)
%extend with remainder
D{dim}(end+1)=size(A,dim)-sum(D{dim});
end
end
B=mat2cell(A,D{1},D{2});
No one has actually directly asked what you want to do with the blocks. So I will, because it probably matters. What do you plan on doing with the blocks once you have them?
And again, like the others keep stressing to you, you will not have blocks all the same size if you have an image 643 pixels wide and use blocks 64 pixels wide. What are your plans for the different sized block?

Sign in to comment.

Products

Release

R2018a

Asked:

on 9 Oct 2019

Commented:

Rik
on 21 Aug 2022

Community Treasure Hunt

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

Start Hunting!