matlab - 如何将矩阵中强元素附近的弱元素归零?

标签 matlab image-processing matrix distance vectorization

我有一个包含一些点和很多零元素的像素矩阵。从那些非零点开始,我想丢弃那些在矩阵范围 N 中具有更强点的点。范围是像素之间的欧氏距离。

input  = [0.0 0.0 0.0 0.9 0.0 0.0 
          0.0 0.0 0.2 0.0 0.0 0.5 
          0.0 0.0 0.7 0.0 0.0 0.0 
          0.0 0.4 0.1 0.0 0.0 0.0];

output = [0.0 0.0 0.0 0.9 0.0 0.0   % 0.7 is the largest number in range
          0.0 0.0 0.0 0.0 0.0 0.5   % 0.2 got removed; was next to 0.9 and 0.7
          0.0 0.0 0.7 0.0 0.0 0.0   % 0.7 is the largest number in range
          0.0 0.0 0.0 0.0 0.0 0.0]; % 0.1 and 0.4 both got removed; were next to 0.7

更新:这就是我到目前为止的想法。它遍历所有非零像素并将当前像素与最大的邻域像素进行比较。然而,邻域包含很多像素。我需要以某种方式选择一个圆形区域,而不是通过索引偏移量来选择区域。此外,如果有更短的方法,我将不胜感激,也许可以用 conv2imfilter 等内置 Matlab 函数替换循环。

% Discard points near stronger points
points = find(Image > 0);
radius = args.Results.min_dist;
for i = 1:size(points)
    [index_y, index_x] = ind2sub(size(Image), points(i));
    %  Find neighborhood
    from_x = max(index_x-radius, 1);
    from_y = max(index_y-radius, 1);
    to_x = min(index_x+radius, size(Image, 2));
    to_y = min(index_y+radius, size(Image, 1));
    neighbors = Image(from_y:to_y, from_x:to_x);
    % Discard if there is a stronger neighbor
    largest = max(max(neighbors));
    if Image(index_y, index_x) < largest
        Image(index_y, index_x) = 0;
    end
end

最佳答案

如果您安装了图像处理工具箱(或 Try Hard's answer 中提到的免费替代品之一),您可以很容易地做到这一点:

imdilate 函数基本上是一个滑动最大过滤器。所以我们要做的是,对于每个像素,我们在您的半径 R 指定的邻域中寻找最大值。然后我们将实际值与找到的最大值进行比较,如果较小,我们将值设置为零。

function A = zeroOutWeakElements(A, R)
[X,Y] = ndgrid(-ceil(R):ceil(R));
neighborhood = (X.^2 + Y.^2)<=R^2;
A(imdilate(A,neighborhood)>A) = 0;

对于大型全矩阵和小距离,这也比当前接受的解决方案快得多。这种优势会随着稀疏矩阵和大半径而逐渐消失,但我想您应该使用实际数据进行测试,以确定什么是最好的。

关于matlab - 如何将矩阵中强元素附近的弱元素归零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407975/

相关文章:

matlab - Matlab中的简单序列特征选择

python - 确定时空图像的图案方向

python - 图像中物体的颜色检测

c++ - 对于大于 255 的值(即类型 CV_8UC1),如何使用 OpenCV 的 adaptiveThreshold?

java - 根据单词之间的空格查找文本的宽度(JAVA)

matlab - 具有相同x轴的两个y轴

matlab - 复数 FFT 然后逆 FFT MATLAB

oop - 清除 MATLAB 中的类定义

c - 程序每次从文件中扫描矩阵时都会跳过一行!

c++ - 使用 Petsc 库用 vector 构造稀疏矩阵