Advertisement
Guest User

Fixed Edge Detection

a guest
Mar 30th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. public class EdgeDetectionFilter : IProcessingFourierOperation
  2.     {
  3.         private const double NOISE = 0.0001;
  4.         private const double EPISLON = 0.0000001;
  5.  
  6.         public double FilterAngle { get; set; }
  7.         public double FilterAngleOffset { get; set; }
  8.         public int FilterRadius { get; set; }
  9.  
  10.         private double _firstLineAngle;
  11.         private double _secondLineAngle;
  12.  
  13.         public EdgeDetectionFilter(double filterAngle, double filterAngleOffset, int filterRadius)
  14.         {
  15.             filterAngleOffset %= 360;
  16.             filterAngleOffset = filterAngleOffset < 0 ? 360 + filterAngleOffset : filterAngleOffset;
  17.             filterAngle /= 2;
  18.  
  19.             FilterAngle = (filterAngle + NOISE) * 2 * Math.PI / 360;
  20.             FilterAngleOffset = (filterAngleOffset + NOISE) * 2 * Math.PI / 360;
  21.             FilterRadius = filterRadius;
  22.  
  23.             _firstLineAngle = FilterAngle + FilterAngleOffset;
  24.             _secondLineAngle = -FilterAngle + FilterAngleOffset;
  25.         }
  26.  
  27.         public void ProcessImage(Complex[][] complexData)
  28.         {
  29.             double imageHeight = complexData.Length;
  30.             double imageWidth = complexData[0].Length;
  31.  
  32.             double halfImageHeight = imageHeight / 2;
  33.             double halfImageWidth = imageWidth / 2;
  34.  
  35.             for (int i = 0; i < imageHeight; i++)
  36.             {
  37.                 for (int j = 0; j < imageWidth; j++)
  38.                 {
  39.                     if (i == halfImageHeight && j == halfImageWidth)
  40.                     {
  41.                         continue;
  42.                     }
  43.  
  44.                     var widthFactor = Math.Pow((halfImageWidth - j) / FilterRadius, 2);
  45.                     var heightFactor = Math.Pow((halfImageHeight - i) / FilterRadius, 2);
  46.  
  47.                     if (Math.Sqrt(widthFactor + heightFactor) < 1)
  48.                     {
  49.                         complexData[i][j] = 0;
  50.                         continue;
  51.                     }
  52.  
  53.                     var firstLineValue = Math.Tan(_firstLineAngle) * (j - halfImageWidth);
  54.                     var secondLineValue = Math.Tan(_secondLineAngle) * (j - halfImageWidth);
  55.  
  56.                     bool firstHalfStartAssert;
  57.                     bool firstHalfEndAssert;
  58.                     bool secondHalfStartAssert;
  59.                     bool secondHalfEndAssert;
  60.  
  61.                     var currentValue = i - halfImageHeight;
  62.  
  63.                     if ((_firstLineAngle - Math.PI / 2 > EPISLON && _secondLineAngle - Math.PI / 2 < EPISLON) ||
  64.                         (_firstLineAngle - 1.5 * Math.PI > EPISLON && _secondLineAngle - 1.5 * Math.PI < EPISLON))
  65.                     {
  66.                         firstHalfStartAssert = firstLineValue > currentValue;
  67.                         firstHalfEndAssert = secondLineValue > currentValue;
  68.                         secondHalfStartAssert = secondLineValue < currentValue;
  69.                         secondHalfEndAssert = firstLineValue < currentValue;
  70.                     }
  71.                     else if (_firstLineAngle - Math.PI > EPISLON && _firstLineAngle - 1.5 * Math.PI < EPISLON)
  72.                     {
  73.                         firstHalfStartAssert = firstLineValue > currentValue;
  74.                         firstHalfEndAssert = secondLineValue < currentValue;
  75.                         secondHalfStartAssert = firstLineValue < currentValue;
  76.                         secondHalfEndAssert = secondLineValue > currentValue;
  77.                     }
  78.                     else
  79.                     {
  80.                         firstHalfStartAssert = firstLineValue < currentValue;
  81.                         firstHalfEndAssert = secondLineValue > currentValue;
  82.                         secondHalfStartAssert = firstLineValue > currentValue;
  83.                         secondHalfEndAssert = secondLineValue < currentValue;
  84.                     }
  85.  
  86.                     if (!((firstHalfStartAssert && firstHalfEndAssert) || (secondHalfStartAssert && secondHalfEndAssert)))
  87.                     {
  88.                         complexData[i][j] = 0;
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement