I have an RGB image of size 412x550
. I want to divide it into sub matrices of size 2x2
. I have tried using mat2cell
function but it is
In general, you can use the following code to divide the image into blocks (for compression process or whatever)
A=imread('image.bmp'); % i assume 8-bit gray scale image
[m,n,k]=size(A); % and m=n with 1 channel k=1
ImageSize=m*n;
BlockD=2; % i assume 2x2 block
BlockSize=BlockD*BlockD;
NoOfBlock=ImageSize/BlockSize;
SubB=zeros(BlockD,BlockD,NoOfBlock); %arrays of blocks.
B=double(A); important to convert uint8 to double when dialing with image.
% thats what ru asking for.
k=1;
for i=1:BlockD:m
for j=1:BlockD:n
SubB(:,:,k)=B(i:i+BlockD-1,j:j+BlockD-1); k=k+1;
end
end
%compare between first submatrix A with first block.. its the same elements.
B(1:2,1:2)
SubB(:,:,1)
You probably got the syntax a bit off. The correct syntax for your situation would be
>> A = rand(412,550);
>> B = mat2cell(A, 2*ones(size(A,1)/2,1), 2*ones(size(A,2)/2,1))
ans =
[2x2 double] [2x2 double] ...
[2x2 double] [2x2 double] ...
...
You should use the function im2col
. It is exactly what you need.
A = rand(412,550);
B = im2col(A,[2 2]);
The doc is there:
http://www.mathworks.fr/help/images/ref/im2col.html;jsessionid=4d24325613716d84d4635b4fc636