Block average images with MATLAB's blkproc or blockproc function

I recently had to perform a block average on an image to reduce its size for processing in MATLAB and learned about a useful function called blkproc or blockproc (in the newer versions) in the Image Processing Toolbox. Block averaging is a process by which you average non-overlapping blocks of an image, which becomes a single pixel in the block averaged image. The standard image resize functions use a filter to resize the image and does not allow the blocks to be non-overlapping.

MATLAB's blkproc or blockproc functions facilitate this by making it easy for you to specify a function to be applied to blocks in the image. If you have an image img and want to do a M x N block average (i.e. the pixels of the resultant image are the average of M x N blocks in the original), you can use:

img = blkproc(img, [M N], 'mean2');

In newer versions of MATLAB, blockproc is preferred and is used this way:

fun = @(block_struct) mean2(;
img = blockproc(img, [M N], 'fun');

mean2 is an Image Processing Toolbox function that takes a 2D array and returns the mean of all values. The distinction between blkproc usage and blockproc usage is that with blockproc, the function that it expects in place of mean2 takes a block struct structure instead of an array. Hence, we need to define a new inline function fun which provides the right interface by wrapping the mean2 function.

What blkproc / blockproc does is divides the image up into M x N blocks and feeds each one to mean2 and then takes the result and puts it into a single pixel in the new image.

You can replace mean2 with other functions that take an M x N array and returns a M' x N' array (where M' and N' are arbitrary numbers; for mean2 this is always 1 x 1) and the result will be constructed by tiling the M' x N' arrays in the order that the M x N blocks occur in the original image. The figure below shows what happens:

Diagram showing MATLAB block processing

The original image is 20 x 20 pixels. Setting M = 4 and N = 4 and using mean2 (which outputs M' = 1 and N' = 1), the final image is 5 x 5 pixels.

When the image cannot be divided up into an integer number of M x N blocks, you will get border effects as the blkproc function pads images out by zeros. The newer version blockproc allows you to specify how you want to treat the partial blocks, either process them as is or pad them with zeros, with the 'PadPartialBlocks' parameter. See the MATLAB documentation for details.

Order of processing

The MATLAB documentation does not indicate the actual order of processing of the blocks (i.e. the actual sequence by which the blocks are processed). If the blkproc or blockproc implementation is parallel or multithreaded, there will not be any guaranteed order at all.

I believe that the blkproc function is single threaded on a single core computer, so the order of processing should be simple. To find out, I used the following test function, which does nothing except print the value of each image block pased into it. When I use this function with blkproc, I will use a block size of 1 x 1 so it will only print one value each time the function is executed.

function a = testblkproc(imblock)
disp(sprintf('%g', imblock))
a = imblock;

I then constructed a matrix of values that I can easily interpret:

>> a = [1 4 7; 2 5 8; 3 6 9]
a =
     1     4     7
     2     5     8
     3     6     9

Finally, I ran blkproc with my test function testblkproc using a block size of 1 x 1. This makes it output the order in which the 1 x 1 blocks are procesed:

>> blkproc(a,[1 1],'testblkproc');

According to this test, blkproc processes entire rows first before moving onto the next row. I should re-iterate that this is only applicable to the blkproc function running on a single core computer.


fatin, 2010/09/25 14:05
I would like to partition an image to 16 parts, then working with each part as image...I mean, I will get 16 images instead of one image.

how can I partition the image to 16 region?
how can I save each region as image?... I am using Matlab

Peter Yu, 2010/09/27 01:36
I haven't tried this but maybe write your own custom block function (function fun) so that it will save the blocks out as images files. It should work since the block functions are just normal functions.
Ashraf Suyyagh, 2011/05/01 04:56
What is the order of block processing? Vertically, horizontally or Random?
Peter Yu, 2011/05/01 14:33
The order is not given in the MATLAB docs. You will have to code a test function to see what happens. I would expect it depends on whether the blockproc implementation is multithreaded in your version of MATLAB, how many cores you have, etc. I updated the page with one such function that you can use and tested it with a single threaded blkproc.
MUHAMMAD USMAN, 2019/02/25 06:16
sir ,
I have a image and I want to divide into multiple blocks like if the image size is 64*64 pixels the block size is 2*2and each block contains 32*32 pixels and if the 4*4 block size the block contains 16*16 pixels the total block size is 816 blocks. please suggest me the best way how can i do it? thanks
soumya, 2011/11/12 01:56
sir ,
I have a 256x256 image and I want to divide into blocks each having 8x8 pixels.So I will get total 1024 blocks.Now I want to process each blocks manually is completlly possible with blkproc.Can u help me ,I visited many sites but none of them are responding
kiran, 2012/02/28 03:01
[r c]=size(I);
bs=8; % Block Size (8x8)

nob=(r/bs)*(c/bs); % Total number of 8x8 Blocks

% Dividing the image into 8x8 Blocks
for i=1:(r/bs)
for j=1:(c/bs)

% Accessing individual Blocks

figure;imshow(Block(:,:,1)) % This shows u the fist 8x8 Block in a figure window
figure;imshow(Block(:,:,2)) % This shows u the second 8x8 Block (i.e as per my %coding rows from 1:8 and col from 9:16) in a figure window and so on.....
Samta , 2013/01/07 07:44
thanks for this code
MUHAMMAD USMAN, 2019/02/25 06:19
sir ,
I have a image and I want to divide into multiple blocks like if the image size is 64*64 pixels the block size is 2*2and each block contains 32*32 pixels and if the 4*4 block size the block contains 16*16 pixels the total block size is 816 blocks. please suggest me the best way how can i do it? thanks
Immanuel, 2014/10/21 02:52
How do we implement this using blockproc? Can we implement user defined functions for blockproc?
danish, 2012/05/17 10:20
is this array [M, N] in blockproc function can be a user input? means can you user enter it run time that which block user want to extract???
Peter Yu, 2012/05/21 21:51
Probably not, if I understand you correctly. The point of blockproc is to process the entire image as separate blocks, not necessarily to let you pick out a block to process.
jeymagnettron, 2012/09/27 14:20
hello ,
i just defined an function for processing the image and this function has 2 inputs like [y]=func_1(x,y); where x is the input image n y is some other data for my function. now how can i interface this function with blkproc??
Peter Yu, 2012/09/27 21:28
Take a look at the blockproc documentation:

fun = @(block_struct) imresize(,0.15);
I2 = blockproc(I,[100 100],fun);

You can supply parameters when you define the 'fun' function.

For blkproc, I think that you can supply the additional parameters after the function name:

img = blkproc(img, [M N], 'myfunction', p1, p2, ...);

Try it. I don't have MATLAB installed right now so I can't say if it will work as I expect.
rajeshri, 2012/12/10 01:07
I have image of 256x256 .which i want to divide it into 16x16 block .after that I want to divide 16x16 block into 4x4 block size . so that i can uniqly represent each 4x4 block with respect to whole image
srujana, 2013/01/23 19:58

I am doing project on image processing in this i have problem is take image and partioned in to several parts every part will be displayed seperately .i.e, my question .i hope on you to get perfect form you.
mohammad, 2013/03/02 07:46
hello to all
I want to get average from Corresponding pixel of my array images
please help me
Peter Yu, 2013/03/04 14:34
The ''mean'' function lets you specify the dimension along which you do the averaging. So you can concatenate (''cat'') your images and then use ''mean'' with the specified dimension.
Malu, 2013/03/09 14:12
Hello All,

I'm doing project in image retrieval. I have partitioned 256 x384 image into 6 non overlapping blocks and have found the mean of each block. Now i've to compare each block of query image with every other block of target image in database. I know comparing this way is time consuming when the size of database is too big. Could you kindly suggest me some better option for searching query image against the images in database. And how do i go about further in retrieval?
Peter Yu, 2013/10/30 16:35
This is still an open area of research. Try searching the literature.
tarun, 2013/04/17 11:27
I have a similar problem

I have an image which is of size 256 X 256. Random noise is distributed all over the image. This image has 16 '64 X 64' size similar images(or tiles). What I want to do is
1. Divide the image into 16 '16 X 16' size blocks.
2. Take average of all the blocks.
3. Place the resulting image in the position of the image in the top-left
corner. Leave the remaining 15 images undisturbed for comparison purposes.

Like 'Average filtering'. Can anyone please help?
foufou, 2013/10/30 10:23

I have an image of size 256 * 256, I want to go in block 4 * 4, then take the average of each block.
Can u help me ,I visited many sites but none of them are responding...
I am using Matlab

fama, 2013/12/19 08:52
i want to apply dct on image on following code....... but it giving error.... please help me...
I = imread('sea.jpg');
dct=@(block_struct) T .* .* T';
I3 = blockproc(I2,[8 8],dct,'PadPartialBlocks',true);


the error is
Function BLOCKPROC encountered an error while evaluating the user supplied
function handle, FUN.

The cause of the error was:

Error using .*
Matrix dimensions must agree.

Error in @(block_struct)T.**T'

Error in blockprocFunDispatcher (line 14)
output_block = fun(block_struct);

Error in blockprocInMemory (line 81)
[ul_output fun_nargout] = blockprocFunDispatcher(fun,block_struct,...

Error in blockproc (line 237)
result_image = blockprocInMemory(source,fun,options);
Nagaveni, 2014/01/31 04:42
I have an image of size 256 * 256, I want to go in block 2 * 2, then compare each block with another image of 2 * 2 and find out least mean square error.
Can u help me sir pls
I am using Matlab
rangu rathod, 2014/02/17 00:33
i have 256*256 contourlet coefficients,how to divide those coefficients into block by block form
rangu rathod, 2014/02/17 00:35
how to embedd the information bits into coefficients,do u have any algorithem
rangu rathod, 2014/02/18 05:21
Hi sir
how to choose center block value from 4*4 blockand how to add bits into center block
Raj, 2014/02/28 06:35
i have divide image into 8*8 bolcks using following program...I want to apply DCT on each block...What is the code for that

clear all;

[a b] = size(A); % get the size of A =128*128
c=8;d=8; % reshape it into 8*8

for i=1:c:a-7
for j=1:d:b-7
eval(['out_' num2str(l) '=C'])
võ nam, 2014/03/28 04:51
Hi! If I have 8x8 block,then I want convert it to the original matrix. What should I do?
jain, 2014/06/24 05:48
I = imread('circle.jpg');
fun = @(block_struct) mean2(;
img = blockproc(I, [8,8],'fun');

I'm trying to divide an image into 8x8 blocks, but I get an error at "img = blockproc(I, [8,8],'fun');" each time I run the code. Altered the code for so many times, but of no avail.
PRIYANKA, 2014/08/01 14:42
I want to know how blocks execute in block processing. parallel or sequentially. whether they use multi-threading if useparallel is turn off.
S.JAYASRI, 2014/08/14 06:52
I'm having 16x16 image and i want to divide it into 2x2 overlapping patches and then how to reconstruct the original image from patches
hiba, 2014/08/17 17:59
i want to divide an image into N x N blocks then apply an equation to the position of the block.for simple example:
let image 256 X 256 divide it into 2 X 2 blocks that meant(block(1,1),block(1,2),block(2,1)and block(2,2))
How can i do it?
B.Krithika, 2014/09/08 10:18
I am doing my project in matlab. i am using 8x8 matrices.What is the meaning for for(i=1:8:r-7) in dct
Sinan, 2014/12/14 04:30
Hi all

i have an image size(M*N) i want to divided it into blocks size(W*W), W=3,4,.....20.

according to this equations

w=(3,4....9) no overlapping

w=(10,11....15) 50% overlapping

w=(16,17....20) 75% overlapping

Thanks in advance
shikha, 2015/01/20 04:13

I want to devide image into 32*32 image blocks and then find the noise variance of each block. for which I am using PCA noise estimator. How to use PCA function to find variance.

Reply me as soon as possible.
roshni, 2015/01/23 03:35

I want to divide an image into 5*5 image blocks and i want to extract feature from each block using histogram.. so how use histogram function to each region.

Please reply
Bharat, 2015/02/09 22:27
I am working on image processing.How I get Overlapping blocks of size b*b are created with one pixel shifting and the number of overlapping blocks for each layer color image(R,G,B)
Ebenezer, 2015/03/09 14:00
after using this code : bansize = blkproc(bandilate,[4 4],'mean2');

I want to reshape to have a matrix 1024 x 200

this is giving me problem can anybody assist me with this
pooja, 2016/02/19 12:35
I have a specific pixel location in a given image. I have to identify 5*5 block around that pixel and then want to calculate average of that block. how to get that 5*5 block?
ARNAB CHATTARAJ, 2016/03/14 12:06
I have a matrix of 1026x1026 order. now I extract each 3x3 blocks or

kernels of above matrix and determine the mean. now in extracting the 3x3

blocks it works at first horizontally then verticaly that mean at first A1

A2.....A342 then it goes to the next vertically shift and again extract

A343 A344 A345.... in this way. Now I want to concatenate these small 3x3

blocks with there only mean value of each block to get back the previous

matrix of 1026x1026 order.(HERE IT IS TO BE TAKEN THAT EACH BLOCK OF 3X3



So is there any way to concatenate those small blocks horizontally and

then vertically simultaneously to get the main big matrix of 1026x1026


Please Reply if anyone know the process for this type of big matrix....
Santh, 2016/03/16 06:17
I need to read in 40 different size images{47x80,50x78..}and using imtool(manually selecting an area in each of the 40 images) in MATLAB and calculate their average and reconstruct the average image which I can use as a template?

Can anyone help with code?
vivek, 2016/04/23 15:47

How to subdivide a 256*256 gray scale image into 8*8 blocks and find the covariance of each image block?

can any one help with code?
JEBA CHRISTILDA, 2016/10/25 05:31
hello sir,
I am doing my project in matlab image processing. my problem is, i have divided a image of 1024*764 into 4*6 and now i want to compare each and every block with my reference block(which is also a block in that same image).
Raji, 2017/02/21 05:12
i want how to find overlapping area with two images???? help me
sajula, 2017/03/09 13:16
How to subdivide a 256*256 gray scale image into 8*8 blocks and find the covariance of each image block?

can any one help with code?
Sumi, 2017/05/08 10:44
I want to divide a 512 x512 image and a 16x16 image concurrently when bit level division of small image reached then add it to the same block of big image. Help me by giving the code.
Anu, 2018/01/25 08:46
I have an image and divided into 2 * 2 grid. Now I want to get centralized sub-image of the same image so that I can find out the features of the centralized sub-image. How to do it?
GOKUL K, 2018/04/11 02:46
I have an image of size 900*1200. I have to subdivide the image into 64*8=512 non overlapping blocks. How can I do that?
MUHAMMAD USMAN, 2019/02/27 14:53
sir ,
I have a image and I want to divide into multiple blocks like if the image size is 64*64 pixels the block size is 2*2and each block contains 32*32 pixels and if the 4*4 block size the block contains 16*16 pixels the total block size is 816 blocks. please suggest me the best way how can i do it? thanks
I would love to hear your feedback. Enter your comment below [ Terms of Use ]:

About Peter Yu I am a research and development professional with expertise in the areas of image processing, remote sensing and computer vision. I received BASc and MASc degrees in Systems Design Engineering at the University of Waterloo. My working experience covers industries ranging from district energy to medical imaging to cinematic visual effects. I like to dabble in 3D artwork, I enjoy cycling recreationally and I am interested in sustainable technology. More about me...

Feel free to contact me with any questions about this site at [user]@[host] where [user]=web and [host]

Copyright © 1997 - 2019 Peter Yu