Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. // B1. Làm trơn Gaussian
  2. GaussianBlur(sourceImage, sourceImage, Size(5, 5), 0, 0);
  3.  
  4. // B2. Tính Edge Strength và Edge Direction bằng cách lọc Sobel theo x và y
  5. // Edge Strength = Gx + Gy; Edge Direction = artan(Gy/Gx);
  6. EdgeDetector ed;
  7. Mat EdgeStrength;
  8.  
  9. float H_Sob[9] = { 1, 0, -1, 2, 0, -2, 1, 0, -1 };
  10. float V_Sob[9] = { -1, -2, -1, 0, 0, 0, 1, 2, 1 };
  11. vector<float> H_kernel;
  12. vector<float> V_kernel;
  13. float t;
  14.  
  15. H_kernel.assign(H_Sob, H_Sob + 9);
  16. V_kernel.assign(V_Sob, V_Sob + 9);
  17. t = 1.0f / 4;
  18. Mat horizon; //Wx
  19. Mat vertical; //Wy
  20.  
  21. // tích chập chiều ngang
  22. _Convo.SetKernel(H_kernel, 3, 3);
  23.  
  24. _Convo.DoConvolution(sourceImage, horizon);
  25.  
  26. //Tích chập chiều dọc
  27. _Convo.SetKernel(V_kernel, 3, 3);
  28. _Convo.DoConvolution(sourceImage, vertical);
  29.  
  30. // Edge Strength
  31. EdgeStrength = abs(horizon) + abs(vertical);
  32.  
  33.  
  34. //Edge Direction
  35. Mat flagPixelDel = Mat::zeros(EdgeStrength.size(), CV_8U);
  36.  
  37. // Đi lần lượt từng pixel của ảnh nguồn, bỏ pixel ở rìa, và áp dụng kĩ thuật nonmaximum suppression:
  38. for (int i = 0; i < EdgeStrength.rows; i ++)
  39. {
  40.  
  41. for (int j = 0; j < EdgeStrength.cols; j ++)
  42. {
  43. // Nếu pixel ở rìa ảnh thì xét không phải biên cạnh.
  44. if (i == 0 || j == 0 || i == EdgeStrength.rows - 1 || j == EdgeStrength.cols - 1)
  45. flagPixelDel.at<uchar>(i, j) = 255;
  46. else
  47. {
  48. float res = (atan2(vertical.at<short>(i, j), horizon.at<short>(i, j)) * 180) / PI;
  49. // Xét pixel kề theo hướng cạnh. Nếu không phải cực đại cục bộ thì loại, không phải là biên cạnh.
  50. if (res <= 22.5 || res > 157.5)
  51. {
  52. if (EdgeStrength.at<short>(i + 1, j) > EdgeStrength.at<short>(i, j) || EdgeStrength.at<short>(i - 1, j) > EdgeStrength.at<short>(i, j))
  53. flagPixelDel.at<uchar>(i, j) = 255;
  54.  
  55. }
  56. else
  57. if (res > 22.5 && res <= 67.5)
  58. {
  59. if (EdgeStrength.at<short>(i + 1, j + 1) > EdgeStrength.at<short>(i, j) || EdgeStrength.at<short>(i - 1, j - 1) > EdgeStrength.at<short>(i, j))
  60.  
  61. flagPixelDel.at<uchar>(i, j) = 255;
  62. }
  63. else
  64. if (res > 67.5 && res <= 112.5)
  65. {
  66. if (EdgeStrength.at<short>(i, j + 1) > EdgeStrength.at<short>(i, j) || EdgeStrength.at<short>(i, j - 1) > EdgeStrength.at<short>(i, j))
  67.  
  68. flagPixelDel.at<uchar>(i, j) = 255;
  69.  
  70. }
  71. else
  72. {
  73. if (EdgeStrength.at<short>(i + 1, j - 1) > EdgeStrength.at<short>(i, j) || EdgeStrength.at<short>(i - 1, j + 1) > EdgeStrength.at<short>(i, j))
  74. flagPixelDel.at<uchar>(i, j) = 255;
  75. }
  76. }
  77. }
  78. }
  79.  
  80.  
  81. horizon.release();
  82. vertical.release();
  83.  
  84. //Áp dụng kĩ thuật hysteresis để loại pixel thừa.
  85.  
  86. double min, max;
  87. minMaxLoc(EdgeStrength, &min, &max);
  88. // Ngưỡng cho Canny.
  89. double T0 = min + 80;
  90. double T1 = min + 100;
  91.  
  92.  
  93. int neighborI[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  94. int neighborJ[8] = { -1, 0, 1, 1, 1, 0, -1, -1};
  95. destinationImage.release();
  96. destinationImage = Mat::zeros(EdgeStrength.size(), CV_8U) ;
  97. // Nếu giá trị độ lớn đạo hàm lớn hơn T1 thì kết nạp là hướng cạnh.
  98. for (int i = 0; i < EdgeStrength.rows; i ++)
  99. for (int j = 0; j < EdgeStrength.cols; j ++)
  100. {
  101. if (flagPixelDel.at<uchar>(i, j) == 0)
  102. if (EdgeStrength.at<short>(i, j) >= T1)
  103. destinationImage.at<uchar>(i, j) = 255;
  104. }
  105.  
  106. cout << "jehe" << endl;
  107.  
  108. Mat flagTemp = Mat::zeros(EdgeStrength.size(), CV_8U);
  109. // Nếu giá trị độ lớn đạo hàm những pixel thoả mãn điều kiện cực đại cục bộ, nhưng không lớn hơn T1, mà lớn hơn T2 và pixel kề là biên cạnh thì kết nạp.
  110. for (int i = 0; i < EdgeStrength.rows; i ++)
  111. for (int j = 0; j < EdgeStrength.cols; j ++)
  112. {
  113. if (flagPixelDel.at<uchar>(i, j) == 0)
  114. if (destinationImage.at<uchar>(i, j) == 0)
  115. if (EdgeStrength.at<short>(i, j) >= T0)
  116. {
  117. // Nếu pixel ở rìa ảnh thì không xét.
  118. if (i == 0 || j == 0 || i == EdgeStrength.rows - 1 || j == EdgeStrength.cols - 1)
  119. continue;
  120. else
  121. {
  122. for (int ij = 0; ij < 8; ij ++)
  123. if (destinationImage.at<uchar>(i + neighborI[ij], j + neighborJ[ij]) == 255)
  124. {
  125. flagTemp.at<uchar>(i, j) = 255;
  126. break;
  127. }
  128.  
  129. }
  130. }
  131. }
  132. destinationImage = destinationImage + flagTemp;
  133.  
  134. flagPixelDel.release();
  135. flagTemp.release();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement