Applying Moore's Boundary Tracing Algorithm for binary image

2 次查看(过去 30 天)
Hello everyone!! I have tried to implement Moore's Boundary Tracing Algorithm for Edge Linking Operation of the binary image. This is my code:
function boundary = traceit(input)
binary = logical(input); [rows,columns] = size(binary); padded(rows+2,columns+2) = 0; padded(2:rows+1,2:columns+1) = binary;
N = circshift(padded,[0 1]); S = circshift(padded,[0 -1]); E = circshift(padded,[-1 0]); W = circshift(padded,[1 0]); boundary_image = padded - (padded + N + S + E + W == 5);
boundary_size = sum(boundary_image(:)) + 1; boundary(boundary_size,2) = 0;
for i = 1:rows for j = 1:columns if binary(i,j) == 1 break; end end if binary(i,j) == 1 break; end end
initial_entry = [j,i] + 1;
neighborhood = [-1 0; -1 -1; 0 -1; 1 -1; 1 0; 1 1; 0 1; -1 1]; exit_direction = [7 7 1 1 3 3 5 5];
for n = 1:8 c = initial_entry + neighborhood(n,:); if padded(c(2),c(1)) initial_position = c; break; end end
initial_direction = exit_direction(n);
boundary(1,:) = initial_position;
position = initial_position; direction = initial_direction; boundary_size = 1;
while true for n = circshift(1:8,[0,1-direction]) c = position + neighborhood(n,:); if padded(c(2),c(1)) position = c; break; end end
direction = exit_direction(n); boundary_size = boundary_size + 1; boundary(boundary_size,:) = position;
if all(position == initial_position) &&...
(direction == initial_direction)
break;
end
end
boundary = boundary - 1; end
After that I try to apply this function for my target image. I have perform the following conversion on the image before using the function: RGB - Gray Scale - Binary - Canny Edge Detector
im = imread('shape.png'); img = rgb2gray(im); BW = im2bw(img,0.5); e = edge(BW,'canny'); imshow(e,[])
e = traceit(out); figure imshow(out)
After I have computed, it does not return with any error, but the algorithm also doesn't work in the manner I expected neither. Please kindly give me some advice.
Thank you very much!!!
  1 个评论
John D'Errico
John D'Errico 2016-6-8
Time to learn to use the debugger. Look at what it is doing. Then think about why there is something strange happening.

请先登录,再进行评论。

回答(0 个)

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by