divide an image to overlapped blocks

3 次查看(过去 30 天)
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?

采纳的回答

Geoff Hayes
Geoff Hayes 2016-7-30
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.

更多回答(1 个)

Image Analyst
Image Analyst 2016-7-30
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 个评论
nadia
nadia 2016-7-31
I need them for building a database of patches, thank you.
Image Analyst
Image Analyst 2016-7-31
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.

请先登录,再进行评论。

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by