divide an image to overlapped blocks

3 views (last 30 days)
I have an image with size 512*512 and I want to extract all blocks with size 31*31 in this image by sliding this window on each pixel of it,now how can I do it?

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Jul 2016
nadia - if you want to extract every 31x31 block, then you could try using blockproc from the Image Processing Toolbox, or you could try the following (which should work though is not necessarily that efficient). Create a cell array for all of your distinct blocks as
blockSize = 31; % assumed square
imageSize = 512; % assumed square
blockArray = cell(imageSize - blockSize +1);
Now iterate over each 31x31 block as follows
for r=1:size(blockArray,1)
for c=1:size(blockArray,2)
blockArray{r,c} = myImage(r:r - 1 + blockSize, ...
c:c - 1 + blockSize);
end
end
where myImage is your 512x512 image. Note how we move the sliding window across each row r and column c.

More Answers (1)

Image Analyst
Image Analyst on 30 Jul 2016
It's not memory efficient to keep all of these blocks in memory at the same time. What do you want to do with them? Can't you just extract the block and "use" it right then and there so that you don't have all those in memory at the same time? This is what blockproc() would do, or you could do it manually with a double for loop.
  2 Comments
nadia
nadia on 31 Jul 2016
I need them for building a database of patches, thank you.
Image Analyst
Image Analyst on 31 Jul 2016
How is the database being constructed? Are you just saving sub-images to files in a folder? Or are you using a regular database, like Oracle or SAS or something, and the Database Toolbox? Either way, I don't see any need to process them all into a single cell array instead of just processing one sub-image at a time. If you want all those in memory at one time, then 31*31 is about a kilobyte. And if you have a megapixel image with a kilobyte at every pixel, that's a gigabyte. Not huge but getting up there, and, I think, wasteful because you don't need to do it.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!