Safiron

Sobel

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