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