Safiron

Edge Det

Mar 8th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.48 KB | None | 0 0
  1. #include "EdgeDetection.h"
  2.  
  3.  
  4. EdgeDetection::EdgeDetection()
  5. {
  6.     original = imread("C:\\circle.jpg", CV_LOAD_IMAGE_GRAYSCALE);
  7.     original.convertTo(original, CV_32FC1, 1.0 / 255.0);
  8.     output = Mat::zeros(original.rows, original.cols, CV_32FC1);
  9.     outputColor = Mat::zeros(original.rows, original.cols, CV_32FC3);
  10.     DeclareSobelOperators();
  11.     blur(original, original, Size(5,5), Point(-1,-1), BORDER_DEFAULT );
  12.     ApplySobel(&original, &output);
  13.     //imshow("ORIGINAL", original);
  14.     imshow("EDGES", outputColor);
  15. }
  16.  
  17. void EdgeDetection::DeclareSobelOperators()
  18. {
  19.     sobelY = Mat(3, 3, CV_32F);
  20.     sobelY.at<float>(0, 0) = -1.0;
  21.     sobelY.at<float>(0, 1) = 0.0;
  22.     sobelY.at<float>(0, 2) = 1.0;
  23.     sobelY.at<float>(1, 0) = -2.0;
  24.     sobelY.at<float>(1, 1) = 0.0;
  25.     sobelY.at<float>(1, 2) = 2.0;
  26.     sobelY.at<float>(2, 0) = -1.0;
  27.     sobelY.at<float>(2, 1) = 0.0;
  28.     sobelY.at<float>(2, 2) = 1.0;
  29.  
  30.  
  31.     sobelX = Mat(3, 3, CV_32F);
  32.     sobelX.at<float>(0, 0) = -1.0;
  33.     sobelX.at<float>(0, 1) = -2.0;
  34.     sobelX.at<float>(0, 2) = -1.0;
  35.     sobelX.at<float>(1, 0) = 0.0;
  36.     sobelX.at<float>(1, 1) = 0.0;
  37.     sobelX.at<float>(1, 2) = 0.0;
  38.     sobelX.at<float>(2, 0) = 1.0;
  39.     sobelX.at<float>(2, 1) = 2.0;
  40.     sobelX.at<float>(2, 2) = 1.0;
  41.  
  42. }
  43.  
  44. float EdgeDetection::CountEdge(float gradY, float gradX)
  45. {
  46.     return sqrt(sqr(gradY) + sqr(gradX));
  47. }
  48.  
  49. float EdgeDetection::CountOrientantion(float gradY, float gradX)
  50. {
  51.  
  52.     return atan2(gradY,gradX);;
  53. }
  54.  
  55. void EdgeDetection::DrawTangent(int y, int x, float orientation, Mat *image)
  56. {
  57.     const int length = 150;
  58.     arrowedLine(*image, Point(x, y), Point(x + length * cos(orientation), y - length * sin(orientation)), Scalar(255, 255, 0), 1, 8, 0, 0.1);
  59. }
  60.  
  61. void EdgeDetection::DrawPerpendicular(int y, int x, float orientation, Mat *image)
  62. {
  63.     const int length = 50;
  64.     arrowedLine(*image, Point(x, y), Point(x + length * sin(orientation), y + length * cos(orientation)), Scalar(0, 255, 255), 1, 8, 0, 0.1);
  65. }
  66.  
  67. Vec3f EdgeDetection::ColorEdges(float orientation)
  68. {
  69.     Vec3f temp;
  70.     if ((orientation < CV_PI && orientation > 2.9) || (orientation < -2.9 && orientation > -CV_PI))
  71.     {
  72.         temp = Vec3f(0, 0, 1);
  73.     }
  74.     else if (orientation <= 2.9 && orientation > 2.4)
  75.     {
  76.         temp = Vec3f(0, 0.5, 1);
  77.     }
  78.     else if (orientation <= 2.4 && orientation > 1.9)
  79.     {
  80.         temp = Vec3f(0, 1, 1);
  81.     }
  82.     else if (orientation <= 1.9 && orientation > 1.4)
  83.     {
  84.         temp = Vec3f(0, 1, 0.5);
  85.     }
  86.     else if (orientation <= 1.4 && orientation > 0.9)
  87.     {
  88.         temp = Vec3f(0, 1, 0);
  89.     }
  90.     else if (orientation <= 0.9 && orientation > 0.4)
  91.     {
  92.         temp = Vec3f(0.5, 1, 0);
  93.     }
  94.     else if ((orientation <= 0.4 && orientation > 0.0) || (orientation <= 0.0 && orientation > -0.2))
  95.     {
  96.         temp = Vec3f(1, 1, 0);
  97.     }
  98.     else if (orientation <= -0.2 && orientation > -0.8)
  99.     {
  100.         temp = Vec3f(1, 0.5, 0);
  101.     }
  102.     else if (orientation <= -0.8 && orientation > -1.3)
  103.     {
  104.         temp = Vec3f(1, 0, 0);
  105.     }
  106.     else if (orientation <= -1.3 && orientation > -1.8)
  107.     {
  108.         temp = Vec3f(1, 0, 0.5);
  109.     }
  110.     else if (orientation <= -1.8 && orientation > -2.3)
  111.     {
  112.         temp = Vec3f(1, 0, 1);
  113.     }
  114.     else if (orientation <= -2.3  && orientation > - 2.9)
  115.     {
  116.         temp = Vec3f(0.5, 0, 1);
  117.     }
  118.  
  119.     return temp;
  120. }
  121.  
  122. void EdgeDetection::ApplySobel(Mat *src, Mat *edges)
  123. {
  124.     float pixValue = 0.0;
  125.     float orientation = 0.0;
  126.     edgesX = Mat::zeros(original.rows, original.cols, CV_32FC1);
  127.     edgesY = Mat::zeros(original.rows, original.cols, CV_32FC1);
  128.  
  129.     for (int y = 1; y < src->rows - 1; y++)
  130.     {
  131.         for (int x = 1; x < src->cols - 1; x++)
  132.         {
  133.             pixValue += sobelY.at<float>(0, 0) * src->at<float>(y - 1, x - 1);
  134.             pixValue += sobelY.at<float>(0, 2) * src->at<float>(y - 1, x + 1);
  135.             pixValue += sobelY.at<float>(1, 0) * src->at<float>(y, x - 1);
  136.             pixValue += sobelY.at<float>(1, 2) * src->at<float>(y, x + 1);
  137.             pixValue += sobelY.at<float>(2, 0) * src->at<float>(y + 1, x - 1);
  138.             pixValue += sobelY.at<float>(2, 2) * src->at<float>(y + 1, x + 1);
  139.             edgesY.at<float>(y, x) = pixValue;
  140.             pixValue = 0;
  141.  
  142.             pixValue += sobelX.at<float>(0, 0) * src->at<float>(y - 1, x - 1);
  143.             pixValue += sobelX.at<float>(0, 1) * src->at<float>(y - 1, x);
  144.             pixValue += sobelX.at<float>(0, 2) * src->at<float>(y - 1, x + 1);
  145.             pixValue += sobelX.at<float>(2, 0) * src->at<float>(y + 1, x - 1);
  146.             pixValue += sobelX.at<float>(2, 1) * src->at<float>(y + 1, x);
  147.             pixValue += sobelX.at<float>(2, 2) * src->at<float>(y + 1, x + 1);
  148.             edgesX.at<float>(y, x) = pixValue;
  149.             pixValue = 0;
  150.  
  151.             float edgeIntensity = CountEdge(edgesY.at<float>(y, x), edgesX.at<float>(y, x));
  152.             edges->at<float>(y, x) = edgeIntensity;
  153.             if (edgeIntensity > 0.2)
  154.             {
  155.                 orientation = CountOrientantion(edgesY.at<float>(y, x), edgesX.at<float>(y, x));
  156.                 outputColor.at<Vec3f>(y, x) = ColorEdges(orientation);
  157.                 if ((x % 20 == 0) && (y % 20 == 0))
  158.                 {
  159.                     EdgeDetection::DrawTangent(y, x, orientation, &outputColor);
  160.                     EdgeDetection::DrawPerpendicular(y, x, orientation, &outputColor);
  161.                 }
  162.             }
  163.            
  164.         }
  165.     }
  166.  
  167. }
Add Comment
Please, Sign In to add comment