Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. void edgeDetection(){
  2. Mat src;
  3. char fname[MAX_PATH];
  4.  
  5. while (openFileDlg(fname)){
  6. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  7.  
  8. Mat gauss = gaussian2D(src, 5, 0.8);
  9.  
  10. Mat gradient = Mat(gauss.rows, gauss.cols, CV_32FC1);
  11. Mat fi = Mat(gauss.rows, gauss.cols, CV_32FC1);
  12. Mat gradientShow = Mat(gauss.rows, gauss.cols, CV_8UC1);
  13. Mat nonMaxima = Mat(gauss.rows, gauss.cols, CV_32FC1);
  14.  
  15. float gx, gy, G, FI;
  16. int area;
  17.  
  18. for (int i = 1; i < gauss.rows - 1; i++)
  19. for (int j = 1; j < gauss.cols - 1; j++){
  20. gx = 0;
  21. gy = 0;
  22.  
  23. gx += -gauss.at<uchar>(i - 1, j - 1);
  24. gx += 0;
  25. gx += gauss.at<uchar>(i - 1, j + 1);
  26. gx += - 2 * gauss.at<uchar>(i, j - 1);
  27. gx += 0;
  28. gx += 2 * gauss.at<uchar>(i, j + 1);
  29. gx += -gauss.at<uchar>(i + 1, j - 1);
  30. gx += 0;
  31. gx += gauss.at<uchar>(i + 1, j + 1);
  32.  
  33. gy += gauss.at<uchar>(i - 1, j - 1);
  34. gy += 2 * gauss.at<uchar>(i - 1, j);
  35. gy += gauss.at<uchar>(i - 1, j + 1);
  36. gy += 0;
  37. gy += 0;
  38. gy += 0;
  39. gy += -gauss.at<uchar>(i + 1, j - 1);
  40. gy += -2*gauss.at<uchar>(i + 1, j);
  41. gy += -gauss.at<uchar>(i + 1, j + 1);
  42.  
  43. G = sqrt(gx*gx + gy*gy);
  44. G /= (4 * sqrt(2));
  45. FI = atan2(gy, gx);
  46.  
  47.  
  48. gradient.at<float>(i, j) = G;
  49. fi.at<float>(i, j) = FI;
  50. gradientShow.at<uchar>(i, j) = (uchar)G;
  51. }
  52. nonMaxima = gradient.clone();
  53. for (int i = 1; i < fi.rows - 1; i++)
  54. for (int j = 1; j < fi.cols - 1; j++){
  55. area = circleZone(fi.at<float>(i, j));
  56. switch(area){
  57. case 0:
  58. if (gradient.at<float>(i, j - 1) > gradient.at<float>(i, j) || gradient.at<float>(i, j + 1) > gradient.at<float>(i, j))
  59. nonMaxima.at<float>(i, j) = 0;
  60. break;
  61. case 1:
  62. if (gradient.at<float>(i - 1, j + 1) > gradient.at<float>(i, j) || gradient.at<float>(i + 1, j - 1) > gradient.at<float>(i, j))
  63. nonMaxima.at<float>(i, j) = 0;
  64. break;
  65. case 2:
  66. if (gradient.at<float>(i - 1, j) > gradient.at<float>(i, j) || gradient.at<float>(i + 1, j) > gradient.at<float>(i, j))
  67. nonMaxima.at<float>(i, j) = 0;
  68. break;
  69. case 3:
  70. if (gradient.at<float>(i - 1, j - 1) > gradient.at<float>(i, j) || gradient.at<float>(i + 1, j + 1) > gradient.at<float>(i, j))
  71. nonMaxima.at<float>(i, j) = 0;
  72. break;
  73. default:
  74. break;
  75. }
  76.  
  77.  
  78. }
  79.  
  80. Mat nonMaximaShow = Mat(nonMaxima.rows, nonMaxima.cols, CV_8UC1, Scalar(0));
  81. for (int i = 1; i < fi.rows - 1; i++)
  82. for (int j = 1; j < fi.cols - 1; j++){
  83. nonMaximaShow.at<uchar>(i, j) = (uchar)nonMaxima.at<float>(i, j);
  84. }
  85.  
  86. int noEdgePixels = 0;
  87.  
  88. for (int i = 1; i < nonMaximaShow.rows - 1; i++)
  89. for (int j = 1; j < nonMaximaShow.cols - 1; j++){
  90. if (nonMaximaShow.at<uchar>(i, j) > 0)
  91. noEdgePixels++;
  92. }
  93. noEdgePixels *= 0.1;
  94. int* histogram = generate_histogram(nonMaximaShow);
  95. int noEdgePixelsSum = 0;
  96.  
  97. int tHigh = 255;
  98. for (tHigh = 255; noEdgePixelsSum < noEdgePixels; noEdgePixelsSum += histogram[tHigh], tHigh--);
  99. int tLow = 0.4 * tHigh;
  100.  
  101. for (int i = 1; i < nonMaximaShow.rows - 1; i++)
  102. for (int j = 1; j < nonMaximaShow.cols - 1; j++){
  103. if (nonMaximaShow.at<uchar>(i, j) >= tHigh)
  104. nonMaximaShow.at<uchar>(i, j) = 255;
  105. else if (nonMaximaShow.at<uchar>(i, j) <= tHigh && nonMaximaShow.at<uchar>(i, j) >= tLow)
  106. nonMaximaShow.at<uchar>(i, j) = 127;
  107. else
  108. nonMaximaShow.at<uchar>(i, j) = 0;
  109. }
  110.  
  111. //Edge linking
  112. int offi[9] = { -1, -1, -1, 0, 0, 0, 1, 1, 1 };
  113. int offj[9] = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
  114.  
  115. for (int i = 1; i < nonMaximaShow.rows - 1; i++){
  116. for (int j = 1; j < nonMaximaShow.cols - 1; j++){
  117. if (nonMaximaShow.at<uchar>(i, j) == 255){
  118. queue<Point2i> Q;
  119. Q.push(Point2i(j, i));
  120.  
  121. while (!Q.empty()){
  122. Point2i q = Q.front();
  123. Q.pop();
  124. for (int k = 0; k < 9; k++){
  125. if (nonMaximaShow.at<uchar>(q.y + offi[k], q.x + offj[k]) == 255){
  126. labels.at<int>(q.y + offi[k], q.x + offj[k]) = label;
  127. Q.push(Point2i(q.x + offj[k], q.y + offi[k]));
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134.  
  135. imshow("Step2", nonMaximaShow);
  136. waitKey();
  137.  
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement