コード例 #1
0
        public void AnyValuesMatchFor2x2AreaWithin6x4()
        {
            var data = DataRectangle.For(Rotate(new[, ]
            {
                { 1, 2, 3, 4, 5, 6 },
                { 7, 8, 9, 10, 11, 12 },
                { 13, 14, 15, 16, 17, 18 },
                { 19, 20, 21, 22, 23, 24 },
                { 25, 26, 27, 28, 29, 30 }
            }));
            var area = new Rectangle(new Point(1, 1), new Size(2, 1));

            Assert.True(
                data.AnyValuesMatch(area, value => value == 8)
                );
            Assert.True(
                data.AnyValuesMatch(area, value => value == 9)
                );
            Assert.False(
                data.AnyValuesMatch(area, value => value < 8)
                );
            Assert.False(
                data.AnyValuesMatch(area, value => value > 9)
                );
        }
コード例 #2
0
        public void Block5x5Into4x4()
        {
            var data = DataRectangle.For(Rotate(new[, ]
            {
                { 1, 1, 1, 1, 2 },
                { 1, 1, 1, 1, 2 },
                { 1, 1, 1, 1, 2 },
                { 1, 1, 1, 1, 2 },
                { 3, 3, 3, 3, 4 }
            }));
            var result = data.BlockOut(
                blockSize: 4,
                reducer: blockData =>
            {
                var valuesInBlock = blockData.Enumerate().Select(pointAndValue => pointAndValue.Item2);
                if (valuesInBlock.Distinct().Count() > 1)
                {
                    throw new Exception("All of the values in each block should be consistent based upon the test data");
                }
                return(valuesInBlock.First());
            }
                );
            var expected = DataRectangle.For(Rotate(new[, ]
            {
                { 1 }
            }));

            Assert.Equal(expected, result, DataRectangleTestsEqualityComparer <int> .Default);
        }
コード例 #3
0
        public void OneHundredAndThirtyFiveDegreeGradient()
        {
            // If straight up is zero degrees then getting lighter as you go UP and right would be 45 degrees. Getting lighter as you go DOWN and right
            // is 135 degrees.
            var oneHundredAndThirtyFiveDegreeGradient = DataRectangle
                                                        .For(new double[64, 60])
                                                        .Transform((value, point) => (double)(point.X + point.Y));

            // 135 will be split across the 130 and 150 bins with most of it going in the 150 bin. In fact, 3x should go in the 130 bin since it the
            // angle is (135 - 130) = 5 degrees away from 135 vs (150 - 135) = 15 degrees away from 150.
            var hogs = HistogramOfGradientGenerator.Get(oneHundredAndThirtyFiveDegreeGradient, blockSize: 8);

            Assert.True(
                hogs.Enumerate().Select(pointAndValue => pointAndValue.Item2).All(hog =>
                                                                                  (hog.Degrees10 == 0) &&
                                                                                  (hog.Degrees30 == 0) &&
                                                                                  (hog.Degrees50 == 0) &&
                                                                                  (hog.Degrees70 == 0) &&
                                                                                  (hog.Degrees90 == 0) &&
                                                                                  (hog.Degrees110 == 0) &&
                                                                                  (hog.Degrees130 > 0) &&
                                                                                  (hog.Degrees150.HasMinimalDifference(hog.Degrees130 / 3)) &&
                                                                                  (hog.Degrees170 == hog.Degrees10)
                                                                                  )
                );
        }
コード例 #4
0
        public void Block4x4Into4x4()
        {
            var data = DataRectangle.For(Rotate(new[, ]
            {
                { 1, 1, 2, 2 },
                { 1, 1, 2, 2 },
                { 3, 3, 4, 4 },
                { 3, 3, 4, 4 }
            }));
            var result = data.BlockOut(
                blockSize: 1,
                reducer: blockData =>
            {
                var valuesInBlock = blockData.Enumerate().Select(pointAndValue => pointAndValue.Item2);
                if (valuesInBlock.Count() != 1)
                {
                    throw new Exception("Expect precisely one value per block");
                }
                return(valuesInBlock.First());
            }
                );
            var expected = data;

            Assert.Equal(expected, result, DataRectangleTestsEqualityComparer <int> .Default);
        }
コード例 #5
0
        public void FortyFiveDegreeGradient()
        {
            // If straight up is zero degrees then getting lighter as you go UP and right is 45 degrees
            var oneHundredAndThirtyFiveDegreeGradient = DataRectangle
                                                        .For(new double[64, 60])
                                                        .Transform((value, point) => (double)(point.X + (60 - point.Y)));

            // 45 will be split across the 30 and 50 bins with most of it going in the 50 bin (3x as much since it's 5 degrees away from 50 and it's 15
            // from 45)
            var hogs = HistogramOfGradientGenerator.Get(oneHundredAndThirtyFiveDegreeGradient, blockSize: 8);

            Assert.True(
                hogs.Enumerate().Select(pointAndValue => pointAndValue.Item2).All(hog =>
                                                                                  (hog.Degrees10 == 0) &&
                                                                                  (hog.Degrees30 > 0) &&
                                                                                  (hog.Degrees50.HasMinimalDifference(hog.Degrees30 * 3)) &&
                                                                                  (hog.Degrees70 == 0) &&
                                                                                  (hog.Degrees90 == 0) &&
                                                                                  (hog.Degrees110 == 0) &&
                                                                                  (hog.Degrees130 == 0) &&
                                                                                  (hog.Degrees150 == 0) &&
                                                                                  (hog.Degrees170 == hog.Degrees10)
                                                                                  )
                );
        }
コード例 #6
0
        public void SampleSegmentFromTrainingData()
        {
            var exampleSegment = DataRectangle.For(DataRectangleTests.Rotate(new[, ]
            {
                { 38.996, 38.996, 38.996, 36.996, 34.997, 29.997, 28.997, 22.998 },
                { 39.996, 38.996, 35.996, 33.997, 29.997, 30.997, 24.998, 13.999 },
                { 39.996, 34.997, 30.997, 29.997, 26.997, 21.998, 12.999, 10.999 },
                { 34.997, 33.997, 26.997, 25.997, 26.997, 19.998, 10.999, 1.000 },
                { 34.997, 27.997, 25.997, 27.997, 16.998, 6.999, 0.000, 0.000 },
                { 31.997, 28.997, 28.997, 18.998, 7.999, 1.000, 1.000, 0.000 },
                { 30.997, 26.997, 22.998, 14.999, 1.000, 0.000, 0.000, 1.000 },
                { 26.997, 22.998, 15.998, 4.000, 3.000, 0.000, 0.000, 3.000 }
            }));
            var hogs = HistogramOfGradientGenerator.Get(exampleSegment, blockSize: 8);

            Assert.Equal(hogs.Width, 1);
            Assert.Equal(hogs.Height, 1);

            // I only want to test that this goes in down-right direction, the precise values aren't that important. Normalising, multiplying each value by ten and then by
            // rounding them means that small values will be ignored and the significant angles may be compared to pre-calculated values that were confirmed to match
            // expectations. This isn't the most precise test in the world, it's more of a finger-in-the-air test against some real data.
            var hog = hogs[0, 0].Normalise().Multiply(10);

            Assert.Equal(0, Math.Round(hog.Degrees10));
            Assert.Equal(0, Math.Round(hog.Degrees30));
            Assert.Equal(0, Math.Round(hog.Degrees50));
            Assert.Equal(0, Math.Round(hog.Degrees70));
            Assert.Equal(0, Math.Round(hog.Degrees90));
            Assert.Equal(2, Math.Round(hog.Degrees110));
            Assert.Equal(6, Math.Round(hog.Degrees130));
            Assert.Equal(2, Math.Round(hog.Degrees150));
            Assert.Equal(0, Math.Round(hog.Degrees170));
        }
コード例 #7
0
        public void Enumerate3x4WithFilter()
        {
            // Don't care about ordering here, only what items are returned - so the result will be ordered with LINQ and the expected array
            // will be defined in ascending order
            var data = DataRectangle.For(new[, ]
            {
                { 0, 1, 2 },
                { 3, 4, 5 },
                { 6, 7, 8 }
            });
            var result   = data.Enumerate((point, value) => value % 2 == 0).Select(pointAndValue => pointAndValue.Item2).OrderBy(value => value);
            var expected = new[] { 0, 2, 4, 6, 8 };

            Assert.Equal(expected, result);
        }
コード例 #8
0
        public void MayNotSliceNegativelyEvenWithNestedSlices()
        {
            var data = DataRectangle.For(Rotate(new[, ]
            {
                { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 }
            }));

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                data
                .Slice(new Rectangle(new Point(1, 1), new Size(3, 3)))
                .Slice(new Rectangle(new Point(-1, 1), new Size(2, 1)));
            });
        }
コード例 #9
0
        public void Slice4x3Into2x1With1x1Offset()
        {
            var data = DataRectangle.For(Rotate(new[, ]
            {
                { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 }
            }));
            var expected = DataRectangle.For(Rotate(new[, ]
            {
                { 6, 7 }
            }));
            var result = data.Slice(new Rectangle(new Point(1, 1), new Size(2, 1)));

            Assert.Equal(expected, result, DataRectangleTestsEqualityComparer <int> .Default);
        }
コード例 #10
0
        public void Transform3x3ByAddingOne()
        {
            var data = DataRectangle.For(new[, ]
            {
                { 0, 1, 2 },
                { 3, 4, 5 },
                { 6, 7, 8 }
            });
            var expected = DataRectangle.For(new[, ]
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
            });

            Assert.Equal(expected, data.Transform(value => value + 1), DataRectangleTestsEqualityComparer <int> .Default);
        }
コード例 #11
0
        public void CombineTwo2x2WithAddition()
        {
            var left = DataRectangle.For(new[, ]
            {
                { 1, 2 },
                { 3, 4 }
            });
            var right = DataRectangle.For(new[, ]
            {
                { 2, 3 },
                { 4, 5 }
            });
            var expected = DataRectangle.For(new[, ]
            {
                { 3, 5 },
                { 7, 9 }
            });

            Assert.Equal(expected, left.CombineWith(right, (x, y) => x + y), DataRectangleTestsEqualityComparer <int> .Default);
        }
コード例 #12
0
        public void Slice6x5Into3x3WithOffset1x1ThenInto2x1With1x1Offset()
        {
            var data = DataRectangle.For(Rotate(new[, ]
            {
                { 1, 2, 3, 4, 5, 6 },
                { 7, 8, 9, 10, 11, 12 },
                { 13, 14, 15, 16, 17, 18 },
                { 19, 20, 21, 22, 23, 24 },
                { 25, 26, 27, 28, 29, 30 }
            }));
            var expected = DataRectangle.For(Rotate(new[, ]
            {
                { 15, 16 }
            }));
            var result = data
                         .Slice(new Rectangle(new Point(1, 1), new Size(3, 3)))
                         .Slice(new Rectangle(new Point(1, 1), new Size(2, 1)));

            Assert.Equal(expected, result, DataRectangleTestsEqualityComparer <int> .Default);
        }
コード例 #13
0
        private static DataRectangle <double> MedianFilter(this DataRectangle <double> source, int blockSize)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (blockSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(blockSize));
            }

            var result = new double[source.Width, source.Height];
            var allLocationsInSource = Enumerable.Range(0, source.Width)
                                       .SelectMany(x => Enumerable.Range(0, source.Height).Select(y =>
                                                                                                  new Point(x, y)
                                                                                                  ));

            Parallel.ForEach(
                allLocationsInSource,
                location =>
            {
                var top          = Math.Max(0, location.Y - (blockSize / 2));
                var bottom       = Math.Min(source.Height, top + blockSize);
                var left         = Math.Max(0, location.X - (blockSize / 2));
                var right        = Math.Min(source.Width, left + blockSize);
                var blockWidth   = right - left;
                var blockHeight  = bottom - top;
                var valuesInArea = new List <double>(capacity: blockWidth * blockHeight);
                for (var xInner = left; xInner < right; xInner++)
                {
                    for (var yInner = top; yInner < bottom; yInner++)
                    {
                        valuesInArea.Add(source[xInner, yInner]);
                    }
                }
                valuesInArea.Sort();
                result[location.X, location.Y] = valuesInArea[valuesInArea.Count / 2];
            }
                );
            return(DataRectangle.For(result));
        }
コード例 #14
0
        public void MayNotCombineDifferentSizes()
        {
            var left = DataRectangle.For(new[, ]
            {
                { 1, 2 },
                { 3, 4 }
            });
            var right = DataRectangle.For(new[, ]
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
            });
            var expected = DataRectangle.For(new[, ]
            {
                { 3, 5 },
                { 7, 9 }
            });

            Assert.Throws <ArgumentException>(() => left.CombineWith(right, (x, y) => x + y));
        }
コード例 #15
0
        public void VerticalGradient()
        {
            var verticalGradient = DataRectangle
                                   .For(new double[64, 60])
                                   .Transform((value, point) => (double)point.Y);
            var hogs = HistogramOfGradientGenerator.Get(verticalGradient, blockSize: 8);

            Assert.True(
                hogs.Enumerate().Select(pointAndValue => pointAndValue.Item2).All(hog =>
                                                                                  (hog.Degrees10 > 0) &&
                                                                                  (hog.Degrees30 == 0) &&
                                                                                  (hog.Degrees50 == 0) &&
                                                                                  (hog.Degrees70 == 0) &&
                                                                                  (hog.Degrees90 == 0) &&
                                                                                  (hog.Degrees110 == 0) &&
                                                                                  (hog.Degrees130 == 0) &&
                                                                                  (hog.Degrees150 == 0) &&
                                                                                  (hog.Degrees170 == hog.Degrees10)
                                                                                  )
                );
        }