public void DoSegment(bool[,] image, out int[,] rows, out int[,] cols)
        {
            //Count the number of dark pixels per row and column
            uint[] colDarkPixelCounts = SegmentationAlgorithmHelpers.CountNumDarkPixelsPerCol(image);
            uint[] rowDarkPixelCounts = SegmentationAlgorithmHelpers.CountNumDarkPixelsPerRow(image);

            //Calculate the Histograms to select a threshold
            BimodalHistogram colHist = new BimodalHistogram(colDarkPixelCounts.ToDoubleArr());
            BimodalHistogram rowHist = new BimodalHistogram(rowDarkPixelCounts.ToDoubleArr());

            //Find the percentile rank corresponding to the threshold selected from the Histograms
            Percentile colPercentile = new Percentile(colDarkPixelCounts.ToDoubleArr());
            Percentile rowPercentile = new Percentile(rowDarkPixelCounts.ToDoubleArr());
            double colThresholdRank = colPercentile.CalculateRank(colHist.ThresholdValue);
            double rowThresholdRank = rowPercentile.CalculateRank(rowHist.ThresholdValue);

            //Look up the percentile values for the ranks x either side of the auto-selected ones
            double colStartRank = Math.Min(100, colThresholdRank + PERCENTILE_EITHER_SIDE);
            double colEndRank = Math.Max(0, colThresholdRank - PERCENTILE_EITHER_SIDE);
            double rowStartRank = Math.Min(100, rowThresholdRank + PERCENTILE_EITHER_SIDE);
            double rowEndRank = Math.Max(0, rowThresholdRank - PERCENTILE_EITHER_SIDE);

            double colStartVal = colPercentile.CalculatePercentile(colStartRank);
            double colEndVal = colPercentile.CalculatePercentile(colEndRank);
            double rowStartVal = rowPercentile.CalculatePercentile(rowStartRank);
            double rowEndVal = rowPercentile.CalculatePercentile(rowEndRank);

            //Determine the start & end indices of all the rows & cols, using the calculated start & end threshold vals
            uint[,] colChars = SegmentationAlgorithmHelpers.FindCharIndices(colDarkPixelCounts, colStartVal, colEndVal);
            uint[,] rowChars = SegmentationAlgorithmHelpers.FindCharIndices(rowDarkPixelCounts, rowStartVal, rowEndVal);

            rows = rowChars.ToIntArr();
            cols = colChars.ToIntArr();
        }
        public void TestThresholdValue1()
        {
            //Test finding the value of the threshold in data where this is trivial
            double[] data = { 1, 3 };
            BimodalHistogram hist = new BimodalHistogram(data, 1, 4, 3);

            //Check bins are as expected
            uint[] expectedBins = { 1, 0, 1 };
            CollectionAssert.AreEqual(expectedBins, hist.Bins);

            //Check the threshold value selected 
            double expected = 2;
            Assert.AreEqual(expected, hist.ThresholdValue);
        }
        public void TestThreshold2()
        {
            //Test finding threshold in data similar to two gaussians not quite fully separated
            double[] data = { 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 7 };
            BimodalHistogram hist = new BimodalHistogram(data, 1, 7, 7);

            //Check bins are as expected
            uint[] expectedBins = { 1, 2, 4, 2, 4, 2, 1 };
            CollectionAssert.AreEqual(expectedBins, hist.Bins);

            //Check the threshold index selected
            int expected = 3;
            Assert.AreEqual(expected, hist.ThresholdBinIdx);
        }
        public void TestThreshold1()
        {
            //Test finding the threshold in data where this is trivial
            double[] data = { 1, 3 };
            BimodalHistogram hist = new BimodalHistogram(data, 1, 3, 3);

            //Check bins are as expected
            uint[] expectedBins = { 1, 0, 1 };
            CollectionAssert.AreEqual(expectedBins, hist.Bins);

            //Check the threshold index selected 
            int expected = 1;
            Assert.AreEqual(expected, hist.ThresholdBinIdx);
        }
        public void DoSegment(bool[,] image, out int[,] rows, out int[,] cols)
        {
            //Count the number of dark pixels per row and column
            uint[] colDarkPixelCounts = SegmentationAlgorithmHelpers.CountNumDarkPixelsPerCol(image);
            uint[] rowDarkPixelCounts = SegmentationAlgorithmHelpers.CountNumDarkPixelsPerRow(image);

            //Calculate the thresholds to use
            BimodalHistogram colHist = new BimodalHistogram(colDarkPixelCounts.ToDoubleArr(), HISTOGRAM_NUM_BINS);
            BimodalHistogram rowHist = new BimodalHistogram(rowDarkPixelCounts.ToDoubleArr(), HISTOGRAM_NUM_BINS);

            //Determine the start & end indices of all the rows & cols
            uint[,] colChars = SegmentationAlgorithmHelpers.FindCharIndices(colDarkPixelCounts, colHist.ThresholdValue);
            uint[,] rowChars = SegmentationAlgorithmHelpers.FindCharIndices(rowDarkPixelCounts, rowHist.ThresholdValue);

            rows = rowChars.ToIntArr();
            cols = colChars.ToIntArr();
        }