Represents an edge detector. More...
Public Member Functions | |
| EdgeDetector (EdgeDetectionType detectionType, double threshold) | |
| Create a new instance of an edge detector. | |
| double[] | Calculate (double[] samples) |
| Analyzes samples and returns new array with samples set to 1.0 where edges were detected. The absolute value of the input is normalized and binarized at the threshold. | |
Represents an edge detector.
Definition at line 34 of file EdgeDetector.cs.
| NewGamePhysics.Mathematics.EdgeDetector.EdgeDetector | ( | EdgeDetectionType | detectionType, | |
| double | threshold | |||
| ) |
Create a new instance of an edge detector.
| detectionType | The detection type (edge(s) to detect). | |
| threshold | The binarization threshold in the range 0.0 to 1.0 (against normalized input) |
Definition at line 61 of file EdgeDetector.cs.
| double [] NewGamePhysics.Mathematics.EdgeDetector.Calculate | ( | double[] | samples | ) |
Analyzes samples and returns new array with samples set to 1.0 where edges were detected. The absolute value of the input is normalized and binarized at the threshold.
| samples | The samples to analyze. |
Definition at line 77 of file EdgeDetector.cs.
00078 { 00079 int nMax = samples.Length; 00080 double[] result = new double[nMax]; 00081 00082 // Make absolute and determine maximum 00083 double maximum = Math.Abs(samples[0]); 00084 for (int i = 0; i < nMax; i++) 00085 { 00086 double value = Math.Abs(samples[i]); 00087 result[i] = value; 00088 if (value > maximum) 00089 { 00090 maximum = value; 00091 } 00092 } 00093 00094 // Binarize 00095 double margin = maximum * this.threshold; 00096 for (int i = 0; i < nMax; i++) 00097 { 00098 result[i] = (result[i] > margin) ? 1.0 : 0.0; 00099 } 00100 00101 // Edge detect 00102 double lastValue = 0.0; 00103 for (int i = 0; i < nMax; i++) 00104 { 00105 bool risingEdge = ((lastValue == 0.0) && (result[i] == 1.0)); 00106 bool fallingEdge = ((lastValue == 1.0) && (result[i] == 0.0)); 00107 lastValue = result[i]; 00108 switch (this.detectionType) 00109 { 00110 case EdgeDetectionType.Falling: 00111 result[i] = (fallingEdge) ? 1.0 : 0.0; 00112 break; 00113 case EdgeDetectionType.Rising: 00114 result[i] = (risingEdge) ? 1.0 : 0.0; 00115 break; 00116 case EdgeDetectionType.Both: 00117 result[i] = (risingEdge || fallingEdge) ? 1.0 : 0.0; 00118 break; 00119 } 00120 } 00121 00122 return result; 00123 }
1.6.2