How can i divide an image into Overlapping Blocks without using for loop?
Show older comments
Hi all, Lets suppose i have an image of size 64x48, i want to divide it into blocks of size 4x4 (or any size) with an overlap of 1 (or any size less than 4 incase of 4). I tried to read blockproc, but one they need some function to be applied on it which i don't have currently, and second they don't give option of overlapping.
How can i do that?
THanks in advance.
Answers (2)
myImage = rand(64,48);
x = 1:3:64;
y = 1:3:48;
[X,Y] = meshgrid(x,y);
subImages = arrayfun( @(x,y) myImage(x:min(end,x+3),y:min(end,y+3)), X, Y, 'UniformOutput', false )';
should give you a cell array containing 4*4 overlapping sub-images upto the final one which may be smaller due to the image size.
I tested that in a bit of a rush though so there may be some elements not quite right. Hopefully it gives you an idea though if it isn't a complete answer.
6 Comments
Adam
on 18 Sep 2014
You mean 6x6 still with overlap of 1?
That still works for me with that method. Obviously you have to change those hard-coded 3's to 5s though.
More generically:
myImage = rand(64,48);
xBlockSize = 6;
yBlockSize = 6;
xOverlap = 1;
yOverlap = 1;
x = 1:(xBlockSize - xOverlap):64;
y = 1:(yBlockSize - yOverlap):48;
[X,Y] = meshgrid(x,y);
subImages = arrayfun( @(x,y) myImage( x:min( end, x + xBlockSize - 1), y:min( end, y + yBlockSize - 1) ), X, Y, 'UniformOutput', false )';
Obviously you could be even more generic by replacing the hard-coded 64 and 48, but that part is trivial.
Rashmi Taneja
on 4 Jun 2016
How can I divide an image into overlapping sub-blocks by sliding window?
Image Analyst
on 4 Jun 2016
You can use blockproc() (demos attached) or if the overlap is all but one pixel, you can use nlfilter().
Elwin Chandramonie
on 1 Feb 2017
How this can be extended to 3 dimensions?
Image Analyst
on 2 Feb 2017
You'd have to write your own code for that since blockproc only works with 2-D images.
Image Analyst
on 18 Sep 2014
Edited: Image Analyst
on 18 Sep 2014
1 vote
See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_split_an_image_into_non-overlapping_blocks.3F. Neither of the two methods listed uses blockproc(). I'm pretty sure you can figure out how to adapt it from non-overlapping to overlapping by 1 pixel.
8 Comments
ihsan
on 22 Sep 2014
Image Analyst
on 22 Sep 2014
Why do you hate loops? You could do a million of them in less than a millisecond. If you don't like the indexing way, then you can use blockproc(). With the proper parameters, you can have the blocks overlap each other. Or you could use nlfilter() which slides over a pixel at a time.
Image Analyst
on 23 Sep 2014
I just did 100 million, yes one hundred million , for loop iterations and it took 0.2 seconds , so it's not the for loop that's taking up all the time. It will be the disk I/O, memory access (if indexing in an inefficient order), and image processing time rather than anything having to do with the for loop. Attached is a demo for nlfilter().
ihsan
on 23 Sep 2014
Image Analyst
on 23 Sep 2014
Convolution, done by conv2(), and correlation, done by xcorr2(), can do processing by overlapping block (windows) thought the overlap is by N-1 pixel where N is the window size. In other words, they slide over by one pixel at a time. If you want to do something custom, then use nlfilter() which is built for that.
Image Analyst
on 23 Sep 2014
I'm attaching a demo for nlfilter(). See the function called LocalOtsu? Just rename it to whatever you want that describes the operations you want. Then put whatever custom operations you want inside it.
By the way, what you described is just a simple cross correlation and can be done with xcorr2().
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!