问题描述
注意网格线?在这种特殊情况下,您只需在完整图像上调用imfilter即可。但是blockproc允许您处理大于物理内存的图像。所以对于这个讨论,想象一下我是一个巨大的tiff文件。
对于这个工作流程 - 如果您只是使用BorderSize在每个20x20块周围包含3个像素边框和 不修剪输出边框:
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');
所以 - 你真的需要修剪边框(默认)
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');
注意 - 我以IMFILTER为例。对于小图像,可以直接使用IMFITLER。只有大图像才会考虑在BLOCPROC中使用IMFITLER。
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):
But when you read the 'TrimBorder'
details, it doesn't clear up much:
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 = []
Consider all the workflows where you want to apply a function fun
on each block of size MxN in an image, but for the result to be valid you actually need the border pixels around the MxN block. (filtering, morphology, any function where a single output pixel value depends on a mxn surrounding neighborhood). i.e you need (M+m, N+n) block of input to compute one MxN output block.
Simple (aka madeup) example:
h = fspecial('gaussian', 3);
im = imread('peppers.png');
B1 = blockproc(im, [20 20], @(bs)imfilter(bs.data,h));
imshowpair(im, B1,'montage');
Notice the grid lines? In this particular case, you would just call imfilter on the full image. But blockproc lets you work with images which are 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 the 3 pixels border around each 20x20 block and did not trim 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');
So - you really need to trim the border (the 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');
Note - I used IMFILTER as an example. For small images, one would use IMFITLER directly. Only for images that are large would one consider using IMFITLER in BLOCPROC.
这篇关于MATLAB blockproc函数中的'BorderSize'和'TrimBorder'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!