Skip to content

Instantly share code, notes, and snippets.

@inhzus
Created April 30, 2019 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inhzus/0474a81fad1c7cd92c40ea740e9423a1 to your computer and use it in GitHub Desktop.
Save inhzus/0474a81fad1c7cd92c40ea740e9423a1 to your computer and use it in GitHub Desktop.
Inner boundary tracing
function output = my_edgelinking(image, row, col)
output = [row col;];
image(row, col) = 0;
dir = 7;
while (1)
[row, col, next_found, dir] = find_next_dir(row, col, dir, image);
if (~next_found)
break;
end
if (output(1, 1) == row && output(1, 2) == col)
break;
end
image(row, col) = 0;
output = [output; row col;];
end
function iimage = fill_image(ih, iw, iimage)
[height, width] = size(iimage);
il = iw > 2;
ir = iw < width - 1;
iu = ih > 2;
id = ih < height - 1;
if (il && iimage(ih, iw - 2) == 1)
iimage(ih, iw - 1) = 1;
end
if (ir && iimage(ih, iw + 2) == 1)
iimage(ih, iw + 1) = 1;
end
if (iu && iimage(ih - 2, iw) == 1)
iimage(ih - 1, iw) = 1;
end
if (id && iimage(ih + 2, iw) == 1)
iimage(ih + 1, iw) = 1;
end
if (il && iu && iimage(ih - 2, iw - 2) == 1)
iimage(ih - 1, iw - 1) = 1;
end
if (il && id && iimage(ih + 2, iw - 2) == 1)
iimage(ih + 1, iw - 1) = 1;
end
if (ir && iu && iimage(ih - 2, iw + 2) == 1)
iimage(ih - 1, iw + 1) = 1;
end
if (ir && id && iimage(ih + 2, iw + 2) == 1)
iimage(ih + 1, iw + 1) = 1;
end
function [frh, frw, frfound, frp] = find_next_dir(fh, fw, fdir, fimage)
[height, width] = size(fimage);
frw = fw;
frh = fh;
signs = zeros(1, 8);
if (fw < width && fimage(fh, fw + 1))
signs(1) = 1;
end
if (fw < width && fh > 1 && fimage(fh - 1, fw + 1))
signs(2) = 1;
end
if (fh > 1 && fimage(fh - 1, fw))
signs(3) = 1;
end
if (fh > 1 && fw > 1 && fimage(fh - 1, fw - 1))
signs(4) = 1;
end
if (fw > 1 && fimage(fh, fw - 1))
signs(5) = 1;
end
if (fw > 1 && fh < height && fimage(fh + 1, fw - 1))
signs(6) = 1;
end
if (fh < height && fimage(fh + 1, fw))
signs(7) = 1;
end
if (fh < height && fw < width && fimage(fh + 1, fw + 1))
signs(8) = 1;
end
frfound = 0;
frp = mod(fdir + 1, 8) + 1;
while (1)
if (signs(frp))
frfound = 1;
break;
end
frp = mod(frp, 8) + 1;
if (frp == mod(fdir + 1, 8) + 1)
break;
end
end
switch (frp)
case 1
frw = frw + 1;
case 2
frw = frw + 1;
frh = frh - 1;
case 3
frh = frh - 1;
case 4
frh = frh - 1;
frw = frw - 1;
case 5
frw = frw - 1;
case 6
frw = frw - 1;
frh = frh + 1;
case 7
frh = frh + 1;
case 8
frw = frw + 1;
frh = frh + 1;
end
frp = frp - 1;
if (mod(frp, 2) == 1)
frp = mod(frp + 6, 8);
else
frp = mod(frp + 7, 8);
end
%in this function, you should finish the edge linking utility.
%the input parameters are a matrix of a binary image containing the edge
%information and coordinates of one of the edge points of a obeject
%boundary, you should run this function multiple times to find different
%object boundaries
%the output parameter is a Q-by-2 matrix, where Q is the number of boundary
%pixels. B holds the row and column coordinates of the boundary pixels.
%you can use different methods to complete the edge linking function
%the better the quality of object boundary and the more the object boundaries, you will get higher scores
% 1 1 1
% 1 1 1
% 1 1 1 1 1
% 1 1 1
% 1 1 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment