| | 1 | | using PointCloud.Benchmark.Common; |
| | 2 | |
|
| | 3 | | namespace PointCloud.Benchmark.Filter; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// A two-dimensional box-blur with separated kernels |
| | 7 | | /// </summary> |
| | 8 | | public class FastBoxFilter |
| | 9 | | { |
| | 10 | | #region fields |
| | 11 | |
|
| | 12 | | private int _radius; |
| | 13 | |
|
| | 14 | | #endregion |
| | 15 | |
|
| | 16 | | #region properties |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Gets or sets the radius. |
| | 20 | | /// </summary> |
| | 21 | | /// <value> |
| | 22 | | /// The radius. |
| | 23 | | /// </value> |
| | 24 | | public int Radius |
| | 25 | | { |
| 0 | 26 | | get => _radius; |
| 0 | 27 | | set => SetRadius(value); |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | public float DefaultValue { get; set; } |
| | 31 | |
|
| | 32 | | #endregion |
| | 33 | |
|
| | 34 | | #region constructor |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Initializes a new instance of the <see cref="BoxFilter"/> class. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="radius">The radius.</param> |
| 0 | 40 | | public FastBoxFilter(int radius) => SetRadius(radius); |
| | 41 | |
|
| | 42 | | #endregion |
| | 43 | |
|
| | 44 | | #region methods |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Filters highly frequented changes in a field of <see cref="ReFlex.Core.Common.Components.Point3"/>. |
| | 48 | | /// </summary> |
| | 49 | | public void Filter(PointCloud3 target) |
| 0 | 50 | | { |
| 0 | 51 | | var targetRef = target.AsJaggedArray(); |
| 0 | 52 | | var width = target.SizeX; |
| 0 | 53 | | var height = target.SizeY; |
| | 54 | |
|
| 0 | 55 | | if (_radius % 2 == 0) |
| 0 | 56 | | _radius++; |
| | 57 | |
|
| 0 | 58 | | var avg = 1f / _radius; |
| | 59 | |
|
| 0 | 60 | | for (var j = 0; j < height; j++) |
| 0 | 61 | | { |
| 0 | 62 | | var hSum = 0f; |
| | 63 | |
|
| 0 | 64 | | for (var x = 0; x < _radius; x++) |
| 0 | 65 | | { |
| 0 | 66 | | hSum += targetRef[x][j].Z; |
| 0 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | var iAvg = hSum * avg; |
| | 70 | |
|
| 0 | 71 | | for (var i = 0; i < width; i++) |
| 0 | 72 | | { |
| 0 | 73 | | if (i - _radius / 2 >= 0 && i + 1 + _radius / 2 < width) |
| 0 | 74 | | { |
| 0 | 75 | | hSum -= targetRef[i - _radius / 2][j].Z; |
| | 76 | |
|
| 0 | 77 | | var tmp = targetRef[i + 1 + _radius / 2][j].Z; |
| 0 | 78 | | hSum += tmp; |
| | 79 | |
|
| 0 | 80 | | iAvg = hSum * avg; |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | targetRef[i][j].Z = iAvg; |
| 0 | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | // need copy ? |
| | 88 | | // Bitmap total = Hblur.Clone(); |
| | 89 | |
|
| 0 | 90 | | for (var i = 0; i < width; i++) |
| 0 | 91 | | { |
| 0 | 92 | | var tSum = 0f; |
| 0 | 93 | | for (var y = 0; y < _radius; y++) |
| 0 | 94 | | { |
| 0 | 95 | | var tmpColor = targetRef[i][y].Z; |
| 0 | 96 | | tSum += tmpColor; |
| 0 | 97 | | } |
| | 98 | |
|
| 0 | 99 | | var iAvg = tSum * avg; |
| | 100 | |
|
| 0 | 101 | | for (var j = 0; j < height; j++) |
| 0 | 102 | | { |
| 0 | 103 | | if (j - _radius / 2 >= 0 && j + 1 + _radius / 2 < height) |
| 0 | 104 | | { |
| 0 | 105 | | var tmpPColor = targetRef[i][j - _radius / 2].Z; |
| 0 | 106 | | tSum -= tmpPColor; |
| | 107 | |
|
| 0 | 108 | | var tmpNColor = targetRef[i][j + 1 + _radius / 2].Z; |
| 0 | 109 | | tSum += tmpNColor; |
| | 110 | | // |
| 0 | 111 | | iAvg = tSum * avg; |
| 0 | 112 | | } |
| | 113 | |
|
| 0 | 114 | | targetRef[i][j].Z = iAvg; |
| 0 | 115 | | } |
| 0 | 116 | | } |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Sets the radius and calculates the diameter. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="radius">The radius.</param> |
| | 123 | | private void SetRadius(int radius) |
| 0 | 124 | | { |
| 0 | 125 | | _radius = radius; |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | #endregion |
| | 129 | | } |