【问题标题】:Error using colfilt function in MatLab/Octave在 MatLab/Octave 中使用 colfilt 函数时出错
【发布时间】:2014-10-19 04:01:56
【问题描述】:

我正在尝试使用 colfilt 函数通过图像运行自定义过滤器。这个过滤器基本上在图像中运行一个 nxn 窗口,并将中心像素替换为其最小和最大和的一半。代码是这样的:

colfilt(image, 3, "sliding", @(x) (min(x(:))+max(x(:)))/2)

但是,我收到了这个错误:

error: col2im: can't resize B in matrix sized (A_SIZE - BLOCK_SIZE +1)
error: called from:
error:   /usr/share/octave/packages/image-2.2.1/col2im.m at line 143, column 9
error:   /usr/share/octave/packages/image-2.2.1/colfilt.m at line 152, column 9

如果我用 nfilter 替换函数,如下所示

nlfilter(image, [n n], @(x) (min(x(:))+max(x(:)))/2)

它工作正常,但它太慢了,所以我认为第一个选项必须更好。

有谁知道如何让它工作?

提前致谢。

【问题讨论】:

  • 看起来您分配colfilt 的结果的矩阵的大小应该是 A - blocks_size 的大小(在你的情况下为 3)+ 1 = A - 2 的大小和它没有这样的尺寸。
  • 但我没有将结果分配给任何变量
  • 你 - 不是,但是 colfilt 调用了 col2im 并且确实如此。原始图像的尺寸是多少?尝试不仅使用3,还使用[3 3]
  • 我在一个 6x6 的假矩阵上测试它。原始图像为 512x512。我对[3 3] 有同样的错误

标签: matlab image-processing octave threshold


【解决方案1】:

您需要更仔细地阅读colfilt 的文档。以下是关于sliding 选项的内容:

B = colfilt(A,[M N],'sliding',FUN) rearranges each M-by-N sliding
neighborhood of A into a column in a temporary matrix, and then applies
the function FUN to this matrix. FUN must return a row vector containing
a single value for each column in the temporary matrix. (Column
compression functions such as SUM return the appropriate type of
output.) colfilt then rearranges the vector returned by FUN into a
matrix of the same size as A.

关键句是:FUN 必须为临时矩阵中的每一列返回一个包含单个值的行向量。 colfilt 所做的是将每个像素邻域转换为单个列并将所有这些列连接到一个临时矩阵中。因此,每一列代表一个像素邻域。因此,您需要编写匿名函数,以便输出将是单行向量,其中该行向量中的每个元素是如果您将函数应用于位于每个像素邻域中的每个像素邻域的输出结果这个临时矩阵的列。因此,您只需修改匿名函数即可:

out = colfilt(image, [3 3], 'sliding', @(x) ((min(x)+max(x))/2));

请注意,您需要将第二个输入指定为[3 3],而不仅仅是您帖子中的 cmets 中注意到的单个数字 3。现在应该独立地找到每列的最小值,将其与每列的最大值相加,然后除以 2。这个行向量现在应该输出您处理的每个像素邻域的结果。

【讨论】:

  • 感谢您的澄清!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2014-07-06
相关资源
最近更新 更多