matlab - 将不需要的值替换为最接近的恰好为正的索引值

标签 matlab image-processing matrix computer-vision interpolation

本题与上一题不同(How to find indices with a negative value and replace the value with the nearest index's value that happens to be positive?)。 上一个问题是用同一行上最接近的正索引值替换索引的不需要的值。

这个问题是用它最接近的正索引值替换不需要的值在整个矩阵中(不仅限于同一行)

  • 如果在整个矩阵中有多个索引最接近观察到的索引,则随机取值(这并不重要)。

  • 我正在处理一个 1003x1170 的单矩阵。因此,如果解决方案不会带来太多开销,那将是最好的。 (可选)

例如,如果负值是不需要的,

[-255  4  6;
   -5 -4  5;
 -400  3  6;
   -6 -7 -8;
    3 -5  4]

变成

[4 4 6;
 4 5 5;
 3 3 6;
 3 3 6;
 3 4 4]

(答案不一定是这样,因为它随机取最近的值(右/左/上/下,如果通过添加一些一致性使事情变得更容易,那也很好。))

目的:我试图这样做是为了去除观察到的图像矩阵中的大尺寸噪声孔,而不会破坏图像的清晰度。

感谢您的支持!

最佳答案

以下代码将每个负数替换为最接近的非负数。如果在相同的最小距离处有多个非负数,则使用线性顺序中的第一个

没有循环

这构建了一个非常大的矩阵作为中间结果。由于内存限制,它可能不适用于大输入。

matrix = [-255 4 6; -5 -4 5; -400 3 16; -6 -7 -8; 13 -5  14];
[r_use, c_use] = find(matrix>=0); % row and column indices of useful values
z_use = r_use+1j*c_use; % same as complex number
[r_neg, c_neg] = find(matrix<0); % row and column indices of negative values
z_neg = r_neg+1j*c_neg; % same as complex number
[~, ind_min] = min(abs(bsxfun(@minus, z_use, z_neg.')), [], 1); % compute distance
    % between each useful value and each negative value. For each negative value, 
    % give  index of nearest useful value. This index is referred to r_use, c_use
ind_use = sub2ind(size(matrix), r_use(ind_min), c_use(ind_min)); % linear indices 
    % of useful values that will replace the negative values
ind_neg = sub2ind(size(matrix), r_neg, c_neg); % linear indices of negative values
matrix(ind_neg) = matrix(ind_use); % replace

之前:

matrix =
  -255     4     6
    -5    -4     5
  -400     3    16
    -6    -7    -8
    13    -5    14

之后:

matrix =
     4     4     6
     4     4     5
     3     3    16
    13     3    16
    13    13    14

带循环

这通过使用循环一次处理一个负值来减少内存消耗。

matrix = [-255 4 6; -5 -4 5; -400 3 16; -6 -7 -8; 13 -5  14];
[r_use, c_use] = find(matrix>=0); % row and column indices of useful values
z_use = r_use+1j*c_use; % same as complex number
[r_neg, c_neg] = find(matrix<0); % row and column indices of negative values
z_neg = r_neg+1j*c_neg; % same as complex number
for k = 1:numel(z_neg) % for each negative value
    [~, ind_min_k] = min(abs(z_use-z_neg(k))); % compute distance between
    % each useful value and this negative value. Give index of nearest
    % useful value. This index is referred to r_use, c_use
    ind_use_k = sub2ind(size(matrix), r_use(ind_min_k), c_use(ind_min_k));
    % linear index of useful value that will replace this negative value
    ind_neg_k = sub2ind(size(matrix), r_neg(k), c_neg(k)); % linear index
    % of this negative value
    matrix(ind_neg_k) = matrix(ind_use_k); % replace
end

关于matlab - 将不需要的值替换为最接近的恰好为正的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753924/

相关文章:

matlab - 四面体面的大型、大部分为空的数据结构

c++ - Opencv 平均滤波器给出与 Matlab 平均滤波器不同的输出

matlab:在 500x500 像素 .jpg 图像中找到绿色边缘的 x-y 位置

python - MATLAB 矩阵乘法性能比 NumPy 快 5 倍

php - 在 PHP 中屏蔽和分层图像

php - 如何检测图像名称是否不同但图像在php中相同

c++ - CUDA 并发内核启动不起作用

r - 将向量转换为 data.frame,每个唯一值对应一列

python - 矩阵外的函数输入

java - 3d 矩阵作为 2d 参数