Dividing image into blocks and calculating mean values

1 view (last 30 days)
i have 256*256 image. i've converted into 4*4 blocks and also converted into 16*1 linear array. now i need code to calculate mean value of 1 to 8 rows, then 9 to 16 rows,... like this.. please send suitable code. i am using the code given below.
I=imread('cameraman.tif');
I=double(I);
Im=size(I);
%find the size of the image
[nr nc nd]=size(I);
% Divide into blocks
disp(nr);
disp(nc);
% Divide into blocks
m=64;
n=64;
row=1;
column=1;
i=1;
j=1;
nn=0;
for i=1:4:nr-3
for j=1:4:nc-3
block=I(i:i+3,j:j+3);
disp(block)
%convert 4X4 into 16X1 column vector
tv=reshape(block,16,1);
disp(tv)
column=column+4;
end
row=row+4;
B(:,nn+1) = mean(tv(:,nn*8+1:(nn+1)*8),2);
end
disp(i)
disp(j)
I=imread('cameraman.tif');
I=double(I);
Im=size(I);
%find the size of the image
[nr nc nd]=size(I);
% Divide into blocks
disp(nr);
disp(nc);
% Divide into blocks
m=64;
n=64;
row=1;
column=1;
i=1;
j=1;
nn=0;
for i=1:4:nr-3
for j=1:4:nc-3
block=I(i:i+3,j:j+3);
disp(block)
%convert 4X4 into 16X1 column vector
tv=reshape(block,16,1);
disp(tv)
column=column+4;
end
row=row+4;
B(:,nn+1) = mean(tv(:,nn*8+1:(nn+1)*8),2);
end
disp(i)
disp(j)

Answers (2)

Matt J
Matt J on 9 Feb 2016
Using SEPBLOCKFUN on the File Exchange, you can do all of this in one line
B=sepblockfun(I,[4,2],'mean');
  2 Comments
divya pandiaraj
divya pandiaraj on 10 Feb 2016
Edited: divya pandiaraj on 10 Feb 2016
Thank u sir for ur reply. but while i am using your code i have an error like, ??? Undefined function or method 'sepblockfun' for input arguments of type 'uint8'.
Error in ==> rev6 at 4 B=sepblockfun(I,[4,2],'mean');
please reply sir.
Matt J
Matt J on 10 Feb 2016
Edited: Matt J on 10 Feb 2016
I suspect that you did not actually download SEPBLOCKFUN from the link I gave you. Also, you should convert I from uint8 to double before using sepblockfun.

Sign in to comment.


Image Analyst
Image Analyst on 10 Feb 2016
The first 8 rows of a 4x4 block you converted into a 16*1 array is essentially the left half of the 4x4 block. In other words, a 4x2 block. This mean can be done with blockproc(). See my attached demos for several ways to use blockproc. One of them takes the means in blocks and you just need to change the block size in the demo to 4 rows and 2 columns. The whole thing should just be about 2 to 4 lines of code.

Tags

Community Treasure Hunt

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

Start Hunting!