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

4 views (last 30 days)
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

Preda Virgil Ionut
Preda Virgil Ionut on 4 Apr 2017
Edited: Preda Virgil Ionut on 4 Apr 2017
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
Image Analyst
Image Analyst on 4 Apr 2017
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)

mizuki
mizuki on 26 Mar 2017
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);

Image Analyst
Image Analyst on 1 Apr 2017
I don't know what that algorithm is, but see the FAQ http://matlab.wikia.com/wiki/FAQ#How_do_I_split_an_image_into_non-overlapping_blocks.3F or else use nlfilter() or blockproc() (demos attached).

Preda Virgil Ionut
Preda Virgil Ionut on 27 Mar 2017
Edited: Preda Virgil Ionut on 27 Mar 2017
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
mizuki
mizuki on 27 Mar 2017
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.


Preda Virgil Ionut
Preda Virgil Ionut on 1 Apr 2017
Edited: Preda Virgil Ionut on 1 Apr 2017
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 =
[ ]

Preda Virgil Ionut
Preda Virgil Ionut on 4 Apr 2017
It is okey now, thank you very much ! Problem solved.

Products

Community Treasure Hunt

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

Start Hunting!