Blockproc
is a really useful function for "gridding" an image in MATLAB. It's pretty well documented, and it even comes complete with a tutorial page. However, when you want some sort of overlap between blocks, things get trickier. The Mathworks forums have some explanations, including this one and this one, and there's an attempt at an explanation here (Question #1), but nobody really explains anywhere why certain flags need to be set with other ones. Can someone please explain what the purpose of the 'BorderSize'
parameter is? It seems that when 'Trim Borders'
is set to false
, the 'BorderSize'
parameter does exactly what the documentation says (and what you'd expect):
'BorderSize': A two-element vector, [V H], specifying the amount of border pixels to add to each block. The function adds V rows above and below each block and H columns left and right of each block. The size of each resulting block will be: [M + 2*V, N + 2*H]
By default, the function automatically removes the border from the result of fun. See the 'TrimBorder' parameter for more information. The function pads blocks with borders extending beyond the image edges with zeros.
But when you read the 'TrimBorder'
details, it doesn't clear up much:
'TrimBorder': A logical scalar. When set to true, the blockproc function trims off border pixels from the output of the user function, fun. The function removes V rows from the top and bottom of the output of fun, and H columns from the left and right edges. The 'BorderSize' parameter defines V and H. The default is true, meaning that the blockproc function automatically removes borders from the fun output.
Why would I want to include a 'BorderSize'
(i.e. overlap tiles) but not apply it to the output? Is this just a poorly explained flag: 'TrimBorder'
must be off in order to use 'BorderSize'
, or is there something bigger I'm missing? I guess the gist of my confusion is: when would I want 'TrimBorder'
set to false
?
Examples:
% Non-overlapping
A = ones(10);
B = blockproc(A, [2,2], @(x)sum(sum(x.data)));
% B =
% [ 4 4 4 4 4 ]
% [ 4 4 4 4 4 ]
% [ 4 4 4 4 4 ]
% [ 4 4 4 4 4 ]
% [ 4 4 4 4 4 ]
% GOOD Overlapping--one-pixel border
B = blockproc(A, [2,2], @(x)sum(sum(x.data)), 'BorderSize', [1,1], 'TrimBorder', false);
% B =
% [ 9 12 12 12 9 ]
% [ 12 16 16 16 12 ]
% [ 12 16 16 16 12 ]
% [ 12 16 16 16 12 ]
% [ 9 12 12 12 9 ]
% BAD Overlapping--one-pixel border
B = blockproc(A, [2,2], @(x)sum(sum(x.data)), 'BorderSize', [1,1]);
% B = []