Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. %318810637 Perov Danny
  2. %318455813 Eden Klainer
  3. %
  4. %The function finds the edges in the image using the Canny edge detector.
  5. %Input: grayscale image
  6. %Output: binary image with only the images.
  7. function [newImg] = edgeDetect(img)
  8.  
  9. %Smooth the image.
  10. img = conv2(img, [1,2,1; 2, 4, 2; 1, 2, 1] / 16, 'same');
  11.  
  12. %Find the gradient.
  13. Sx = conv2(img, [1, -1], 'same');
  14. Sy = conv2(img, [1; -1], 'same');
  15. S = sqrt(Sx.^2 + Sy.^2);
  16. teta = Sy ./ Sx;
  17.  
  18. [rows, cols] = size(img);
  19. tempImg = S;
  20.  
  21. %Go over all the pixels, and compare each and one of them to its neighbours
  22. for r = 1 : rows
  23. for c = 1 : cols
  24.  
  25. if(isnan(teta(r, c)) == 1 || isinf(teta(r, c)) == 1 || r == 1 || c == 1 || r == rows || c == cols)
  26. tempImg(r, c) = 0;
  27. continue;
  28. end
  29.  
  30. if(-0.4142 <= teta(r, c) && teta(r, c) <= 0.4142)
  31. if(S(r, c) <= S(r, c - 1) || S(r, c) <= S(r, c + 1))
  32. tempImg(r, c) = 0;
  33. end
  34.  
  35. elseif(0.4142 <= teta(r, c) && teta(r, c) <= 2.4142)
  36. if(S(r, c) <= S(r - 1, c + 1) || S(r, c) <= S(r + 1, c - 1))
  37. tempImg(r, c) = 0;
  38. end
  39.  
  40. elseif(abs(teta(r, c)) >= 2.4142 )
  41. if(S(r, c) <= S(r - 1, c) || S(r, c) <= S(r + 1, c))
  42. tempImg(r, c) = 0;
  43. end
  44.  
  45. elseif(-2.4142 <= teta(r, c) && teta(r, c) <= -0.4142)
  46. if(S(r, c) <= S(r - 1, c - 1) || S(r, c) <= S(r + 1, c + 1))
  47. tempImg(r, c) = 0;
  48.  
  49. end
  50. end
  51. end
  52. end
  53.  
  54. Tmax = 17/255;%43 / 255; % TODO decide upon values
  55. Tmin = 5/255;%10 / 255;
  56.  
  57. %Create matrices which contain values above the given thresholds
  58. aboveTMax = (tempImg > Tmax);
  59. aboveTMin = (tempImg > Tmin);
  60.  
  61. %Create a matix in which a strong cell has value 2, and a weak one has value 1
  62. aboveThreshold = aboveTMax + aboveTMin;
  63. finalEdges = aboveTMax;
  64. counter = 1;
  65. currentPixels = [];
  66.  
  67. %Check each of the cells to find the edges by using the local patch
  68. for r = 2 : rows - 1
  69. for c = 2 : cols - 1
  70. if(aboveThreshold(r, c) ~= 1)
  71. continue;
  72. end
  73.  
  74. localPatch = aboveThreshold(r - 1 : r + 1, c - 1 : c + 1);
  75. patchMax = max(max(localPatch));
  76.  
  77. if(patchMax == 2)
  78. currentPixels(counter, 1) = r;
  79. currentPixels(counter, 2) = c;
  80. counter = counter + 1;
  81. finalEdges(r, c) = 1;
  82. end
  83. end
  84. end
  85.  
  86. counter = 1;
  87.  
  88. %Set the final edge values in the result matrix.
  89. while(size(currentPixels, 1) > 0)
  90. newPix = [];
  91. for i = 1 : size(currentPixels, 1)
  92. r = currentPixels(i, 1);
  93. c = currentPixels(i, 2);
  94. if r ~= 0 && c ~= 0
  95. for dr = -1 : 1
  96. for dc = -1 : 1
  97. if (dr == 0 && dc == 0)
  98. continue;
  99. end
  100. r2 = r + dr;
  101. c2 = c + dc;
  102.  
  103. if (aboveThreshold(r2, c2) == 1 && finalEdges(r2, c2) == 0)
  104. newPix(counter, 1) = r2;
  105. newPix(counter, 2) = c2;
  106. counter = counter + 1;
  107. finalEdges(r2, c2) = 1;
  108. end
  109. end
  110. end
  111. end
  112. end
  113.  
  114. currentPixels = newPix;
  115. end
  116.  
  117. newImg = finalEdges;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement