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 日
編集済み: 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 日
編集済み: 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
Preda Virgil Ionut 2017 年 4 月 4 日
It is okey now, thank you very much ! Problem solved.
  2 件のコメント
M Sh
M Sh 2019 年 10 月 20 日
please help me and tell me how the problem was solved?
Image Analyst
Image Analyst 2019 年 10 月 20 日
I bet he used either blockproc() or mat2cell() like the FAQ said. The link is in my answer.

サインインしてコメントする。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by