gpt4 book ai didi

matlab - 链码无限循环

转载 作者:行者123 更新时间:2023-12-04 05:28:44 24 4
gpt4 key购买 nike

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

我从边界上的某个像素开始,然后根据以下原因检查存在哪个相邻像素:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com