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(block_struct.data); 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:
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.
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'); 1 4 7 2 5 8 3 6 9
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.
Discussion
how can I partition the image to 16 region?
how can I save each region as image?... I am using Matlab
regards
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 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
[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
kk=0;
for i=1:(r/bs)
for j=1:(c/bs)
Block(:,:,kk+j)=I((bs*(i-1)+1:bs*(i-1)+bs),(bs*(j-1)+1:bs*(j-1)+bs));
end
kk=kk+(r/bs);
end
% 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.....
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 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??
fun = @(block_struct) imresize(block_struct.data,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.
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
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.
I want to get average from Corresponding pixel of my array images
please help me
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?
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?
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
regards
I = imread('sea.jpg');
I2=im2double(I)
T=dctmtx(8)
dct=@(block_struct) T .* block_struct.data .* T';
I3 = blockproc(I2,[8 8],dct,'PadPartialBlocks',true);
figure;
imshow(I3);
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.*block_struct.data.*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);
Can u help me sir pls
I am using Matlab
how to choose center block value from 4*4 blockand how to add bits into center block
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
clc;
clear all;
B=imread('trial.bmp');
A=imresize(B,[128,128]);
[a b] = size(A); % get the size of A =128*128
c=8;d=8; % reshape it into 8*8
matrices
l=0;
for i=1:c:a-7
for j=1:d:b-7
C=A((i:i+7),(j:j+7));
eval(['out_' num2str(l) '=C'])
l=l+1;
end
end
fun = @(block_struct) mean2(block_struct.data);
img = blockproc(I, [8,8],'fun');
imshow(img)
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.
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?
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
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
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.
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
I want to reshape to have a matrix 1024 x 200
this is giving me problem can anybody assist me with this
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
SIZE IS CONSIDERED BY ITS MEAN VALUE*[1 1 1;1 1 1;1 1 1] WAY FOR ALL OTHER
BLOCKS ALSO TO CONCATENATE).
So is there any way to concatenate those small blocks horizontally and
then vertically simultaneously to get the main big matrix of 1026x1026
size?
Please Reply if anyone know the process for this type of big matrix....
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?
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?
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).
can any one help with code?
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