【问题标题】:Using roifilt2 on an image in Matlab在 Matlab 中的图像上使用 roifilt2
【发布时间】:2018-12-16 08:15:09
【问题描述】:

我正在尝试使用roifilt2 函数对蒙版区域内的 400 x 640 图像中的所有值求和。我使用roipoly 创建了一个蒙版,它还存储为一个 400 x 640 矩阵,在我不感兴趣的单元格中为 0,在我希望总结的单元格中为 1。

我想知道您将如何解决这个问题,我查看了 roifilt2 的 mathworks 页面,但我不明白如何正确地将 sum 函数传递给它。这就是我正在尝试的:

fh = @(I)(sum(I,2));
I2 = roifilt2(I, mask, fh);

我是我的形象,面具是我的面具。我也不断收到以下错误,即使我的 I 和我的面罩大小相同:

Error using roifilt2 (line 79) The filtered image is not the same size as the original image.

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 为什么不直接使用sum(sum(I.*mask)) 或sum(I(:).*mask(:))?掩码会将您不关心的所有位置归零。
  • 我仍然收到 The filtered image is not the same size as the original image. 作为错误,抱歉我忘了说我也遇到了这个问题。
  • 我上面的函数不需要使用roifilt2。返回值将是遮罩区域中像素的总和,这就是您所要求的。
  • 我在尝试该功能时收到ans = NaN?我认为这是因为我的面具是 而我的 I 是 有什么想法吗?
  • 更改矩阵标志没有帮助,两个方程仍然返回 NaN 作为输出。

标签: matlab filter sum mask roi


【解决方案1】:

问题是总和改变了输出图像的大小,尝试清除区域外的所有值然后求和

% Define the function you want to use as a filter. 
% This function, named f, passes the input image x to the imsharpen 
% function and specifies the strength of the sharpening effect
% by using the 'Amount' name-value pair argument.
% f simply puts the values to 0
f = @(x)x-x;

% Filter the ROI using the roifilt2 function and specifying the image, mask, and filtering function.
% This filter clears all the values which are not in the region of interest  
interest_region = roifilt2(img, ~mask, f);

因此,您所在地区的最终总和将是:

img_sum = sum(interest_region,'all');

无论如何,正如 Raab70 所写,您可以在不使用 roifilt2 的情况下乘以掩码以获得相同的结果:

img_sum = sum(img.*mask,'all');

【讨论】:

  • 如果mask 是合乎逻辑的,则使用索引而不是乘法:img(mask)。这将返回一个列向量,其中包含img 中的所有值,其中mask 是true。
  • 对,那也会更快
猜你喜欢
  • 2018-04-27
  • 2011-12-19
  • 1970-01-01
  • 2014-06-29
  • 2014-12-20
  • 2018-06-15
  • 2011-11-04
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多