Difference between blockproc() and for loop method of splitting an image with overlap?

3 views (last 30 days)
So I'm required to split an image into overlapping blocks of 8x8 and 4x4 pixels overlap, and perform dct on each block after that. I know of two methods blockproc() and a for loop method of splitting the image. But I'm wondering, would the output(dct matrix) be different between the methods?
For what i understood, the blockproc() method creates a border around the image in case of overlapping where as the for loop method from https://matlab.fandom.com/wiki/Split_image_into_blocks does not. (Note, the method in the link is for non-overlapping, but some modifications can be made for overlapping, which i did). In the case of blockproc(), the code is:
T2 = dctmtx(8);
dct_2 = @(block_struct) T * block_struct.data * T';
B1 = blockproc(I, [4 4], dct_2, 'BorderSize',[2 2], 'TrimBorder', false, 'PadPartialBlocks',true)
'BorderSize' [2 2] means that blockproc creates a border of 2 pixles top, down, right and left of the image and performs dct then. But in the case of the for loop method above, it doesn't use any borders for padding. Hence the dct when computed by each method will be different. I would like to know which method is more feasible and correct.

Answers (1)

Guillaume
Guillaume on 10 Aug 2019
Edited: Guillaume on 10 Aug 2019
Hum, a for loop just iterates over whatever you want and execute whichever code you want. You can write a for loop that does exactly the same as blockproc, or something different. The choice is up to you. There's no inherent for loop method. It's whatever you want it to be.
However, note that blockproc doesn't do overlapping blocks. Also note that by default blockproc does not add a border, despite your statement.
One of the functions you can use for processing along a sliding-window is nlfilter. This function does indeed pad the image if necessary (if you don't want that just trim the image). If it's possible, you should be using colfilt instead.
But regardless which function you choose, again, you can implement the exact same thing with for loops and any deviation is your choice.
  4 Comments
Stewart Tan
Stewart Tan on 10 Aug 2019
Edited: Stewart Tan on 10 Aug 2019
@Guillaume thanks. That cleared up a bit. Also, for the case of for loops, would I even have to consider padding the image? I’ve actually implemented it(not sure right or not)but it doesn’t perform padding(the link in the question) and when I computed the dct for each block, it is different from the output of blockproc. That’s why I began becoming confuse on which is right and wrong. I’ll try out nlfilter and colfilt soon enough and get back to you whenever necessary.
Guillaume
Guillaume on 12 Aug 2019
would I even have to consider padding the image
That depends solely on your algorithm. Does it require blocks that are always the same size? If so, then you'll either have to pad the smaller blocks (when you reach the edges of the image) or ignore these blocks. If your algorithm doesn't care about the size of the blocks, then padding is not necessary.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!