How to divide an image into 3*3 grids?

I have a set of leaf images of different sizes..I want to divide an image into 3*3 grids and then applying processing on that 3*3 grids individually..Can anyone please tell how to divide an image into 3*3 grids?

 Accepted Answer

Have a look at blockproc
result = blockproc(img, [3 3], @somefun);

4 Comments

Thank you sir..But i want to divide an image without using any predefined function..Do you have any example code for that.
What does "without using any predefined function" mean? Simply indexing into a matrix involves the built-in subsref, addition and subtraction involve the built-in plus and minus, etc.
IIf the size of the image (width and height) is a multiple of 3, you can use mat2cell and cellfun instead of blockproc. It's not as flexible but is faster:
blocks = mat2cell(img, ones(1, size(img, 1)/3)*3, ones(1, size(img, 2)/3)*3, size(img, 3));
result = cellfun(@somefun, blocks, 'UniformOutput', false); %set 'UniformOutput', true if somefun returns a scalar.
Thank you sir..

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!