matlab - 链码无限循环

标签 matlab image-processing

我正在实现链码,我偶然发现了一个小问题。首先,我做了什么:

我从边界上的某个像素开始,然后检查存在哪个相邻像素:

3  2  1
 \ | /
4-- --0
 / | \
5  6  7

虽然这里我有一个问题:

enter image description here

小红点是算法开始的地方。那么第一个方向是 2 然后 1 然后 0 然后 0 等等...

如果你沿着黄点走,你会看到它以两个相邻的点结束。这是算法沿方向 4 进行的位置(因为在方向 0、1、2 或 3 处不存在值为 1 的像素)。尽管在下一步中它会检查方向 0(这始终是第一个开始的方向)...当然它存在,因为它是前一个位置并且会向右移动。这里它陷入了无限循环(从左到右,从右到左)。

我现在的问题是,我该如何解决这个问题?

我使用的代码是:

% Implementation of tangent angle function (chain code)

clear 
clc

directions = [ 1, 0
               1,-1
               0,-1
              -1,-1
              -1, 0
              -1, 1
               0, 1
               1, 1]

I = imread('./IMAGES/1/M_201005_1_0001.pgm');
Ibw = im2bw(I,0.15); % This is also by setting all the pixels with intesity lower than 17 to 0;


tanAngFunc ={};

[row,col] = find(Ibw);

y = col(find(max(row)==row))
x = max(row)

imshow(I)
hold on;
plot(y,x,'r.','MarkerSize',1)
hold on

l=1;

  not_done = true;
  while not_done
    if l== 36
        'test'
    end



      % Right (0)
      if Ibw(x+directions(1,2),y+directions(1,1)) ~=0

            tanAngFunc{l} = 0;
           x=  x+directions(1,2);
           y= y+directions(1,1);

      % Above right(1)
      elseif Ibw(x+directions(2,2),y+directions(2,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(2,2);
            y= y+directions(2,1);

      % Above (2)
      elseif Ibw(x+directions(3,2),y+directions(3,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(3,2);
            y= y+directions(3,1); 

      % Above left (3)
      elseif Ibw(x+directions(4,2),y+directions(4,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(4,2);
            y= y+directions(4,1);

      % Left (4)
      elseif Ibw(x+directions(5,2),y+directions(5,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(5,2);
            y= y+directions(5,1);

      % Bottom left (5)
      elseif Ibw(x+directions(6,2),y+directions(6,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(6,2);
            y= y+directions(6,1);

      % Bottom (6)
      elseif Ibw(x+directions(7,2),y+directions(7,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(7,2);
            y= y+directions(7,1);

      % Bottom right (7)
      elseif Ibw(x+directions(8,2),y+directions(8,1)) ~=0
            tanAngFunc{l} = 3;
            x= x+directions(8,2);
            y= y+directions(8,1);
      end


      plot(y,x,'y.','MarkerSize',3)
      hold on

      pause(1)
      l = l + 1;





    not_done = (x ~= col(find(max(row)==row)) && y ~= max(row));
  end

在此图像上: enter image description here

如果您想自己尝试一下

编辑:

正如评论中所建议的,在我的第二个版本中,我确保算法不使用以前访问过的像素。结果: enter image description here

如您所见,这不是一个有效的答案。

编辑2

关于@jonas的更新解决方案。我很快就把它放到了Excel中,这样更容易理解。 (尚未找到具有 1 的像素,已找到像素 x)。

enter image description here

所以这并不能 100% 有效,在处理巨大的数据集时,确保每张图片的边缘都是 2 像素宽是相当不可能的。

最佳答案

您的算法的问题在于,它仅检查要访问的“候选”像素是否是对象的一部分,但不执行其他检查以确保该像素是边界的一部分。这就是为什么它会愉快地穿过对象的内部。

一种流行的边界追踪算法是摩尔邻域追踪算法。除了Wikipedia page ,看看this page ,它更详细地解释了算法背后的想法。

关于matlab - 链码无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12885055/

相关文章:

opencv - 在文本行之间添加白线

C# 为什么 Bitmap.Save 会忽略位图的 PixelFormat?

c++ - OpenCV - 如何实现渐变图像模糊效果?

c++ - Lib svm,如何将 MyModel.mat 转换为 MyModel.model

regex - MATLAB 2012 正则表达式

matlab - 是否可以在 matlab 文件夹内的文件中搜索特定表达式/条目?

matlab - 无法执行赋值,因为此类型的变量不支持点索引

matlab - 使用 OpenCV 和 Matlab 的校准图像和三角网格的深度图

ios - 使用 UISlider 增加/减少图像的亮度?

c++ - 使用openCV在C++中查看灰度RAW图像文件