How to create sub images of a main image?

14 views (last 30 days)
Alejandro Cruz Rubio
Alejandro Cruz Rubio on 17 May 2019
Answered: Guillaume on 17 May 2019
Hi everyone,
I need to create and save images of a main image. My resolution of my main image is 1024x2048 and I have to save images of 256x256 of all array (main image), so I have to go through the entire array and save images of 256x256 consecutively
Does anybody Know how to do that?
Thanks everyone:)

Answers (1)

Guillaume
Guillaume on 17 May 2019
%inputs:
%img: a full colour (3D), or greyscale image (2D)
%blocksize: a 2-element vector giving the height and width of each block
%
%precondition: the image size is a multiple of blocksize
assert(all(mod([size(img, 1); size(img, 2)], blocksize(:)) == 0), 'size of image is not a multiple of blocksize');
numblocks = [size(img, 1); size(img, 2)] ./ blocksize(:);
subimages = mat2cell(img, ones(1, numblocks(1)) * blocksize(1), ones(1, numblocks(2)) * blocksize(2), size(img, 3));
subimages is a cell array containing each 256x256 block. You can then save these in a loop. mat2cell is the function that does the extraction of each block.

Community Treasure Hunt

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

Start Hunting!