matlab - 如何从图像中删除/替换边框像素?

标签 matlab image-processing

我是 Matlab 新手,需要一些帮助。我有一个分段图像,我想从图像边框中删除背景(或黑色)像素(值== 1)。我已经能够获得我不想要的边界像素的图像掩模。 Interior_blackPixels 很有用,但我想摆脱outer_blackPixels,但是,我不确定如何使用我拥有的掩码(outer_blackPixels)检索没有黑色像素边框的最终图像。到目前为止的代码如下所示:

img =   [1 1 1 1 1 1 1 1
         1 1 1 1 2 2 2 1
         1 1 1 2 2 2 2 1
         1 1 1 2 2 2 2 1
         1 1 2 2 2 2 2 1
         1 1 2 2 2 2 2 1
         1 3 3 1 1 1 3 1
         1 3 3 1 1 1 3 1
         1 3 3 3 3 3 3 1
         1 1 1 1 1 1 1 1];

% Get the black pixels image array
blackPixels = (img == 1);

% Obtain the other pixels by negating the black pixels
otherPixels = ~blackPixels

% Get the border black pixels (or mask)
outer_blackPixels    = blackPixels & ~imclearborder(blackPixels)
interior_blackPixels = blackPixels & ~outer_blackPixels

请注意,我不介意将outer_blackPixels的像素值替换为“0”,因为这不会影响我的分析。因此,我希望我的最终图像是这样的:

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

任何帮助/建议将不胜感激。谢谢!

最佳答案

您可以使用bwlabel来区分1的区域。由于此函数仅处理二进制图像,因此您可以使用逻辑img转换为二进制:

L=bwlabel(~logical(img-1),4)

然后将边框转换为 0:

img(L==L(1))=0

img =

 0     0     0     0     0     0     0     0
 0     0     0     0     2     2     2     0
 0     0     0     2     2     2     2     0
 0     0     0     2     2     2     2     0
 0     0     2     2     2     2     2     0
 0     0     2     2     2     2     2     0
 0     3     3     1     1     1     3     0
 0     3     3     1     1     1     3     0
 0     3     3     3     3     3     3     0
 0     0     0     0     0     0     0     0

关于matlab - 如何从图像中删除/替换边框像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41216646/

相关文章:

c++ - Matlab Random 和 C++ Random 是否有相同的种子?

debugging - 为什么断点在 Matlab 编辑器窗口中不可见?

python - 使用 Python 图像处理分割生物样本的照片以提取感兴趣的圆形区域

c++ - RGB 到 LAB 并返回错误

image-processing - 如何使用 OpenCV 查找图像上一个点与另一个点的坐标

c# - 计算量大导致栈溢出错误

matlab - Numpy vs Matlab float 到 uint8 转换

matlab - 非线性黑盒系统辨识

matlab - 如何以 Matlab eps 格式导出变音符号(或任何外来字符)?

java - 如何在Java中计算共现矩阵?