在MATLAB中修复图像中缺少的边界

时间:2011-08-03 18:26:22

标签: matlab image-processing

假设我有一个整数值矩阵,例如显示的矩阵:

color-coded image

在上图中,暗边界由数字 0 表示,并且一个像素宽(请忽略缩放工件)。

是否有一种在MATLAB中添加缺少黑暗边界的有效方法? (白色圆圈显示缺少边界的位置示例)。

我想保证每个彩色区域都被 4-wise 像素连接下的黑色边界完全包围。

请注意,解决方案必须将非零值翻转为零。

相关矩阵的类型为 uint32 (以上面的颜色显示)。

编辑:原始图片位于:

enter image description here

1 个答案:

答案 0 :(得分:7)

我相信你可以通过一些简单的逻辑来获得相当不错的结果,这些逻辑涉及图像的移位版本(使用CIRCSHIFT制作)。假设值为0表示颜色为黑色,则应该起作用:

rawImage = ...;                            %# Your starting image
shiftedImage = circshift(rawImage,1);      %# Shift image down one row
index = (rawImage ~= shiftedImage) & ...   %# A logical matrix with ones where
        rawImage & shiftedImage;           %#   up-down neighbors differ and
                                           %#   neither is black
rawImage(index) = 0;                       %# Set those pixels to black
shiftedImage = circshift(rawImage,[0 1]);  %# Shift image right one column
index = (rawImage ~= shiftedImage) & ...   %# A logical matrix with ones where
        rawImage & shiftedImage;           %#   left-right neighbors differ and
                                           %#   neither is black
rawImage(index) = 0;                       %# Set those pixels to black