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

2 次查看(过去 30 天)
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.

采纳的回答

Preda Virgil Ionut
编辑:Preda Virgil Ionut 2017-4-4
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 个评论
Image Analyst
Image Analyst 2017-4-4
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.

请先登录,再进行评论。

更多回答(5 个)

mizuki
mizuki 2017-3-26
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 2017-4-1
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 2017-3-27
编辑:Preda Virgil Ionut 2017-3-27
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 个评论
mizuki
mizuki 2017-3-27
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')

请先登录,再进行评论。


Preda Virgil Ionut
编辑:Preda Virgil Ionut 2017-4-1
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
It is okey now, thank you very much ! Problem solved.

产品

Community Treasure Hunt

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

Start Hunting!

Translated by