Example #1
0
        /// <summary>
        /// </summary>
        /// <param name="m">The spectral sonogram passes as matrix of doubles.</param>
        public static void DoLocalOtsuThresholding(double[,] m, out byte[,] opByteMatrix)
        {
            int byteThreshold      = 30;
            int minPercentileBound = 5;
            int maxPercentileBound = 95;
            int temporalNh         = 15;
            int freqBinNh          = 15;

            int rowCount = m.GetLength(0);
            int colCount = m.GetLength(1);

            //double[,] normM  = MatrixTools.NormaliseInZeroOne(m);
            var ipByteMatrix = MatrixTools.ConvertMatrixOfDouble2Byte(m);
            var bd1          = DataTools.GetByteDistribution(ipByteMatrix);

            opByteMatrix = new byte[rowCount, colCount];

            // for all cols i.e. freq bins
            for (int col = freqBinNh; col < colCount - freqBinNh; col++)
            {
                // for all rows i.e. frames
                for (int row = temporalNh; row < rowCount - temporalNh; row++)
                {
                    var localMatrix = MatrixTools.Submatrix(ipByteMatrix, row - temporalNh, col - freqBinNh, row + temporalNh, col + freqBinNh);

                    // debug check for min and max - make sure it worked
                    int[] bd = DataTools.GetByteDistribution(localMatrix);

                    int[] histo         = Histogram.Histo(localMatrix, out var minIntensity, out var maxIntensity);
                    int   lowerBinBound = Histogram.GetPercentileBin(histo, minPercentileBound);
                    int   upperBinBound = Histogram.GetPercentileBin(histo, maxPercentileBound);
                    int   range         = upperBinBound - lowerBinBound;

                    //normM[row, col] = (upperBinBound - lowerBinBound);
                    if (range > byteThreshold)
                    {
                        var    thresholder = new OtsuThresholder();
                        byte[] vector      = DataTools.Matrix2Array(localMatrix);
                        int    threshold   = thresholder.CalculateThreshold(vector);
                        if (localMatrix[temporalNh, freqBinNh] > threshold)
                        {
                            opByteMatrix[row, col] = 255;
                        }
                    }
                }
            }

            // debug check for min and max - make sure it worked
            var bd2 = DataTools.GetByteDistribution(opByteMatrix);

            bd2 = null;
        }
Example #2
0
        public static void GetOtsuThreshold(byte[,] matrix, out byte[,] m2, out int threshold)
        {
            int width  = matrix.GetLength(1);
            int height = matrix.GetLength(0);

            byte[] vector = DataTools.Matrix2Array(matrix);

            // Create Otsu Thresholder
            OtsuThresholder thresholder = new OtsuThresholder();

            threshold = thresholder.CalculateThreshold(vector, out var outputArray);
            m2        = DataTools.Array2Matrix(outputArray, width, height);
        }
Example #3
0
        public static void GetOtsuThreshold(byte[,] matrix, out byte[,] m2, out int threshold, out Image <Rgb24> histogramImage)
        {
            int width  = matrix.GetLength(1);
            int height = matrix.GetLength(0);

            byte[] vector = DataTools.Matrix2Array(matrix);

            // Create Otsu Thresholder
            OtsuThresholder thresholder = new OtsuThresholder();

            threshold      = thresholder.CalculateThreshold(vector, out var outputArray);
            m2             = DataTools.Array2Matrix(outputArray, width, height);
            histogramImage = CreateHistogramFrame(thresholder, height, 256);
        }