matlab - MATLAB blockproc 函数中的 'BorderSize' 和 'TrimBorder'

标签 matlab image-processing

Blockproc是在 MATLAB 中“网格化”图像的一个非常有用的函数。它有很好的文档记录,甚至还带有 tutorial page。 .但是,当您希望 block 之间有某种重叠时,事情就变得棘手了。 Mathworks 论坛有一些解释,包括 this onethis one ,并尝试进行解释 here (问题 #1),但没有人真正解释为什么某些标志需要与其他标志一起设置。有人可以解释一下 'BorderSize' 参数的用途是什么吗?似乎当 'Trim Borders' 设置为 false 时,'BorderSize' 参数的作用与文档所说的完全相同(以及您d 期望):

'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.

但是当您阅读 'TrimBorder' 的详细信息时,它并没有弄清楚多少:

'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.

为什么我要包含 'BorderSize'(即重叠图 block )但不将其应用于输出?这只是一个解释不清的标志:'TrimBorder' 必须关闭才能使用 'BorderSize',还是我遗漏了更大的东西?我想我困惑的要点是:我什么时候希望 'TrimBorder' 设置为 false

例子:

% 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 = []

最佳答案

Why would I want to include a 'BorderSize' (i.e. overlap tiles) but not apply it to the output?

考虑所有要在图像中每个大小为 MxN 的 block 上应用函数 fun 的工作流程,但为了使结果有效,您实际上需要 MxN block 周围的边界像素。 (过滤、形态学、任何单个输出像素值取决于 mxn 周围邻域的函数)。即您需要 (M+m, N+n) 个输入 block 来计算一个 MxN 输出 block 。

简单的(又名虚构的)示例:

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

注意到网格线了吗?在这种特殊情况下,您只需对完整图像调用 imfilter。但是 blockproc 可以让你处理比你的物理内存大的图像。所以对于这个讨论,假设 im 是一个巨大的 tiff 文件。

对于此工作流程 - 如果您只是使用 BorderSize 在每个 20x20 block 周围包含 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');

enter image description here

所以 - 你真的需要修剪边框(默认)

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

注意 - 我以 IMFILTER 为例。对于小图片,可以直接使用 IMFITLER。只有对于大图像,才会考虑在 BLOCPROC 中使用 IMFITLER。

关于matlab - MATLAB blockproc 函数中的 'BorderSize' 和 'TrimBorder',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735848/

相关文章:

matlab - Matlab中的颜色直方图算法

python - Python 中的 MATLAB 定点函数 "fi"等效项

matlab - 在 MATLAB 中向量化 for 循环

matlab - 寻找旋转图像的边界

image-processing - 压缩数字化文档图像

c# - 错误 : 'Subscript indices must either be real positive integers or logicals' when using Matlab . NET 生成器

linux - gsl : make file not linking,/usr/bin/ld: 找不到-lgsl

android - 从Android中的图像进行数字检测

python - 如何使用opencv丢弃图像的边缘?

python - 使用 patchify 库创建补丁时出现的问题