How to crop an image into blocks and store blocks in an array or matrix ?
Show older comments
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
More Answers (5)
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
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
on 1 Apr 2017
1 vote
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
on 27 Mar 2017
Edited: Preda Virgil Ionut
on 27 Mar 2017
1 Comment
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')
Preda Virgil Ionut
on 1 Apr 2017
Edited: Preda Virgil Ionut
on 1 Apr 2017
Preda Virgil Ionut
on 4 Apr 2017
0 votes
2 Comments
M Sh
on 20 Oct 2019
Hello Preda Virgil Ionut
please help me and tell me how the problem was solved?
Image Analyst
on 20 Oct 2019
I bet he used either blockproc() or mat2cell() like the FAQ said. The link is in my answer.
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!