"BorderSize" and "TrimBorder" in the blockproc MATLAB function

Blockproc is a really useful feature for "grid" images in MATLAB. This is pretty well documented and it even comes with a page. However, when you need some kind of overlap between blocks, things get more complicated. There are some explanations on the Mathworks forums, including this one and this one , and there is an attempt to explain here (question number 1), but no one explains why some flags should be set with others. Can someone explain what the purpose of the parameter is 'BorderSize' ? It appears that when 'Trim Borders' set to false , the 'BorderSize' parameter does exactly what the documentation says (and what you expect):

'BorderSize': A two-element vector, [VH], which defines the number of border pixels to add to each block. The function adds V rows above and below each block and columns H to the 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 fun result. See the TrimBorder Option for more information. Function pads block with borders extending beyond the edges of the image with zeros.

But when you read the details of 'TrimBorder' , it doesn’t make it very clear:

'TrimBorder': a logical scalar. If set to true, the blockproc function trims the border pixels from the output of the user function, fun. The function removes V rows above and below the output of fun, and columns H from the left and right edges. The BorderSize parameter defines V and H. The default value is true, which means that the blockproc function automatically removes borders from the output signal.

Why do I want to enable 'BorderSize' (i.e. overlapping fragments) but not apply it to the output? Is this just a poorly explained flag: should 'TrimBorder' be turned off in order to use 'BorderSize' , or is there something more I am missing? I assume the essence of my confusion: when do 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 = [] 
+5
source share
2 answers

Why do I want to enable "BorderSize" (ie, overlapping fragments), but not apply it to the output?

Consider all the workflows in which you want to apply the fun function to each block of size MxN in the image, but in order for the result to be valid, you really need border pixels around the block MxN. (filtering, morphology, any function, where one value of the output pixel depends on the environment mxn). you need an (M + m, N + n) input block to compute one output block MxN.

A simple example (aka makeup):

 h = fspecial('gaussian', 3); im = imread('peppers.png'); B1 = blockproc(im, [20 20], @(bs)imfilter(bs.data,h)); imshowpair(im, B1,'montage'); 

enter image description here

Pay attention to the grid lines? In this particular case, you simply name imfilter in the full image. But blockproc allows you to work with images larger than your physical memory. So, for this discussion, imagine im is a huge tiff file.

For this workflow - if you just used BorderSize to include a border of 3 pixels around each 20x20 block and didn't crop the output border:

 h = fspecial('gaussian'); im = imread('peppers.png'); B1 = blockproc(im, [20 20], @(bs)imfilter(bs.data,h), 'BorderSize', [3 3], 'TrimBorder', false); imshowpair(im, B1,'montage'); 

enter image description here

So - you really need to crop the border (default)

 h = fspecial('gaussian'); im = imread('peppers.png'); B1 = blockproc(im, [20 20], @(bs)imfilter(bs.data,h), 'BorderSize', [3 3], 'TrimBorder', true); imshowpair(im, B1,'montage'); 

enter image description here

Note As an example, I used IMFILTER. For small images, use IMFITLER directly. Only for large images can IMFITLER be used in BLOCPROC.

+5
source

Just in case you want to get an overlapping block and save it -

  function cropSaveBlock(bs,ii) subBlockRegion =(bs.data(:,:)); fileName = [strrep(num2str(bs.location,ii),' ','_') '.jpg']; imwrite(subBlockRegion, fileName); end a = imread('cameraman.tif'); 

ii = [1-any u want filename]% [1:20] if your image has 20 result blocks

 aa=blockproc(a, [257 257], @(bs)cropSaveBlock(bs,ii),'BorderSize', [10 10 ]) 

% In this case, the border is x, y 10, 10 - of the whole image a - not of blocks

0
source

Source: https://habr.com/ru/post/1240399/


All Articles