如何替换符合距离标准的像素? [英] How do I replace pixels that meet a distance criteria?

查看:122
本文介绍了如何替换符合距离标准的像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图像:

A = [3 1 1 2 2
     0 0 0 3 2
     0 0 3 3 2
     1 1 1 1 2
     1 1 1 2 2];

从图像中我获得了以下矩阵:

From the image I obtained the following matrix:

B = [1,1; 3,3; 2,4; 3,4];

现在,我想测试'B'中每个像素之间的距离查看与下一行中的直接像素相比哪些大于1。对于它们之间距离< = 1 的像素,我想要替换'A'中的两个位置a NaN ,否则,我会保留原样。

Now, I want to test the distances between each pixel in 'B' to see which ones are greater than 1 when compared to the immediate pixel in the next row. For pixels that have distances <= 1 between them, I would want to replace both locations in ‘A’ with a NaN, otherwise, I would leave them as they are.

我的预期输出为:

A = [3 1 1   2   2
     0 0 0   NaN 2
     0 0 NaN NaN 2
     1 1 1   1   2
     1 1 1   2   2];

我尝试过以下代码,但我不太明白我究竟做错了什么。

I have tried the following code, but i can’t quite understand what exactly i am doing wrong.

[row, col] = find(A==3);
B          = [row col]

for k = size(B, 1)-1
    if sqrt( (row(k,:) - (row(k+1,:)))^.2 + (col(k,:) - (col(k+1,:)))^.2 ) <= 1
       A(B(k, :)) = NaN
    end 
end

如果您对此有任何帮助,我们将不胜感激。非常感谢!

Please any help on this is greatly appreciated. Many thanks!

推荐答案

如果我理解你的问题,你想改变邻近的 3 小区到 NaN 。最简单的方法是检查所有 3 单元格并检查其中一个邻居是否为 3 ,如代码所示下面:

If I understood your question correctly, you want to change neighboring 3 cells to NaN. The easiest way is to check all 3 cells and check if one of its neighbors are 3, as done in the code below:

for k = 1:length(row)
    if safeCheck(A, row(k)+1, col(k)) || safeCheck(A, row(k), col(k)+1) || ...
            safeCheck(A, row(k)-1, col(k)) || safeCheck(A, row(k), col(k)-1)
        A(row(k), col(k)) = NaN;
    end
end

function b = safeCheck(A, row, col)
    if (1 <= row && row <= size(A, 1) && 1 <= col && col <= size(A, 2))
        b = A(row, col) == 3 || isnan(A(row, col));
    else
        b = false;
    end
end

这篇关于如何替换符合距离标准的像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆