How to crop an image into blocks and store blocks in an array or matrix ?

I need to crop an image into blocks an store the blocks in a matrix because I need to apply BoxCounting algorithm on each block.

 Accepted Answer

So, I want to Thank you four your answers.
The BoxCounting Algorithm is this: click
I will be more specific: Step 1: I need to divide a binary picture in blocks Step 2: I need to apply BoxCounting function on each block Step 3: My result need to be a matrix or an array with the values of BoxCounting applied on each block.
If I use:
T=blockproc(Ibw, blockSize, @BoxCountfracDim);
My response from Matlab is a matrix with BoxCounting applied on the full image, not on every block. I hope you understand me.
I am so grateful for your help !

1 Comment

I don't understand. blockproc() will take a block of pixels and pass it to your function. In that function you can do whatever you want - Hausdorf box counting or whatever. Then your "answer" for that block is stored as the answer for that particular location of the block, for example the block centered at (13, 450) or wherever it may be. So it does it both on every block, and on the whole image (since it was done on every block in the whole image). If my image was 10 by 20, and my blocksize was 2, and I "jumped" by the blocksize, and I returned a single scalar value for each block location, then at the output of blockproc() I'd have a 5 by 10 image, because five 2x2 blocks could fit vertically and 10 2x2 blocks could fit horizontally.

Sign in to comment.

More Answers (5)

Create a function cropAndSaveBlock.m first, which is specified in the function of blockproc. This function writes the cropped images with imwrite .
function cropAndSaveBlock(bs)
save_loc = pwd;
fileName = [save_loc, '\img', int2str(bs.location(1)), '_', int2str(bs.location(2)), '.jpg'];
imwrite(bs.data, fileName)
end
Call this function from blockproc .
After reading an image with imread, set the cropped image size. The third input argument is cropAndSaveBlock.m - the function that you want to apply to the image.
>> I = imread('peppers.png');
>> blockSize = [200 200];
>> blockproc(I, blockSize, @cropAndSaveBlock);
Thank you very much. When I run the code It doesn't return me the matrix with the stored blocks, the answer is:
Ans =
[]
How can I acces the matrix with the stored blocks ?

1 Comment

You need to make a folder called 'img' first to the current folder (pwd) by
>> mkdir('img')
Then, run the code again. .jpg file is stored under that img folder. Load those images by
>> I = imread('*filename*.jpg')

Sign in to comment.

Thank you again, your code is cropping the image into blocks stored in "img" folder. But how can I apply BoxCounting algorithm on each block? My result need to be an array or a matrix with the BoxCounting values of each block.
The result is:
Ans =
[ ]
It is okey now, thank you very much ! Problem solved.

2 Comments

please help me and tell me how the problem was solved?
I bet he used either blockproc() or mat2cell() like the FAQ said. The link is in my answer.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!