Guest User

Untitled

a guest
Jan 7th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. function cc = chain_code(matrix, i, j)
  2. %matrix: array
  3. %i, j: initial pixel
  4.  
  5. [M,N] = size(matrix);
  6.  
  7. d_i = [0 -1 -1 -1 0 1 1 1];
  8. d_j = [1 1 0 -1 -1 -1 0 1];
  9.  
  10. firstVal = matrix(i, j);
  11. k = 0;
  12. w = i; y = j; %Current pixel coordinates
  13. index = 4;
  14.  
  15. while ((w ~= i) || (y ~= j))
  16. proper_neighbor = false;
  17. d = -1;
  18.  
  19. for ii = index+1:index+7 %Pixel neighbors
  20. jj = mod(ii, 8);
  21. if ((d_i(jj + 1) + w) <= M) && ((d_j(jj + 1) + y) <= N) && (d_i(jj + 1) + w > 0) && ((d_j(jj + 1) + y) > 0)
  22. if (matrix(d_i(jj + 1) + w, d_j(jj + 1) + y) == firstVal)
  23. d = jj;
  24. proper_neighbor = true;
  25. break;
  26. end;
  27. end;
  28. end;
  29.  
  30. if proper_neighbor
  31. if k < 200
  32. k = k+1;
  33. c(k) = d;
  34. w = w + d_i(d+1); y = y + d_j(d+1);
  35. index = mod(d+4, 8);
  36. end;
  37. else break;
  38. end;
  39.  
  40. if k >= 200
  41. break;
  42. end;
  43. end;
  44.  
  45. cc = c;
Advertisement
Add Comment
Please, Sign In to add comment