Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EdgeDetectionFilter : IProcessingFourierOperation
- {
- private const double NOISE = 0.0001;
- private const double EPISLON = 0.0000001;
- public double FilterAngle { get; set; }
- public double FilterAngleOffset { get; set; }
- public int FilterRadius { get; set; }
- private double _firstLineAngle;
- private double _secondLineAngle;
- public EdgeDetectionFilter(double filterAngle, double filterAngleOffset, int filterRadius)
- {
- filterAngleOffset %= 360;
- filterAngleOffset = filterAngleOffset < 0 ? 360 + filterAngleOffset : filterAngleOffset;
- filterAngle /= 2;
- FilterAngle = (filterAngle + NOISE) * 2 * Math.PI / 360;
- FilterAngleOffset = (filterAngleOffset + NOISE) * 2 * Math.PI / 360;
- FilterRadius = filterRadius;
- _firstLineAngle = FilterAngle + FilterAngleOffset;
- _secondLineAngle = -FilterAngle + FilterAngleOffset;
- }
- public void ProcessImage(Complex[][] complexData)
- {
- double imageHeight = complexData.Length;
- double imageWidth = complexData[0].Length;
- double halfImageHeight = imageHeight / 2;
- double halfImageWidth = imageWidth / 2;
- for (int i = 0; i < imageHeight; i++)
- {
- for (int j = 0; j < imageWidth; j++)
- {
- if (i == halfImageHeight && j == halfImageWidth)
- {
- continue;
- }
- var widthFactor = Math.Pow((halfImageWidth - j) / FilterRadius, 2);
- var heightFactor = Math.Pow((halfImageHeight - i) / FilterRadius, 2);
- if (Math.Sqrt(widthFactor + heightFactor) < 1)
- {
- complexData[i][j] = 0;
- continue;
- }
- var firstLineValue = Math.Tan(_firstLineAngle) * (j - halfImageWidth);
- var secondLineValue = Math.Tan(_secondLineAngle) * (j - halfImageWidth);
- bool firstHalfStartAssert;
- bool firstHalfEndAssert;
- bool secondHalfStartAssert;
- bool secondHalfEndAssert;
- var currentValue = i - halfImageHeight;
- if ((_firstLineAngle - Math.PI / 2 > EPISLON && _secondLineAngle - Math.PI / 2 < EPISLON) ||
- (_firstLineAngle - 1.5 * Math.PI > EPISLON && _secondLineAngle - 1.5 * Math.PI < EPISLON))
- {
- firstHalfStartAssert = firstLineValue > currentValue;
- firstHalfEndAssert = secondLineValue > currentValue;
- secondHalfStartAssert = secondLineValue < currentValue;
- secondHalfEndAssert = firstLineValue < currentValue;
- }
- else if (_firstLineAngle - Math.PI > EPISLON && _firstLineAngle - 1.5 * Math.PI < EPISLON)
- {
- firstHalfStartAssert = firstLineValue > currentValue;
- firstHalfEndAssert = secondLineValue < currentValue;
- secondHalfStartAssert = firstLineValue < currentValue;
- secondHalfEndAssert = secondLineValue > currentValue;
- }
- else
- {
- firstHalfStartAssert = firstLineValue < currentValue;
- firstHalfEndAssert = secondLineValue > currentValue;
- secondHalfStartAssert = firstLineValue > currentValue;
- secondHalfEndAssert = secondLineValue < currentValue;
- }
- if (!((firstHalfStartAssert && firstHalfEndAssert) || (secondHalfStartAssert && secondHalfEndAssert)))
- {
- complexData[i][j] = 0;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement